attune 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -5
- data/attune.gemspec +3 -3
- data/lib/attune.rb +14 -0
- data/lib/attune/api/anonymous.rb +131 -0
- data/lib/attune/api/entities.rb +195 -0
- data/lib/attune/call_dropping.rb +1 -0
- data/lib/attune/client.rb +20 -170
- data/lib/attune/configurable.rb +1 -0
- data/lib/attune/default.rb +1 -0
- data/lib/attune/json_logger.rb +1 -0
- data/lib/attune/models/anonymousresult.rb +41 -0
- data/lib/attune/models/batchrankingrequest.rb +44 -0
- data/lib/attune/models/batchrankingresult.rb +44 -0
- data/lib/attune/models/blacklist.rb +111 -0
- data/lib/attune/models/blacklistgetresponse.rb +44 -0
- data/lib/attune/models/blacklistparams.rb +87 -0
- data/lib/attune/models/blacklistsaveresponse.rb +41 -0
- data/lib/attune/models/customer.rb +41 -0
- data/lib/attune/models/rankedentities.rb +60 -0
- data/lib/attune/models/rankingparams.rb +103 -0
- data/lib/attune/models/scopeentry.rb +49 -0
- data/lib/attune/net_http_persistent.rb +1 -0
- data/lib/attune/param_flattener.rb +1 -0
- data/lib/attune/version.rb +2 -1
- data/spec/attune/client_spec.rb +51 -59
- data/spec/remote_spec.rb +27 -27
- metadata +40 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb1defe49351ac0ececcbee2e6234ecd06dd923e
|
4
|
+
data.tar.gz: f5d0b12d7b5f6514847b2de1bcd07977b1b6acfb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0251f285edcf9c2e759928f87ac2407e2e6cd274ee71bceda05b959a87a64eb26a7eadce39744fc5fc8c0c60d654d2b4cae3a1d222d8bffcc7e514cde8afb79c
|
7
|
+
data.tar.gz: 07d2c0d9cae0cecf2158438e9597832c0fcad15f9e1f455cefde22bde4f2dbf81aa73d14d4f32c4f40cce1ab119f5897a8d9950e7f0c17126f7ee218f1492e36
|
data/README.md
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
A client for the [Attune ranking API](http://attune.co/). Built using the excellent [faraday](https://github.com/lostisland/faraday) library.
|
4
4
|
|
5
|
-
* [Github](https://github.com/
|
5
|
+
* [Github](https://github.com/attune-api/attune-ruby)
|
6
6
|
* [RubyGems](https://rubygems.org/gems/attune)
|
7
|
-
* [Documentation](http://rdoc.info/github/
|
7
|
+
* [Documentation](http://rdoc.info/github/attune-api/attune-ruby)
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -103,9 +103,9 @@ client = Attune::Client.new(auth_token: "my-secure-auth-token", timeout: 2)
|
|
103
103
|
```
|
104
104
|
|
105
105
|
See the documentation for
|
106
|
-
[Attune::Configurable](http://rdoc.info/github/
|
106
|
+
[Attune::Configurable](http://rdoc.info/github/attune-api/attune-ruby/master/Attune/Configurable)
|
107
107
|
and the
|
108
|
-
[default configuration](http://rdoc.info/github/
|
108
|
+
[default configuration](http://rdoc.info/github/attune-api/attune-ruby/master/Attune/Default)
|
109
109
|
for more details.
|
110
110
|
|
111
111
|
### Testing
|
@@ -121,7 +121,7 @@ Calling `.test_mode!` is equivalent to setting `disabled` to true and `exception
|
|
121
121
|
|
122
122
|
## Contributing
|
123
123
|
|
124
|
-
1. Fork it ( http://github.com/
|
124
|
+
1. Fork it ( http://github.com/attune-api/attune-ruby/fork )
|
125
125
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
126
126
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
127
127
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/attune.gemspec
CHANGED
@@ -6,11 +6,11 @@ require 'attune/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "attune"
|
8
8
|
spec.version = Attune::VERSION
|
9
|
-
spec.authors = ["John Hawthorn"]
|
10
|
-
spec.email = ["john@freerunningtechnologies.com"]
|
9
|
+
spec.authors = ["John Hawthorn", "Jason Gritman"]
|
10
|
+
spec.email = ["john@freerunningtechnologies.com", "jason@attune.co"]
|
11
11
|
spec.summary = %q{Client for the Attune product ranking API.}
|
12
12
|
spec.description = %q{Client for the Attune product ranking API.}
|
13
|
-
spec.homepage = "https://github.com/
|
13
|
+
spec.homepage = "https://github.com/attune-api/attune-ruby"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
data/lib/attune.rb
CHANGED
@@ -4,6 +4,19 @@ require "attune/version"
|
|
4
4
|
require "attune/configurable"
|
5
5
|
require "attune/default"
|
6
6
|
require "attune/client"
|
7
|
+
require "attune/api/entities"
|
8
|
+
require "attune/api/anonymous"
|
9
|
+
require "attune/models/blacklistgetresponse"
|
10
|
+
require "attune/models/anonymousresult"
|
11
|
+
require "attune/models/blacklistparams"
|
12
|
+
require "attune/models/rankedentities"
|
13
|
+
require "attune/models/rankingparams"
|
14
|
+
require "attune/models/customer"
|
15
|
+
require "attune/models/blacklist"
|
16
|
+
require "attune/models/batchrankingrequest"
|
17
|
+
require "attune/models/scopeentry"
|
18
|
+
require "attune/models/blacklistsaveresponse"
|
19
|
+
require "attune/models/batchrankingresult"
|
7
20
|
|
8
21
|
module Attune
|
9
22
|
def self.client
|
@@ -23,3 +36,4 @@ module Attune
|
|
23
36
|
Default.configure(&block)
|
24
37
|
end
|
25
38
|
end
|
39
|
+
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require "json"
|
2
|
+
require "uri"
|
3
|
+
|
4
|
+
module Attune
|
5
|
+
module Api
|
6
|
+
class Anonymous
|
7
|
+
basePath = "/"
|
8
|
+
|
9
|
+
def initialize(client)
|
10
|
+
@client = client
|
11
|
+
end
|
12
|
+
|
13
|
+
def escapeString(string)
|
14
|
+
URI.encode(string.to_s)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Creates an anonymous visitor.
|
18
|
+
#
|
19
|
+
# @return [Attune::Model::AnonymousResult]
|
20
|
+
# @raise [ArgumentError] if user_agent is not provided
|
21
|
+
# @raise [Faraday::Error::ClientError] if the request fails or exceeds the timeout
|
22
|
+
# @raise [AuthenticationException] if authorization header not accepted
|
23
|
+
def create ()
|
24
|
+
query_param_keys = []
|
25
|
+
|
26
|
+
# set default values and merge with input
|
27
|
+
options = {
|
28
|
+
}
|
29
|
+
|
30
|
+
#resource path
|
31
|
+
path = "/anonymous".sub('{format}','json')
|
32
|
+
|
33
|
+
# pull querystring keys from options
|
34
|
+
queryopts = options.select do |key,value|
|
35
|
+
query_param_keys.include? key
|
36
|
+
end
|
37
|
+
|
38
|
+
headers = nil
|
39
|
+
post_body = nil
|
40
|
+
response = @client.request(:POST, path, {:params=>queryopts,:headers=>headers, :body=>post_body })
|
41
|
+
Attune::Model::AnonymousResult.new(JSON.parse(response.body))
|
42
|
+
end
|
43
|
+
|
44
|
+
# Binds one actor to another, allowing activities of those actors to be shared between the two.
|
45
|
+
#
|
46
|
+
# @param [string] anonymous
|
47
|
+
# @param [Customer] body
|
48
|
+
# @raise [ArgumentError] if user_agent is not provided
|
49
|
+
# @raise [Faraday::Error::ClientError] if the request fails or exceeds the timeout
|
50
|
+
# @raise [AuthenticationException] if authorization header not accepted
|
51
|
+
def update (anonymous,body)
|
52
|
+
query_param_keys = []
|
53
|
+
|
54
|
+
# verify existence of params
|
55
|
+
raise "anonymous is required" if anonymous.nil?
|
56
|
+
raise "body is required" if body.nil?
|
57
|
+
# set default values and merge with input
|
58
|
+
options = {
|
59
|
+
:anonymous => anonymous,
|
60
|
+
:body => body}
|
61
|
+
|
62
|
+
#resource path
|
63
|
+
path = "/anonymous/{anonymous}".sub('{format}','json').sub('{' + 'anonymous' + '}', escapeString(anonymous))
|
64
|
+
|
65
|
+
|
66
|
+
# pull querystring keys from options
|
67
|
+
queryopts = options.select do |key,value|
|
68
|
+
query_param_keys.include? key
|
69
|
+
end
|
70
|
+
|
71
|
+
headers = nil
|
72
|
+
post_body = nil
|
73
|
+
if body != nil
|
74
|
+
if body.is_a?(Array)
|
75
|
+
array = Array.new
|
76
|
+
body.each do |item|
|
77
|
+
if item.respond_to?("to_body".to_sym)
|
78
|
+
array.push item.to_body
|
79
|
+
else
|
80
|
+
array.push item
|
81
|
+
end
|
82
|
+
end
|
83
|
+
post_body = array
|
84
|
+
|
85
|
+
else
|
86
|
+
if body.respond_to?("to_body".to_sym)
|
87
|
+
post_body = body.to_body
|
88
|
+
else
|
89
|
+
post_body = body
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
@client.request(:PUT, path, {:params=>queryopts,:headers=>headers, :body=>post_body})
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
# get
|
98
|
+
#
|
99
|
+
# @param [string] anonymous
|
100
|
+
# @return [Attune::Model::Customer]
|
101
|
+
# @raise [ArgumentError] if user_agent is not provided
|
102
|
+
# @raise [Faraday::Error::ClientError] if the request fails or exceeds the timeout
|
103
|
+
# @raise [AuthenticationException] if authorization header not accepted
|
104
|
+
def get (anonymous)
|
105
|
+
query_param_keys = []
|
106
|
+
|
107
|
+
# verify existence of params
|
108
|
+
raise "anonymous is required" if anonymous.nil?
|
109
|
+
# set default values and merge with input
|
110
|
+
options = {
|
111
|
+
:anonymous => anonymous}
|
112
|
+
|
113
|
+
#resource path
|
114
|
+
path = "/anonymous/{anonymous}".sub('{format}','json').sub('{' + 'anonymous' + '}', escapeString(anonymous))
|
115
|
+
|
116
|
+
|
117
|
+
# pull querystring keys from options
|
118
|
+
queryopts = options.select do |key,value|
|
119
|
+
query_param_keys.include? key
|
120
|
+
end
|
121
|
+
|
122
|
+
headers = nil
|
123
|
+
post_body = nil
|
124
|
+
response = @client.request(:GET, path, {:params=>queryopts,:headers=>headers, :body=>post_body })
|
125
|
+
Attune::Model::Customer.new(JSON.parse(response.body))
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
@@ -0,0 +1,195 @@
|
|
1
|
+
require "json"
|
2
|
+
require "uri"
|
3
|
+
|
4
|
+
module Attune
|
5
|
+
module Api
|
6
|
+
class Entities
|
7
|
+
basePath = "/"
|
8
|
+
|
9
|
+
def initialize(client)
|
10
|
+
@client = client
|
11
|
+
end
|
12
|
+
|
13
|
+
def escapeString(string)
|
14
|
+
URI.encode(string.to_s)
|
15
|
+
end
|
16
|
+
|
17
|
+
# blacklistGet
|
18
|
+
#
|
19
|
+
# @return [Attune::Model::BlacklistGetResponse]
|
20
|
+
# @raise [ArgumentError] if user_agent is not provided
|
21
|
+
# @raise [Faraday::Error::ClientError] if the request fails or exceeds the timeout
|
22
|
+
# @raise [AuthenticationException] if authorization header not accepted
|
23
|
+
def blacklist_get ()
|
24
|
+
query_param_keys = []
|
25
|
+
|
26
|
+
# set default values and merge with input
|
27
|
+
options = {
|
28
|
+
}
|
29
|
+
|
30
|
+
#resource path
|
31
|
+
path = "/entities/rankings/blacklists".sub('{format}','json')
|
32
|
+
|
33
|
+
# pull querystring keys from options
|
34
|
+
queryopts = options.select do |key,value|
|
35
|
+
query_param_keys.include? key
|
36
|
+
end
|
37
|
+
|
38
|
+
headers = nil
|
39
|
+
post_body = nil
|
40
|
+
response = @client.request(:GET, path, {:params=>queryopts,:headers=>headers, :body=>post_body })
|
41
|
+
Attune::Model::BlacklistGetResponse.new(JSON.parse(response.body))
|
42
|
+
end
|
43
|
+
|
44
|
+
# blacklistSave
|
45
|
+
#
|
46
|
+
# @param [BlacklistParams] body
|
47
|
+
# @return [Attune::Model::BlacklistSaveResponse]
|
48
|
+
# @raise [ArgumentError] if user_agent is not provided
|
49
|
+
# @raise [Faraday::Error::ClientError] if the request fails or exceeds the timeout
|
50
|
+
# @raise [AuthenticationException] if authorization header not accepted
|
51
|
+
def blacklist_save (body)
|
52
|
+
query_param_keys = []
|
53
|
+
|
54
|
+
# set default values and merge with input
|
55
|
+
options = {
|
56
|
+
:body => body}
|
57
|
+
|
58
|
+
#resource path
|
59
|
+
path = "/entities/rankings/blacklists".sub('{format}','json')
|
60
|
+
|
61
|
+
# pull querystring keys from options
|
62
|
+
queryopts = options.select do |key,value|
|
63
|
+
query_param_keys.include? key
|
64
|
+
end
|
65
|
+
|
66
|
+
headers = nil
|
67
|
+
post_body = nil
|
68
|
+
if body != nil
|
69
|
+
if body.is_a?(Array)
|
70
|
+
array = Array.new
|
71
|
+
body.each do |item|
|
72
|
+
if item.respond_to?("to_body".to_sym)
|
73
|
+
array.push item.to_body
|
74
|
+
else
|
75
|
+
array.push item
|
76
|
+
end
|
77
|
+
end
|
78
|
+
post_body = array
|
79
|
+
|
80
|
+
else
|
81
|
+
if body.respond_to?("to_body".to_sym)
|
82
|
+
post_body = body.to_body
|
83
|
+
else
|
84
|
+
post_body = body
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
response = @client.request(:POST, path, {:params=>queryopts,:headers=>headers, :body=>post_body })
|
89
|
+
Attune::Model::BlacklistSaveResponse.new(JSON.parse(response.body))
|
90
|
+
end
|
91
|
+
|
92
|
+
# getRankings
|
93
|
+
#
|
94
|
+
# @param [RankingParams] body
|
95
|
+
# @return [Attune::Model::RankedEntities]
|
96
|
+
# @raise [ArgumentError] if user_agent is not provided
|
97
|
+
# @raise [Faraday::Error::ClientError] if the request fails or exceeds the timeout
|
98
|
+
# @raise [AuthenticationException] if authorization header not accepted
|
99
|
+
def get_rankings (body)
|
100
|
+
query_param_keys = []
|
101
|
+
|
102
|
+
# verify existence of params
|
103
|
+
raise "body is required" if body.nil?
|
104
|
+
# set default values and merge with input
|
105
|
+
options = {
|
106
|
+
:body => body}
|
107
|
+
|
108
|
+
#resource path
|
109
|
+
path = "/entities/ranking".sub('{format}','json')
|
110
|
+
|
111
|
+
# pull querystring keys from options
|
112
|
+
queryopts = options.select do |key,value|
|
113
|
+
query_param_keys.include? key
|
114
|
+
end
|
115
|
+
|
116
|
+
headers = nil
|
117
|
+
post_body = nil
|
118
|
+
if body != nil
|
119
|
+
if body.is_a?(Array)
|
120
|
+
array = Array.new
|
121
|
+
body.each do |item|
|
122
|
+
if item.respond_to?("to_body".to_sym)
|
123
|
+
array.push item.to_body
|
124
|
+
else
|
125
|
+
array.push item
|
126
|
+
end
|
127
|
+
end
|
128
|
+
post_body = array
|
129
|
+
|
130
|
+
else
|
131
|
+
if body.respond_to?("to_body".to_sym)
|
132
|
+
post_body = body.to_body
|
133
|
+
else
|
134
|
+
post_body = body
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
response = @client.request(:POST, path, {:params=>queryopts,:headers=>headers, :body=>post_body })
|
139
|
+
Attune::Model::RankedEntities.new(JSON.parse(response.body))
|
140
|
+
end
|
141
|
+
|
142
|
+
# batchGetRankings
|
143
|
+
#
|
144
|
+
# @param [BatchRankingRequest] body
|
145
|
+
# @return [Attune::Model::BatchRankingResult]
|
146
|
+
# @raise [ArgumentError] if user_agent is not provided
|
147
|
+
# @raise [Faraday::Error::ClientError] if the request fails or exceeds the timeout
|
148
|
+
# @raise [AuthenticationException] if authorization header not accepted
|
149
|
+
def batch_get_rankings (body)
|
150
|
+
query_param_keys = []
|
151
|
+
|
152
|
+
# verify existence of params
|
153
|
+
raise "body is required" if body.nil?
|
154
|
+
# set default values and merge with input
|
155
|
+
options = {
|
156
|
+
:body => body}
|
157
|
+
|
158
|
+
#resource path
|
159
|
+
path = "/entities/ranking/many".sub('{format}','json')
|
160
|
+
|
161
|
+
# pull querystring keys from options
|
162
|
+
queryopts = options.select do |key,value|
|
163
|
+
query_param_keys.include? key
|
164
|
+
end
|
165
|
+
|
166
|
+
headers = nil
|
167
|
+
post_body = nil
|
168
|
+
if body != nil
|
169
|
+
if body.is_a?(Array)
|
170
|
+
array = Array.new
|
171
|
+
body.each do |item|
|
172
|
+
if item.respond_to?("to_body".to_sym)
|
173
|
+
array.push item.to_body
|
174
|
+
else
|
175
|
+
array.push item
|
176
|
+
end
|
177
|
+
end
|
178
|
+
post_body = array
|
179
|
+
|
180
|
+
else
|
181
|
+
if body.respond_to?("to_body".to_sym)
|
182
|
+
post_body = body.to_body
|
183
|
+
else
|
184
|
+
post_body = body
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
response = @client.request(:POST, path, {:params=>queryopts,:headers=>headers, :body=>post_body })
|
189
|
+
Attune::Model::BatchRankingResult.new(JSON.parse(response.body))
|
190
|
+
end
|
191
|
+
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
data/lib/attune/call_dropping.rb
CHANGED
data/lib/attune/client.rb
CHANGED
@@ -15,7 +15,8 @@ module Attune
|
|
15
15
|
|
16
16
|
class Client
|
17
17
|
include Attune::Configurable
|
18
|
-
|
18
|
+
attr_reader :entities
|
19
|
+
attr_reader :anonymous
|
19
20
|
# Initializes a new Client
|
20
21
|
#
|
21
22
|
# @example
|
@@ -30,7 +31,9 @@ module Attune
|
|
30
31
|
Attune::Configurable::KEYS.each do |key|
|
31
32
|
send("#{key}=", options[key] || Attune::Default.send(key))
|
32
33
|
end
|
33
|
-
|
34
|
+
@entities = Attune::Api::Entities.new(self)
|
35
|
+
@anonymous = Attune::Api::Anonymous.new(self)
|
36
|
+
end
|
34
37
|
|
35
38
|
# Request an auth token
|
36
39
|
#
|
@@ -46,11 +49,10 @@ module Attune
|
|
46
49
|
raise ArgumentError, "client_id required" unless client_id
|
47
50
|
raise ArgumentError, "client_secret required" unless client_secret
|
48
51
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
)
|
52
|
+
params = {client_id: client_id,
|
53
|
+
client_secret: client_secret,
|
54
|
+
grant_type: :client_credentials}
|
55
|
+
response = post_form("oauth/token", params)
|
54
56
|
if response
|
55
57
|
body = JSON.parse(response.body)
|
56
58
|
if body['error']
|
@@ -63,160 +65,17 @@ module Attune
|
|
63
65
|
end
|
64
66
|
end
|
65
67
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
# )
|
77
|
-
# @param [Hash] options
|
78
|
-
# @option options [String] :id optional. An id will be generated if this is not provided
|
79
|
-
# @option options [String] :user_agent The user agent for the application used by the anonymous users
|
80
|
-
# @return id [String]
|
81
|
-
# @raise [ArgumentError] if user_agent is not provided
|
82
|
-
# @raise [Faraday::Error::ClientError] if the request fails or exceeds the timeout
|
83
|
-
# @raise [AuthenticationException] if authorization header not accepted
|
84
|
-
def create_anonymous(options)
|
85
|
-
raise ArgumentError, "user_agent required" unless options[:user_agent]
|
86
|
-
if id = options[:id]
|
87
|
-
put("anonymous/#{id}", {user_agent: options[:user_agent]})
|
88
|
-
id
|
89
|
-
else
|
90
|
-
if response = post_json("anonymous", {user_agent: options[:user_agent]})
|
91
|
-
response[:location][/\Aurn:id:([a-z0-9\-]+)\Z/, 1]
|
92
|
-
else
|
93
|
-
# Return a new UUID if there was an exception and we're in mock mode
|
94
|
-
SecureRandom.uuid
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
# Returns all entities from the specified collection in order of the user's preference
|
100
|
-
#
|
101
|
-
# @example
|
102
|
-
# rankings = client.get_rankings(
|
103
|
-
# id: '0cddbc0-6114-11e3-949a-0800200c9a66',
|
104
|
-
# view: 'b/mens-pants',
|
105
|
-
# collection: 'products',
|
106
|
-
# entities: %w[1001, 1002, 1003, 1004]
|
107
|
-
# )
|
108
|
-
# @param [Hash] options
|
109
|
-
# @option options [String] :id The anonymous user id for whom to grab rankings
|
110
|
-
# @option options [String] :view The page or app URN on which the entities will be displayed
|
111
|
-
# @option options [String] :collection name of the collection of entities
|
112
|
-
# @option options [Array<String>] :entities entities to be ranked. These should be numeric strings or integers.
|
113
|
-
# @option options [String] :ip ip address of remote user. Used for geolocation (optional)
|
114
|
-
# @option options [String] :customer id of customer (optional)
|
115
|
-
# @return [Hash{Symbol => Hash, Array<String>}]
|
116
|
-
# * :headers the attune headers indicating ranking performed
|
117
|
-
# * :entities entities in their ranked order
|
118
|
-
# @raise [ArgumentError] if required parameters are missing
|
119
|
-
# @raise [Faraday::Error::ClientError] if the request fails or exceeds the timeout
|
120
|
-
# @raise [AuthenticationException] if authorization header not accepted
|
121
|
-
def get_rankings(options)
|
122
|
-
qs = encoded_ranking_params(options)
|
123
|
-
rankings = {}
|
124
|
-
if response = get("rankings/#{qs}", customer: options.fetch(:customer, 'none'))
|
125
|
-
rankings[:headers] = response.headers.select { |k,v| k =~ /^attune/ }
|
126
|
-
rankings[:entities] = JSON.parse(response.body)['ranking']
|
127
|
-
else
|
128
|
-
# In mock mode: return the entities in the order passed in
|
129
|
-
rankings[:headers] = {"attune-cell"=>"mock", "attune-ranking"=>"mock"}
|
130
|
-
rankings[:entities] = options[:entities].map(&:to_s)
|
131
|
-
end
|
132
|
-
rankings
|
133
|
-
end
|
134
|
-
|
135
|
-
# Get multiple rankings in one call
|
136
|
-
#
|
137
|
-
# @example
|
138
|
-
# rankings = client.multi_get_rankings([
|
139
|
-
# {
|
140
|
-
# id: '0cddbc0-6114-11e3-949a-0800200c9a66',
|
141
|
-
# view: 'b/mens-pants',
|
142
|
-
# collection: 'products',
|
143
|
-
# entities: %w[1001, 1002, 1003, 1004]
|
144
|
-
# },
|
145
|
-
# {
|
146
|
-
# id: '0cddbc0-6114-11e3-949a-0800200c9a66',
|
147
|
-
# view: 'b/mens-pants',
|
148
|
-
# collection: 'products',
|
149
|
-
# entities: %w[2001, 2002, 2003, 2004]
|
150
|
-
# }
|
151
|
-
# ])
|
152
|
-
# @param [Array<Hash>] multi_options An array of options (see #get_rankings)
|
153
|
-
# @return [Hash{Symbol => Hash, Array<Array<String>>}]
|
154
|
-
# * :headers the attune headers indicating ranking performed
|
155
|
-
# * :entities entities in their ranked order
|
156
|
-
# @raise [Faraday::Error::ClientError] if the request fails or exceeds the timeout
|
157
|
-
# @raise [AuthenticationException] if authorization header not accepted
|
158
|
-
def multi_get_rankings(multi_options)
|
159
|
-
requests = multi_options.map do |options|
|
160
|
-
encoded_ranking_params(options)
|
161
|
-
end
|
162
|
-
rankings = {}
|
163
|
-
if response = get("rankings", ids: requests)
|
164
|
-
results = JSON.parse(response.body)['results']
|
165
|
-
rankings[:headers] = response.headers.select { |k,v| k =~ /^attune/ }
|
166
|
-
|
167
|
-
# Order of encoded paramaters may change, so we must parse them
|
168
|
-
results = Hash[results.map do |request, result|
|
169
|
-
[CGI.parse(request), result]
|
170
|
-
end]
|
171
|
-
rankings[:entities] = requests.map do |request|
|
172
|
-
results[CGI.parse(request)]['ranking']
|
173
|
-
end
|
174
|
-
else
|
175
|
-
# In mock mode: return the entities in the order passed in
|
176
|
-
rankings[:headers] = {"attune-cell"=>"mock", "attune-ranking"=>"mock"}
|
177
|
-
rankings[:entities] = multi_options.map do |options|
|
178
|
-
options[:entities].map(&:to_s)
|
179
|
-
end
|
68
|
+
def request(http_method, path, opts)
|
69
|
+
adapter_method = adapter.method(http_method.downcase)
|
70
|
+
headers = {'Content-Type' => 'application/json' }
|
71
|
+
headers.merge! opts[:headers] if opts[:headers]
|
72
|
+
adapter_method.call do |req|
|
73
|
+
req.url path
|
74
|
+
req.headers['Content-Type'] = 'application/json'
|
75
|
+
req.headers.merge! opts[:headers] if opts[:headers]
|
76
|
+
req.params = opts[:params] if opts[:params]
|
77
|
+
req.body = ::JSON.dump(opts[:body]) if opts[:body]
|
180
78
|
end
|
181
|
-
rankings
|
182
|
-
end
|
183
|
-
|
184
|
-
# Binds an anonymous user to a customer id
|
185
|
-
#
|
186
|
-
# @param [String] id The anonymous visitor to bind
|
187
|
-
# @param [String] customer_id The customer id to bind
|
188
|
-
# @example
|
189
|
-
# rankings = client.bind(
|
190
|
-
# '25892e17-80f6-415f-9c65-7395632f022',
|
191
|
-
# 'cd171f7c-560d-4a62-8d65-16b87419a58'
|
192
|
-
# )
|
193
|
-
# @raise [Faraday::Error::ClientError] if the request fails or exceeds the timeout
|
194
|
-
# @raise [AuthenticationException] if authorization header not accepted
|
195
|
-
def bind(id, customer_id)
|
196
|
-
put("bindings/anonymous=#{id}&customer=#{customer_id}")
|
197
|
-
true
|
198
|
-
end
|
199
|
-
|
200
|
-
private
|
201
|
-
def encoded_ranking_params(options)
|
202
|
-
params = {
|
203
|
-
anonymous: options.fetch(:id),
|
204
|
-
entities: options.fetch(:entities).join(','),
|
205
|
-
entity_collection: options.fetch(:collection),
|
206
|
-
ip: options.fetch(:ip, 'none'),
|
207
|
-
view: options.fetch(:view)
|
208
|
-
}
|
209
|
-
Faraday::Utils::ParamsHash[params].to_query
|
210
|
-
end
|
211
|
-
|
212
|
-
def get(path, params={})
|
213
|
-
adapter.get(path, params)
|
214
|
-
rescue Errno::ENOENT, Faraday::Error::ClientError => e
|
215
|
-
handle_exception(e)
|
216
|
-
end
|
217
|
-
|
218
|
-
def put(path, params={})
|
219
|
-
adapter.put(path, ::JSON.dump(params))
|
220
79
|
rescue Errno::ENOENT, Faraday::Error::ClientError => e
|
221
80
|
handle_exception(e)
|
222
81
|
end
|
@@ -227,16 +86,6 @@ module Attune
|
|
227
86
|
handle_exception(e)
|
228
87
|
end
|
229
88
|
|
230
|
-
def post_json(path, params={})
|
231
|
-
adapter.post do |req|
|
232
|
-
req.url path
|
233
|
-
req.headers['Content-Type'] = 'application/json'
|
234
|
-
req.body = ::JSON.dump(params)
|
235
|
-
end
|
236
|
-
rescue Errno::ENOENT, Faraday::Error::ClientError => e
|
237
|
-
handle_exception(e)
|
238
|
-
end
|
239
|
-
|
240
89
|
def handle_exception e
|
241
90
|
if exception_handler == :mock
|
242
91
|
nil
|
@@ -259,3 +108,4 @@ module Attune
|
|
259
108
|
end
|
260
109
|
end
|
261
110
|
end
|
111
|
+
|