Class: ODDB::Session

Inherits:
SBSM::Session
  • Object
show all
Defined in:
src/util/session.rb,
test/stub/session.rb

Constant Summary

LF_FACTORY =
LookandfeelFactory
DEFAULT_FLAVOR =
"gcc"
DEFAULT_LANGUAGE =
"de"
DEFAULT_STATE =
State::Drugs::Init
DEFAULT_ZONE =
:drugs
SERVER_NAME =
'www.oddb.org'
'oddb-preferences'
QUERY_LIMIT =
5
QUERY_LIMIT_AGE =
60 * 60 * 24

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Session) initialize(key, app, validator = nil)

A new instance of Session



31
32
33
34
35
# File 'src/util/session.rb', line 31

def initialize(key, app, validator=nil)
  super(key, app, validator)
  @interaction_basket = []
  @currency_rates = {}
end

Instance Attribute Details

- (Object) desired_state

Returns the value of attribute desired_state



13
14
15
# File 'src/util/session.rb', line 13

def desired_state
  @desired_state
end

- (Object) lookandfeel=(value) (writeonly)

Sets the attribute lookandfeel

Parameters:

  • value

    the value to set the attribute lookandfeel to.



9
10
11
# File 'test/stub/session.rb', line 9

def lookandfeel=(value)
  @lookandfeel = value
end

- (Object) request (readonly)

Returns the value of attribute request



8
9
10
# File 'test/stub/session.rb', line 8

def request
  @request
end

- (Object) state (readonly)

Returns the value of attribute state



8
9
10
# File 'test/stub/session.rb', line 8

def state
  @state
end

Class Method Details

+ (Object) reset_query_limit(ip = nil)



24
25
26
27
28
29
30
# File 'src/util/session.rb', line 24

def Session.reset_query_limit(ip = nil)
  if(ip)
    @@requests.delete(ip)
  else
    @@requests.clear
  end
end

Instance Method Details

- (Object) active_state



36
37
38
39
40
41
42
43
44
45
# File 'src/util/session.rb', line 36

def active_state
  state = super
  unless  
     = true
    if user = 
      state = state.autologin user
    end
  end
  state
end

- (Object) add_to_interaction_basket(object)



117
118
119
# File 'src/util/session.rb', line 117

def add_to_interaction_basket(object)
  @interaction_basket = @interaction_basket.push(object).uniq
end

- (Boolean) allowed?(*args)

Returns:

  • (Boolean)


46
47
48
# File 'src/util/session.rb', line 46

def allowed?(*args)
  @user.allowed?(*args)
end

- (Object) analysis_alphabetical(range)



147
148
149
# File 'src/util/session.rb', line 147

def analysis_alphabetical(range)
  @app.search_analysis_alphabetical(range, self.language)
end

- (Object) clear_interaction_basket



120
121
122
# File 'src/util/session.rb', line 120

def clear_interaction_basket
  @interaction_basket.clear
end

- (Object) currency



123
124
125
# File 'src/util/session.rb', line 123

def currency 
  cookie_set_or_get(:currency) || "CHF"
end

- (Object) event



49
50
51
52
53
54
55
56
# File 'src/util/session.rb', line 49

def event
  if(@lookandfeel \
    && persistent_user_input(:flavor) != @lookandfeel.flavor)
    :home
  else
    super || :home
  end
end

- (Boolean) expired?(now = Time.now)

Returns:

  • (Boolean)


57
58
59
# File 'src/util/session.rb', line 57

def expired?(now=Time.now)
  super || (logged_in? && @user.expired?)
end

- (Object) flavor



60
61
62
# File 'src/util/session.rb', line 60

def flavor
  @flavor ||= (@valid_input[:partner] || super)
end

- (Object) get_currency_rate(currency)



126
127
128
# File 'src/util/session.rb', line 126

def get_currency_rate(currency)
  @currency_rates[currency] ||= @app.get_currency_rate(currency)
end

- (Object) interaction_basket



129
130
131
132
133
134
135
136
# File 'src/util/session.rb', line 129

def interaction_basket
  if(ids = user_input(:substance_ids))
    ids = ids.split(/[+ ]/u).collect { |id| id.to_i }
    @interaction_basket.delete_if { |sub| !ids.delete(sub.oid) }
    ids.each { |id| @interaction_basket.push @app.substance(id, false) }
  end
  @interaction_basket = @interaction_basket.compact.uniq
