Class: Amee::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/amee/session.rb

Defined Under Namespace

Classes: AmeeError, Expired, MissingParameterOrDuplicateError, NotAuthenticated, NotFound, PermissionDenied, ServerNotFound, UnAuthorized, UnknownError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, authenticate = true) ⇒ Session



22
23
24
25
26
27
# File 'lib/amee/session.rb', line 22

def initialize(username, password, authenticate = true)
  @username = username
  @password = password
  @cache = create_cache_store if caching?
  authenticate! if authenticate
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



13
14
15
# File 'lib/amee/session.rb', line 13

def auth_token
  @auth_token
end

#cacheObject

Returns the value of attribute cache.



13
14
15
# File 'lib/amee/session.rb', line 13

def cache
  @cache
end

#serviceObject

Returns the value of attribute service.



13
14
15
# File 'lib/amee/session.rb', line 13

def service
  @service
end

Class Method Details

.create(username = nil, password = nil) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
# File 'lib/amee/session.rb', line 15

def self.create(username = nil, password = nil)
  username ||= Amee::Config[:username]
  password ||= Amee::Config[:password]
  raise ArgumentError unless !username.nil? && !password.nil?
  new(username, password)
end

Instance Method Details

#api_call(method, parser_name, path, options = {}, use_auth_token = true, &proc) ⇒ Object

this over long ie bad method is to either get the cache for a api call  or actually call the amee api; if the session has expired we will try  to get a new one



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/amee/session.rb', line 80

def api_call(method, parser_name, path, options ={}, use_auth_token =true, &proc)
  if (cache = @cache[cache_key(path, options[:query] || {})]) && caching?
    return cache
  else
    attempts = 0
    begin
      if Amee::Config[:logging]
        Amee::Logging.log_amee_api(method, parser_name, path, options) do
          api_call_without_logging(method, parser_name, path, options, use_auth_token =true, &proc)
        end
      else
        api_call_without_logging(method, parser_name, path, options, use_auth_token =true, &proc)
      end
    rescue Amee::Session::Expired
      attempts +=1 
      if attempts < 2
        self.authenticate!
        retry
      else
        raise
      end
    end
  end
end

#api_call_without_logging(method, parser_name, path, options, use_auth_token = true, &proc) ⇒ Object

we send the service the HTTP method along with the parser we want to use and the path



69
70
71
72
73
74
# File 'lib/amee/session.rb', line 69

def api_call_without_logging(method, parser_name, path, options, use_auth_token =true, &proc)
  result = service.send(method, parser_name, path, options)
  result = yield result if block_given?
  store_resource(path,result, options||{}) if method == :get && caching?
  result
end

#authenticate!Object



41
42
43
44
45
46
47
# File 'lib/amee/session.rb', line 41

def authenticate!
  @auth_token = nil
  unless @auth_token = Amee::Service.auth_token(@username, @password, Amee::Config[:auth_path])
    raise Amee::Session::UnAuthorized.new "Please provide your correct username and password. "
  end
  service.auth_token = @auth_token
end

#authenticated?Boolean



49
50
51
# File 'lib/amee/session.rb', line 49

def authenticated?
  !@auth_token.nil?
end

#caching?Boolean



37
38
39
# File 'lib/amee/session.rb', line 37

def caching?
  Amee::Config[:cache]
end

#create_cache_storeMoneta::Store

We create a cache store vie using the config cache_store and parameters if they exist; Moneta Stores are only supported



32
33
34
# File 'lib/amee/session.rb', line 32

def create_cache_store
  @cache = Amee::Config[:cache_store].send(:new, *Amee::Config[:cache_store_parameters] || nil)
end

#create_profileAmee::ProfileApi::Profile



138
139
140
141
142
143
# File 'lib/amee/session.rb', line 138

def create_profile
  @cache.clear
  api_call(:post, "create.profile", "/profiles", :query => {:profile => true}) do |response|
    Amee::ProfileApi::Profile.from_hash(response, self)
  end
end

#create_profile_item(path, uid, options = {}) ⇒ Amee::ProfileApi::ProfileCategory, String

 If you want a full representation; which is the profile category by the way then pass :representation => true in the options hash



171
172
173
174
175
176
177
178
179
180
# File 'lib/amee/session.rb', line 171

def create_profile_item(path, uid, options = {})
  @cache.clear
  api_call(:post, options[:representation] ? "create.profile_item" : "profile_item.location", path, 
    :query => {:dataItemUid => uid, :representation => options[:representation] ? "full" : ""}.merge(options[:query])) do |response|
      return response if (options[:raw_response] || !response.is_a?(Hash))
      Amee::ProfileApi::ProfileCategory.from_hash(response, self) do |profile_category|
        profile_category.lazy_loaded = true
      end
    end
end

#delete_profile(uid) ⇒ Boolean



192
193
194
195
196
197
# File 'lib/amee/session.rb', line 192

