Class: Attune::Client
- Inherits:
-
Object
- Object
- Attune::Client
- Includes:
- Configurable
- Defined in:
- lib/attune/client.rb
Constant Summary
Constants included from Configurable
Instance Attribute Summary
Attributes included from Configurable
#auth_token, #disabled, #endpoint, #exception_handler, #middleware, #timeout
Instance Method Summary collapse
-
#bind(id, customer_id) ⇒ Object
Binds an anonymous user to a customer id.
-
#create_anonymous(options) ⇒ Object
Create an anonymous tracked user.
-
#get_auth_token(client_id, client_secret) ⇒ Object
Request an auth token.
-
#get_rankings(options) ⇒ Hash{Symbol => Hash, Array<String>}
Returns all entities from the specified collection in order of the user's preference.
-
#initialize(options = {}) ⇒ Object
constructor
Initializes a new Client.
-
#multi_get_rankings(multi_options) ⇒ Hash{Symbol => Hash, Array<Array<String>>}
Get multiple rankings in one call.
Methods included from Configurable
Constructor Details
#initialize(options = {}) ⇒ Object
Initializes a new Client
29 30 31 32 33 |
# File 'lib/attune/client.rb', line 29 def initialize(={}) Attune::Configurable::KEYS.each do |key| send("#{key}=", [key] || Attune::Default.send(key)) end end |
Instance Method Details
#bind(id, customer_id) ⇒ Object
Binds an anonymous user to a customer id
195 196 197 198 |
# File 'lib/attune/client.rb', line 195 def bind(id, customer_id) put("bindings/anonymous=#{id}&customer=#{customer_id}") true end |
#create_anonymous(options) ⇒ Object
Create an anonymous tracked user
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/attune/client.rb', line 84 def create_anonymous() raise ArgumentError, "user_agent required" unless [:user_agent] if id = [:id] put("anonymous/#{id}", {user_agent: [:user_agent]}) id else if response = post_json("anonymous", {user_agent: [:user_agent]}) response[:location][/\Aurn:id:([a-z0-9\-]+)\Z/, 1] else # Return a new UUID if there was an exception and we're in mock mode SecureRandom.uuid end end end |
#get_auth_token(client_id, client_secret) ⇒ Object
Request an auth token
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/attune/client.rb', line 45 def get_auth_token(client_id, client_secret) raise ArgumentError, "client_id required" unless client_id raise ArgumentError, "client_secret required" unless client_secret response = post_form("oauth/token", client_id: client_id, client_secret: client_secret, grant_type: :client_credentials ) if response body = JSON.parse(response.body) if body['error'] raise AuthenticationException, body['error_description'] end body['access_token'] else # Return a new UUID if there was an exception and we're in mock mode SecureRandom.uuid end end |
#get_rankings(options) ⇒ Hash{Symbol => Hash, Array<String>}
Returns all entities from the specified collection in order of the user's preference
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/attune/client.rb', line 121 def get_rankings() qs = encoded_ranking_params() rankings = {} if response = get("rankings/#{qs}", customer: .fetch(:customer, 'none')) rankings[:headers] = response.headers.select { |k,v| k =~ /^attune/ } rankings[:entities] = JSON.parse(response.body)['ranking'] else # In mock mode: return the entities in the order passed in rankings[:headers] = {"attune-cell"=>"mock", "attune-ranking"=>"mock"} rankings[:entities] = [:entities] end rankings end |
#multi_get_rankings(multi_options) ⇒ Hash{Symbol => Hash, Array<Array<String>>}
Get multiple rankings in one call
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/attune/client.rb', line 158 def multi_get_rankings() requests = .map do || encoded_ranking_params() end rankings = {} if response = get("rankings", ids: requests) results = JSON.parse(response.body)['results'] rankings[:headers] = response.headers.select { |k,v| k =~ /^attune/ } # Order of encoded paramaters may change, so we must parse them results = Hash[results.map do |request, result| [CGI.parse(request), result] end] rankings[:entities] = requests.map do |request| results[CGI.parse(request)]['ranking'] end else # In mock mode: return the entities in the order passed in rankings[:headers] = {"attune-cell"=>"mock", "attune-ranking"=>"mock"} rankings[:entities] = .map do || [:entities] end end rankings end |