end

- (Object) interaction_basket_count



137
138
139
# File 'src/util/session.rb', line 137

def interaction_basket_count
  @interaction_basket.size
end

- (Object) interaction_basket_ids



140
141
142
# File 'src/util/session.rb', line 140

def interaction_basket_ids
  @interaction_basket.collect { |sub| sub.oid }.join('+')
end


143
144
145
146
# File 'src/util/session.rb', line 143

def interaction_basket_link
  lookandfeel._event_url(:interaction_basket, 
                         :substance_ids => interaction_basket_ids)
end

- (Object) limit_queries



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'src/util/session.rb', line 63

def limit_queries
  requests = (@@requests[remote_ip] ||= [])
  if(@state.limited?)
    requests.delete_if { |other| 
      (@process_start - other) >= QUERY_LIMIT_AGE 
    }
    requests.push(@process_start)
    if(requests.size > QUERY_LIMIT)
      @desired_state = @state
      @active_state = @state = @state.limit_state
      @state.request_path = @desired_state.request_path
    end
  end
end

- (Object) login



77
78
79
80
81
82
83
84
85
86
87
# File 'src/util/session.rb', line 77

def 
  # @app.login raises Yus::YusError
  @user = @app.(user_input(:email), user_input(:pass))
  if cookie_set_or_get(:remember_me)
    set_cookie_input :remember, @user.generate_token
    set_cookie_input :email, @user.
  else
    @cookie_input.delete :remember
  end
  @user
end

- (Object) login_token



88
89
90
91
92
93
94
95
96
97
98
# File 'src/util/session.rb', line 88

def 
  # @app.login raises Yus::YusError
   = get_cookie_input(:email)
  token = get_cookie_input(:remember)
  if  && token && !token.empty?
    @user = @app. , token
    set_cookie_input :remember, @user.generate_token
    @user
  end
rescue Yus::YusError
end

- (Object) logout



99
100
101
102
103
104
105
106
107
# File 'src/util/session.rb', line 99

def logout
  token = get_cookie_input(:remember)
  @cookie_input.delete :remember
  if(@user.respond_to?(:yus_session))
    @user.remove_token token
    @app.logout(@user.yus_session)
  end
  super
end

- (Object) migel_alphabetical(range)



150
151
152
# File 'src/util/session.rb', line 150

def migel_alphabetical(range)
  @app.search_migel_alphabetical(range, self.language)
end


153
154
155
# File 'src/util/session.rb', line 153

def navigation
  @active_state.navigation
end

- (Object) process(request)



108
109
110
111
112
113
114
115
116
# File 'src/util/session.rb', line 108

def process(request)
  @request_path = request.unparsed_uri
  @process_start = Time.now
  super
  if(!is_crawler? && self.lookandfeel.enabled?(:query_limit))
    limit_queries 
  end
  '' ## return empty string across the drb-border
end

- (Object) search_exact_indication(query)



160
161
162
# File 'src/util/session.rb', line 160

def search_exact_indication(query)
  @app.search_exact_indication(query, self.language)
end

- (Object) search_interactions(query)



163
164
165
166
# File 'src/util/session.rb', line 163

def search_interactions(query)
  @persistent_user_input[:search_query] ||= query
  @app.search_interactions(query)
end

- (Object) search_migel_products(query)



167
168
169
170
# File 'src/util/session.rb', line 167

def search_migel_products(query)
  @persistent_user_input[:search_query] ||= query
  @app.search_migel_products(query, self.language)
end

- (Object) search_oddb(query)



156
157
158
159
# File 'src/util/session.rb', line 156

def search_oddb(query)
  @persistent_user_input[:search_query] ||= query
  @app.search_oddb(query, self.language)
end

- (Object) search_substances(query)



171
172
173
174
# File 'src/util/session.rb', line 171

def search_substances(query)
  @persistent_user_input[:search_query] ||= query
  @app.search_substances(query)
end

- (Object) set_persistent_user_input(key, val)



175
176
177
# File 'src/util/session.rb', line 175

def set_persistent_user_input(key, val)
  @persistent_user_input.store(key, val)
end


178
179
180
# File 'src/util/session.rb', line 178

def 
  @app.(flavor)
end