def delete_profile(uid)
  @cache.clear
  api_call(:delete, "delete.profile", "/profiles/#{uid}") do |response|
    true
  end
end

#delete_profile_item(path) ⇒ Boolean



200
201
202
203
204
205
# File 'lib/amee/session.rb', line 200

def delete_profile_item(path)
  @cache.clear
  api_call(:delete, "delete.profile_item", path) do |response|
    true
  end
end

#drill(path, options = {}) ⇒ Amee::DataApi::DrillDown



115
116
117
118
119
# File 'lib/amee/session.rb', line 115

def drill(path, options = {})
  api_call(:get, "data.drill", "#{path}/drill", options) do |response|
    Amee::DataApi::DrillDown.from_hash(response, self)
  end
end

#get_data_category(path, options = {}) ⇒ Amee::DataApi::DataCategory



106
107
108
109
110
111
112
# File 'lib/amee/session.rb', line 106

def get_data_category(path, options = {})
  api_call(:get, "data.category", path, options) do |response|
    Amee::DataApi::DataCategory.from_hash(response, self) do |data_category|
      data_category.lazy_loaded = true
    end
  end
end

#get_data_item(path, options = {}) ⇒ Amee::DataApi::DataItem



122
123
124
125
126
127
128
# File 'lib/amee/session.rb', line 122

def get_data_item(path, options = {})
  api_call(:get, "data.item", path, options) do |response|
    Amee::DataApi::DataItem.from_hash(response, self) do |data_item|
      data_item.lazy_loaded = true
    end
  end
end

#get_data_item_value(path, options = {}) ⇒ Amee::DataApi::DataItemValue



131
132
133
134
135
# File 'lib/amee/session.rb', line 131

def get_data_item_value(path, options ={})
  api_call(:get, "data.item_value", path, options) do |response|
    Amee::DataApi::DataItemValue.from_hash(response, self)
  end
end

#get_profile(uid) ⇒ Amee::ProfileApi::Profile



146
147
148
149
150
# File 'lib/amee/session.rb', line 146

def get_profile(uid)
  api_call(:get, "get.profile", "/profiles/#{uid}") do |response|
    Amee::ProfileApi::Profile.from_hash(response, self)
  end
end

#get_profile_category(path, options = {}) ⇒ Amee::ProfileApi::ProfileCategory



208
209
210
211
212
213
214
# File 'lib/amee/session.rb', line 208

def get_profile_category(path, options = {})
  api_call(:get, "profile_category", path, options) do |response|
    Amee::ProfileApi::ProfileCategory.from_hash(response, self) do |profile_category|
      profile_category.lazy_loaded = true
    end
  end
end

#get_profile_item(path, options = {}) ⇒ Amee::ProfileApi::ProfileItem



217
218
219
220
221
222
223
# File 'lib/amee/session.rb', line 217

def get_profile_item(path, options = {})
  api_call(:get, "profile_item", path, options) do |response|
    Amee::ProfileApi::ProfileItem.from_hash(response, self) do |profile_item|
      profile_item.lazy_loaded = true
    end
  end
end

#get_raw(path, options = {}) ⇒ Hash



235
236
237
# File 'lib/amee/session.rb', line 235

def get_raw(path, options = {})
  api_call(:get, "raw", path)
end

#new_profileHash

does not create a profile object compared to the create version



184
185
186
187
188
189
# File 'lib/amee/session.rb', line 184

def new_profile
  @cache.clear
  api_call(:post, "create.profile", "/profiles", :query => {:profile => true}) do |response|
    response
  end
end

#profilesArray[Amee::Profile]



226
227
228
229
230
231
232
# File 'lib/amee/session.rb', line 226

def profiles
  api_call(:get, "profiles", "/profiles") do |response|
    response.map do |profile|
      Amee::ProfileApi::Profile.from_hash(profile, self) unless profile.empty?
    end
  end
end

#store_resource(path, result, options = {}) ⇒ Object



241
242
243
244
245
# File 'lib/amee/session.rb', line 241

def store_resource(path, result, options = {})
  key = cache_key(path, options[:query] ||{})
  cache_options = options[:cache] || {}
  @cache.store(key, result, :expires_in => cache_options[:expires_in] || Amee::Config[:expires_in])
end

#update_profile_item(path, options = {}) ⇒ Amee::ProfileApi::ProfileItem

Note:

representation FULL is not working on AMEE VERSION 2.0 ticket has been raised



156
157
158
159
160
161
162
163
# File 'lib/amee/session.rb', line 156

def update_profile_item(path, options = {})
  @cache.clear
  api_call(:put, options[:representation] ? "profile_item" : "ok", path, 
    :body => {:representation => options[:representation] ? "full" : ""}.merge(options[:query])) do |response|
    return response if (options[:raw_response] || !response.is_a?(Hash))
    Amee::ProfileApi::ProfileItem.from_hash(response, self)
  end
end