live_paper 0.0.16 → 0.0.20
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/live_paper/base_object.rb +93 -45
- data/lib/live_paper/image.rb +3 -3
- data/lib/live_paper/link.rb +0 -1
- data/lib/live_paper/qr_trigger.rb +2 -5
- data/lib/live_paper/version.rb +1 -1
- data/lib/live_paper/wm_trigger.rb +5 -5
- data/lib/live_paper.rb +0 -1
- data/spec/live_paper/base_object_spec.rb +63 -10
- metadata +2 -5
- data/lib/live_paper/http_client.rb +0 -87
- data/spec/live_paper/http_client_spec.rb +0 -196
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b78bada6a9a8ab8ff6c8cb229635947544c4e7f
|
4
|
+
data.tar.gz: b350744d43232723db63f9770f449a3c4104aede
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7fa9418f1a5ecc2d26c6473fac545f0fc792aab1d95bb737fc4299b2c3bd2873d6ba6278203e574fc25474a3f971e03dd1423cfabdaca5989ed0d81306c3f7a0
|
7
|
+
data.tar.gz: 63b41015e36b75060691730b83596848f3adf6b83d333dc523cae7a064624ec6de02a3270923a4c2829f0b431289bb9af969af3c13f5f0fe519b0c7683ee0418
|
data/README.md
CHANGED
@@ -145,7 +145,7 @@ You can list existing resources with the list operation.
|
|
145
145
|
|
146
146
|
## Contributing
|
147
147
|
|
148
|
-
1. Fork it ( https://github.com/IPGPTP/
|
148
|
+
1. Fork it ( https://github.com/IPGPTP/live_paper_rubygem/fork )
|
149
149
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
150
150
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
151
151
|
4. Push to the branch (`git push origin my-new-feature`)
|
@@ -1,9 +1,14 @@
|
|
1
|
-
require_relative 'http_client'
|
2
1
|
require 'json'
|
3
2
|
|
3
|
+
class NotAuthenticatedError < Exception
|
4
|
+
end
|
5
|
+
|
4
6
|
module LivePaper
|
5
7
|
class BaseObject
|
6
|
-
|
8
|
+
|
9
|
+
LP_API_HOST="https://www.livepaperapi.com"
|
10
|
+
AUTH_URL = "#{LP_API_HOST}/auth/token"
|
11
|
+
|
7
12
|
attr_accessor :id, :name, :date_created, :date_modified, :link
|
8
13
|
|
9
14
|
def assign_attributes(data)
|
@@ -23,71 +28,66 @@ module LivePaper
|
|
23
28
|
|
24
29
|
def save
|
25
30
|
validate_attributes!
|
26
|
-
|
27
|
-
response = BaseObject.
|
31
|
+
unless present? @id
|
32
|
+
response = BaseObject.rest_request( self.class.api_url, :post, body: create_body.to_json )
|
28
33
|
parse(response.body)
|
29
|
-
end
|
34
|
+
end
|
30
35
|
self
|
31
36
|
end
|
32
37
|
|
33
38
|
def self.get(id)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
39
|
+
response = rest_request( "#{api_url}/#{id}", :get )
|
40
|
+
case response.code
|
41
|
+
when 200
|
42
|
+
parse response.body
|
43
|
+
else #when 404
|
44
|
+
nil
|
45
|
+
end
|
38
46
|
end
|
39
47
|
|
40
48
|
def self.list
|
41
49
|
objects=[]
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
50
|
+
# $lpp_access_token = 'force retry'
|
51
|
+
|
52
|
+
response = rest_request( api_url, :get )
|
53
|
+
JSON.parse(response.body, symbolize_names: true)[list_key].each do |linkdata|
|
54
|
+
objects << self.parse({item_key => linkdata}.to_json)
|
55
|
+
end
|
48
56
|
objects
|
49
57
|
end
|
50
58
|
|
51
59
|
def update
|
52
60
|
response_code = 'Object Invalid'
|
53
61
|
if self.id
|
54
|
-
BaseObject.
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
@errors=response.body
|
68
|
-
'Conflict'
|
69
|
-
else
|
70
|
-
'Object Invalid'
|
71
|
-
end
|
62
|
+
response = BaseObject.rest_request( "#{self.class.api_url}/#{id}", :put, body: update_body.to_json )
|
63
|
+
response_code = case response.code
|
64
|
+
when 200
|
65
|
+
parse(response.body)
|
66
|
+
'200 OK'
|
67
|
+
when 400
|
68
|
+
@errors=response.body
|
69
|
+
'Bad Request'
|
70
|
+
when 409
|
71
|
+
@errors=response.body
|
72
|
+
'Conflict'
|
73
|
+
else
|
74
|
+
'Object Invalid'
|
72
75
|
end
|
73
76
|
end
|
74
77
|
response_code
|
75
78
|
end
|
76
79
|
|
77
80
|
def delete
|
78
|
-
response_code = nil
|
79
81
|
if self.id
|
80
|
-
BaseObject.
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
'Conflict'
|
90
|
-
end
|
82
|
+
response = BaseObject.rest_request( "#{self.class.api_url}/#{id}", :delete )
|
83
|
+
response_code = case response.code
|
84
|
+
when 200
|
85
|
+
'200 OK'
|
86
|
+
when 409
|
87
|
+
@errors=response.body
|
88
|
+
'Conflict'
|
89
|
+
else
|
90
|
+
'unknown'
|
91
91
|
end
|
92
92
|
else
|
93
93
|
response_code = "Object Invalid"
|
@@ -95,6 +95,54 @@ module LivePaper
|
|
95
95
|
response_code
|
96
96
|
end
|
97
97
|
|
98
|
+
def self.rest_request(url, method, options={})
|
99
|
+
tries = 0
|
100
|
+
verb = (method||"get").downcase.to_sym
|
101
|
+
raise "Method '#{verb}' not supported." unless [:get, :post, :put, :delete].include?(verb)
|
102
|
+
|
103
|
+
request_access_token unless $lpp_access_token
|
104
|
+
headers = {}
|
105
|
+
headers[:authorization] = "Bearer #{$lpp_access_token}"
|
106
|
+
headers[:accept] = options[:accept] || "application/json"
|
107
|
+
headers[:x_user_info] = 'app=rubygem' unless options[:skip] == :user_info #watermark download fails if set
|
108
|
+
headers[:content_type] = 'application/json' unless options[:body].nil?
|
109
|
+
|
110
|
+
h = {:method => verb, :url => url.to_s, :headers => headers}
|
111
|
+
h.merge!({:payload => options[:body]}) unless options[:body].nil?
|
112
|
+
|
113
|
+
begin
|
114
|
+
response = RestClient::Request.execute(h) { |response, request, result| response }
|
115
|
+
raise NotAuthenticatedError if response.code == 401
|
116
|
+
rescue NotAuthenticatedError => e
|
117
|
+
tries += 1
|
118
|
+
if tries < 3
|
119
|
+
request_access_token
|
120
|
+
headers[:authorization] = "Bearer #{$lpp_access_token}"
|
121
|
+
retry
|
122
|
+
else
|
123
|
+
raise e
|
124
|
+
end
|
125
|
+
end
|
126
|
+
response
|
127
|
+
end
|
128
|
+
|
129
|
+
def self.request_access_token
|
130
|
+
h = { method: :post,
|
131
|
+
url: AUTH_URL,
|
132
|
+
headers: { authorization: "Basic #{$lpp_basic_auth}",
|
133
|
+
content_type: 'application/x-www-form-urlencoded',
|
134
|
+
accept: 'application/json' },
|
135
|
+
payload: 'grant_type=client_credentials&scope=all'
|
136
|
+
}
|
137
|
+
|
138
|
+
response = RestClient::Request.execute(h) { |response, request, result| response }
|
139
|
+
|
140
|
+
parsed = JSON.parse(response.body)
|
141
|
+
@access_token = parsed['accessToken']
|
142
|
+
$lpp_access_token = @access_token
|
143
|
+
end
|
144
|
+
|
145
|
+
|
98
146
|
def errors
|
99
147
|
begin
|
100
148
|
JSON.parse(@errors)
|
data/lib/live_paper/image.rb
CHANGED
@@ -3,7 +3,6 @@ require 'rest-client'
|
|
3
3
|
|
4
4
|
module LivePaper
|
5
5
|
class Image
|
6
|
-
extend HttpClient
|
7
6
|
|
8
7
|
attr_accessor :url
|
9
8
|
|
@@ -16,14 +15,15 @@ module LivePaper
|
|
16
15
|
end
|
17
16
|
begin
|
18
17
|
src_image = RestClient.get(img, Accept: 'image/jpg')
|
19
|
-
request_access_token unless
|
18
|
+
BaseObject.request_access_token unless $lpp_access_token
|
20
19
|
response = RestClient.post API_URL,
|
21
20
|
src_image.body,
|
22
|
-
|
21
|
+
authorization: "Bearer #{$lpp_access_token}",
|
23
22
|
content_type: 'image/jpg'
|
24
23
|
response.headers[:location]
|
25
24
|
rescue Exception => e
|
26
25
|
puts "Exception! ******\n#{e}"
|
26
|
+
puts e.response
|
27
27
|
img
|
28
28
|
end
|
29
29
|
end
|
data/lib/live_paper/link.rb
CHANGED
@@ -14,11 +14,8 @@ module LivePaper
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def download_qrcode
|
17
|
-
QrTrigger.
|
18
|
-
|
19
|
-
response = QrTrigger.send_request(request)
|
20
|
-
response.body.empty? ? nil : response.body
|
21
|
-
end
|
17
|
+
response = QrTrigger.rest_request( self.qrcode_url, :get, accept: "image/jpg" )
|
18
|
+
response.body.empty? ? nil : response.body
|
22
19
|
end
|
23
20
|
|
24
21
|
private
|
data/lib/live_paper/version.rb
CHANGED
@@ -15,11 +15,11 @@ module LivePaper
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def download_watermark
|
18
|
-
WmTrigger.
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
response = WmTrigger.rest_request( self.wm_url, :get, accept: "image/jpg", skip: :x_user_info )
|
19
|
+
response.body.empty? ? nil : response.body
|
20
|
+
rescue Exception => e
|
21
|
+
puts 'Exception!\n'
|
22
|
+
puts e.response
|
23
23
|
end
|
24
24
|
|
25
25
|
private
|
data/lib/live_paper.rb
CHANGED
@@ -159,7 +159,7 @@ describe LivePaper::BaseObject do
|
|
159
159
|
id: obj_id,
|
160
160
|
date_created: 'date_created',
|
161
161
|
date_modified: 'date_modified'} }
|
162
|
-
let(:resp_body) {
|
162
|
+
let(:resp_body) {}
|
163
163
|
|
164
164
|
before do
|
165
165
|
stub_unimplemented_methods
|
@@ -167,21 +167,21 @@ describe LivePaper::BaseObject do
|
|
167
167
|
end
|
168
168
|
|
169
169
|
context 'with valid data' do
|
170
|
-
let(:resp_body) { {
|
171
|
-
|
172
|
-
|
173
|
-
|
170
|
+
let(:resp_body) { {object: {name: new_name,
|
171
|
+
id: obj_id,
|
172
|
+
date_created: 'date_created',
|
173
|
+
date_modified: 'new_date_modified'}} }
|
174
174
|
let(:new_name) { 'my_valid_name_change' }
|
175
|
-
|
176
|
-
|
177
|
-
stub_request(:put, "#{@api_url}/#{obj_id}").to_return(:body =>
|
175
|
+
before do
|
176
|
+
@response = resp_body
|
177
|
+
stub_request(:put, "#{@api_url}/#{obj_id}").to_return(:body => resp_body.to_json, :status => 200)
|
178
178
|
@obj=LivePaper::BaseObject.new data1
|
179
179
|
@obj.name = new_name
|
180
180
|
end
|
181
181
|
it 'should return success' do
|
182
182
|
ret_val = @obj.update
|
183
183
|
assert_requested :put, "#{@api_url}/#{obj_id}"
|
184
|
-
expect(ret_val).to eq 'OK'
|
184
|
+
expect(ret_val).to eq '200 OK'
|
185
185
|
end
|
186
186
|
xit 'should reflect the updated object' do
|
187
187
|
allow(@response).to receive(:body).and_return(@response[:object])
|
@@ -266,7 +266,7 @@ describe LivePaper::BaseObject do
|
|
266
266
|
it 'should DELETE when there is an ID' do
|
267
267
|
result=@obj.delete
|
268
268
|
assert_requested :delete, "#{@api_url}/#{@obj.id}"
|
269
|
-
expect(result).to eq 'OK'
|
269
|
+
expect(result).to eq '200 OK'
|
270
270
|
end
|
271
271
|
end
|
272
272
|
|
@@ -383,4 +383,57 @@ describe LivePaper::BaseObject do
|
|
383
383
|
expect { LivePaper::BaseObject.new.send :create_body }.to raise_error
|
384
384
|
end
|
385
385
|
end
|
386
|
+
|
387
|
+
describe '.request_access_token' do
|
388
|
+
it 'should corerectly get the token' do
|
389
|
+
$lpp_access_token = nil
|
390
|
+
LivePaper::BaseObject.request_access_token
|
391
|
+
expect($lpp_access_token).to eq 'SECRETTOKEN'
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
describe 'rest_request' do
|
396
|
+
before do
|
397
|
+
|
398
|
+
end
|
399
|
+
|
400
|
+
context 'when there is no access token' do
|
401
|
+
before do
|
402
|
+
$lpp_access_token = nil
|
403
|
+
end
|
404
|
+
it 'should request the access token' do
|
405
|
+
expect(LivePaper::BaseObject).to receive(:request_access_token)
|
406
|
+
LivePaper::BaseObject.rest_request(@api_url, :post)
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
context 'when there is an access token' do
|
411
|
+
before do
|
412
|
+
$lpp_access_token = 'TOPSECRET'
|
413
|
+
end
|
414
|
+
|
415
|
+
it 'should NOT call request_access_token' do
|
416
|
+
expect(LivePaper::BaseObject).to receive(:request_access_token).exactly(0).times
|
417
|
+
LivePaper::BaseObject.rest_request(@api_url, :post)
|
418
|
+
end
|
419
|
+
|
420
|
+
context 'when the access token is invalid' do
|
421
|
+
before do
|
422
|
+
$lpp_access_token = 'invalid'
|
423
|
+
|
424
|
+
@response = double('A mock for a response')
|
425
|
+
allow(@response).to receive(:body)
|
426
|
+
@response.stub(:code).and_return(401, 401, 200) #fail first two calls
|
427
|
+
RestClient::Request.stub(:execute).and_return(@response)
|
428
|
+
end
|
429
|
+
|
430
|
+
it 'should request access an token' do
|
431
|
+
expect(LivePaper::BaseObject).to receive(:request_access_token).exactly(2).times
|
432
|
+
LivePaper::BaseObject.rest_request(@api_url, :put, body: @data.to_json)
|
433
|
+
end
|
434
|
+
|
435
|
+
end
|
436
|
+
end
|
437
|
+
|
438
|
+
end
|
386
439
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: live_paper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.20
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Whitmarsh
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-11-
|
12
|
+
date: 2014-11-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rest-client
|
@@ -95,7 +95,6 @@ files:
|
|
95
95
|
- Rakefile
|
96
96
|
- lib/live_paper.rb
|
97
97
|
- lib/live_paper/base_object.rb
|
98
|
-
- lib/live_paper/http_client.rb
|
99
98
|
- lib/live_paper/image.rb
|
100
99
|
- lib/live_paper/link.rb
|
101
100
|
- lib/live_paper/payoff.rb
|
@@ -106,7 +105,6 @@ files:
|
|
106
105
|
- lib/live_paper/wm_trigger.rb
|
107
106
|
- live_paper.gemspec
|
108
107
|
- spec/live_paper/base_object_spec.rb
|
109
|
-
- spec/live_paper/http_client_spec.rb
|
110
108
|
- spec/live_paper/link_spec.rb
|
111
109
|
- spec/live_paper/payoff_spec.rb
|
112
110
|
- spec/live_paper/trigger_spec.rb
|
@@ -139,7 +137,6 @@ specification_version: 4
|
|
139
137
|
summary: Ruby interface to the Live Paper service by HP.
|
140
138
|
test_files:
|
141
139
|
- spec/live_paper/base_object_spec.rb
|
142
|
-
- spec/live_paper/http_client_spec.rb
|
143
140
|
- spec/live_paper/link_spec.rb
|
144
141
|
- spec/live_paper/payoff_spec.rb
|
145
142
|
- spec/live_paper/trigger_spec.rb
|
@@ -1,87 +0,0 @@
|
|
1
|
-
require 'open-uri'
|
2
|
-
require 'net/https'
|
3
|
-
|
4
|
-
class NotAuthenticatedError < Exception
|
5
|
-
end
|
6
|
-
|
7
|
-
module LivePaper
|
8
|
-
module HttpClient
|
9
|
-
LP_API_HOST="https://www.livepaperapi.com"
|
10
|
-
AUTH_URL = "#{LP_API_HOST}/auth/token"
|
11
|
-
|
12
|
-
def send_request(request, options={})
|
13
|
-
request['Content-type'] = options[:content_type] if options[:content_type]
|
14
|
-
request.body = options[:body] if options[:body]
|
15
|
-
options[:allow_codes] ||= [200,201]
|
16
|
-
response = @http.request(request)
|
17
|
-
check_response(response, options[:allow_codes])
|
18
|
-
response
|
19
|
-
end
|
20
|
-
|
21
|
-
def check_response(response, allow_codes)
|
22
|
-
status = response.code.to_i
|
23
|
-
raise NotAuthenticatedError.new("Unauthorized") if status == 401
|
24
|
-
unless allow_codes.include?(status)
|
25
|
-
raise "Request failed with code #{status}"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def request_handling_auth(url, method)
|
30
|
-
tries = 0
|
31
|
-
begin
|
32
|
-
request_access_token unless @access_token
|
33
|
-
request = http_request(url, method)
|
34
|
-
request['Authorization'] = "Bearer #{@access_token}"
|
35
|
-
request['X-user-info'] = 'app=rubygem'
|
36
|
-
request['Accept'] = "application/json"
|
37
|
-
yield request
|
38
|
-
rescue NotAuthenticatedError => e
|
39
|
-
tries += 1
|
40
|
-
if tries < 3
|
41
|
-
@access_token = nil
|
42
|
-
retry
|
43
|
-
else
|
44
|
-
raise e
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def request_access_token
|
50
|
-
request = http_request(AUTH_URL, 'POST')
|
51
|
-
request['Authorization'] = "Basic #{$lpp_basic_auth}"
|
52
|
-
request['Content-Type'] = 'application/x-www-form-urlencoded'
|
53
|
-
request.body = 'grant_type=client_credentials&scope=all'
|
54
|
-
response = @http.request(request)
|
55
|
-
parsed = JSON.parse(response.body)
|
56
|
-
@access_token = parsed['accessToken']
|
57
|
-
end
|
58
|
-
|
59
|
-
def http_request(url, method)
|
60
|
-
uri = URI.parse(url)
|
61
|
-
set_http uri
|
62
|
-
|
63
|
-
case method.to_s.upcase
|
64
|
-
when 'POST'
|
65
|
-
Net::HTTP::Post.new(uri.request_uri)
|
66
|
-
when 'GET'
|
67
|
-
Net::HTTP::Get.new(uri.request_uri)
|
68
|
-
when 'PUT'
|
69
|
-
Net::HTTP::Put.new(uri.request_uri)
|
70
|
-
when 'DELETE'
|
71
|
-
Net::HTTP::Delete.new(uri.request_uri)
|
72
|
-
else
|
73
|
-
raise "Method '#{method}' not supported."
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
private
|
78
|
-
def set_http(uri)
|
79
|
-
http_params = [uri.host, uri.port]
|
80
|
-
http_params.concat ENV['HTTP_PROXY'].gsub('http://', '').split(':') unless ENV['HTTP_PROXY'].to_s.empty?
|
81
|
-
|
82
|
-
@http = Net::HTTP.new(*http_params)
|
83
|
-
@http.use_ssl = true
|
84
|
-
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
@@ -1,196 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module LivePaper
|
4
|
-
class DummyHTTP
|
5
|
-
include HttpClient
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
describe LivePaper::HttpClient do
|
10
|
-
before do
|
11
|
-
$lpp_basic_auth=Base64.strict_encode64("auth:pass")
|
12
|
-
@http_client = LivePaper::DummyHTTP.new
|
13
|
-
stub_request(:post, 'https://auth:pass@www.livepaperapi.com/auth/token').to_return(:body => lpp_auth_response_json, :status => 200)
|
14
|
-
end
|
15
|
-
|
16
|
-
describe '#send_request' do
|
17
|
-
|
18
|
-
before(:each) do
|
19
|
-
@http = double('Http mock')
|
20
|
-
allow(@http).to receive(:request)
|
21
|
-
@http_client.instance_variable_set(:@http, @http)
|
22
|
-
@request = double('Request mock')
|
23
|
-
allow(@http_client).to receive(:check_response)
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'should add the content type to the request' do
|
27
|
-
expect(@request).to receive(:[]=).with('Content-type', 'image/jpg')
|
28
|
-
allow(@request).to receive(:body=)
|
29
|
-
@http_client.send_request @request, content_type: 'image/jpg', body: 'body'
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should add the body to the request' do
|
33
|
-
expect(@request).to receive(:body=).with('body')
|
34
|
-
allow(@request).to receive(:[]=)
|
35
|
-
@http_client.send(:send_request, @request, content_type: 'image/jpg', body: 'body')
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'should call the request method from the http instance' do
|
39
|
-
allow(@request).to receive(:body=)
|
40
|
-
allow(@request).to receive(:[]=)
|
41
|
-
expect(@http).to receive(:request)
|
42
|
-
@http_client.send(:send_request, @request, content_type: 'image/jpg', body: 'body')
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'should check the response with default allow_codes' do
|
46
|
-
allow(@request).to receive(:body=)
|
47
|
-
allow(@request).to receive(:[]=)
|
48
|
-
expect(@http_client).to receive(:check_response).with(anything, [200, 201])
|
49
|
-
@http_client.send(:send_request, @request, content_type: 'image/jpg', body: 'body')
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'should call check_response with the provided allow_codes' do
|
53
|
-
allow(@request).to receive(:body=)
|
54
|
-
allow(@request).to receive(:[]=)
|
55
|
-
allow_codes = [200, 201, 409]
|
56
|
-
expect(@http_client).to receive(:check_response).with(anything, allow_codes)
|
57
|
-
@http_client.send(:send_request, @request, content_type: 'image/jpg', body: 'body', allow_codes: allow_codes)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
describe 'check_response' do
|
62
|
-
|
63
|
-
before(:each) do
|
64
|
-
@response = double('A mock for a response')
|
65
|
-
allow(@response).to receive(:body)
|
66
|
-
@allow=[200, 201]
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'should raise NotAuthenticatedError if the response code is 401' do
|
70
|
-
allow(@response).to receive(:code).and_return('401')
|
71
|
-
expect { @http_client.send(:check_response, @response, @allow) }.to raise_error NotAuthenticatedError
|
72
|
-
end
|
73
|
-
|
74
|
-
it 'should not raise any exception if the response code is 200..201' do
|
75
|
-
allow(@response).to receive(:code).and_return('201')
|
76
|
-
expect { @http_client.send(:check_response, @response, @allow) }.to_not raise_error
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'should raise exception if the response code is other than 200..201|401' do
|
80
|
-
allow(@response).to receive(:code).and_return('500')
|
81
|
-
expect { @http_client.send(:check_response, @response, @allow) }.to raise_error
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
describe 'request_handling_auth' do
|
86
|
-
|
87
|
-
before(:each) do
|
88
|
-
@url = 'https://dev.livepaperapi.com/auth/token'
|
89
|
-
end
|
90
|
-
|
91
|
-
context 'when there is no access token' do
|
92
|
-
|
93
|
-
before(:each) do
|
94
|
-
@http_client.instance_variable_set(:@access_token, nil)
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'should request the access token' do
|
98
|
-
expect(@http_client).to receive(:request_access_token)
|
99
|
-
@http_client.send(:request_handling_auth, @url, "POST") { |request|}
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
context 'when there is access token' do
|
104
|
-
|
105
|
-
before(:each) do
|
106
|
-
@http_client.instance_variable_set(:@access_token, 'TOPSECRET')
|
107
|
-
end
|
108
|
-
|
109
|
-
it 'should yield the given block' do
|
110
|
-
a_mock = double('Some mock')
|
111
|
-
expect(a_mock).to receive(:a_message)
|
112
|
-
@http_client.send(:request_handling_auth, @url, 'POST') do |request|
|
113
|
-
a_mock.a_message
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
context 'when the access token is invalid' do
|
118
|
-
|
119
|
-
before(:each) do
|
120
|
-
@http_client.instance_variable_set(:@access_token, 'INVALID')
|
121
|
-
end
|
122
|
-
|
123
|
-
it 'should retry max 3 times' do
|
124
|
-
a_mock = double('Some mock')
|
125
|
-
expect(a_mock).to receive(:a_message).at_least(:twice)
|
126
|
-
begin
|
127
|
-
@http_client.send(:request_handling_auth, @url, 'POST') do |request|
|
128
|
-
a_mock.a_message
|
129
|
-
raise NotAuthenticatedError.new
|
130
|
-
end
|
131
|
-
rescue NotAuthenticatedError => e
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
it 'should raise exception if retried more than 3 times' do
|
136
|
-
allow(@http_client).to receive (:request_access_token)
|
137
|
-
expect {
|
138
|
-
@http_client.send(:request_handling_auth, @url, 'POST') do |request|
|
139
|
-
raise NotAuthenticatedError.new
|
140
|
-
end
|
141
|
-
}.to raise_error
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
describe 'request_access_token' do
|
148
|
-
|
149
|
-
it 'should parse the response getting the accessToken entry' do
|
150
|
-
@http_client.send :request_access_token
|
151
|
-
expect(@http_client.instance_variable_get(:@access_token)).to eq 'SECRETTOKEN'
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
describe 'http_request' do
|
156
|
-
|
157
|
-
before do
|
158
|
-
@host = 'https://dev.livepaperapi.com/auth/token'
|
159
|
-
@http = double('Http mock')
|
160
|
-
allow(@http).to receive(:verify_mode=)
|
161
|
-
allow(@http).to receive(:use_ssl=)
|
162
|
-
end
|
163
|
-
it 'should create and return a Net::HTTP::Post instance if POST method is chosen.' do
|
164
|
-
expect(Net::HTTP::Post).to receive(:new).and_call_original
|
165
|
-
@http_client.send(:http_request, @host, 'POST')
|
166
|
-
end
|
167
|
-
|
168
|
-
it 'should create and return a Net::HTTP::Get instance if GET method is chosen.' do
|
169
|
-
expect(Net::HTTP::Get).to receive(:new).and_call_original
|
170
|
-
@http_client.send(:http_request, @host, 'GET')
|
171
|
-
end
|
172
|
-
|
173
|
-
it 'should create and return a Net::HTTP::Delete instance if DELETE method is chosen.' do
|
174
|
-
expect(Net::HTTP::Delete).to receive(:new).and_call_original
|
175
|
-
@http_client.send(:http_request, @host, 'DELETE')
|
176
|
-
end
|
177
|
-
|
178
|
-
it 'should use ssl' do
|
179
|
-
allow(Net::HTTP).to receive(:new).and_return(@http)
|
180
|
-
expect(@http).to receive(:use_ssl=)
|
181
|
-
@http_client.send(:http_request, @host, 'POST')
|
182
|
-
end
|
183
|
-
|
184
|
-
it 'should handle proxy settings' do
|
185
|
-
ENV['HTTP_PROXY'] = 'http://proxy.com:1234'
|
186
|
-
expect(Net::HTTP).to receive(:new).with('dev.livepaperapi.com', 443, 'proxy.com', '1234').and_return(@http)
|
187
|
-
@http_client.send(:http_request, @host, 'POST')
|
188
|
-
end
|
189
|
-
|
190
|
-
it 'should raise exception if the method provided is not supported.' do
|
191
|
-
expect { @http_client.send(:http_request, @host, 'NOT_SUPPORTED') }.to raise_error
|
192
|
-
end
|
193
|
-
|
194
|
-
end
|
195
|
-
|
196
|
-
end
|