live_paper 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ require_relative 'base_object'
2
+
3
+ module LivePaper
4
+ class QrTrigger < BaseObject
5
+ attr_accessor :subscription, :qrcode_url
6
+
7
+ DEFAULT_SUBSCRIPTION = :month
8
+
9
+ def parse(data)
10
+ data = JSON.parse(data, symbolize_names: true)[:trigger]
11
+ assign_attributes data
12
+ self.qrcode_url=data[:link].select { |item| item[:rel] == "image" }.first[:href]
13
+ self
14
+ end
15
+
16
+ def self.api_url
17
+ "#{LP_API_HOST}/api/v1/triggers"
18
+ end
19
+
20
+ def download_qrcode
21
+ QrTrigger.request_handling_auth(self.qrcode_url, 'GET') do |request|
22
+ response = QrTrigger.send_request(request)
23
+ response.body.empty? ? nil : response.body
24
+ end
25
+ end
26
+
27
+ private
28
+ def validate_attributes!
29
+ raise ArgumentError, 'Required Attributes needed: name' unless all_present? [@name]
30
+ end
31
+
32
+ def create_body
33
+ {
34
+ trigger: {
35
+ name: @name,
36
+ type: "qrcode",
37
+ subscription: {
38
+ package: DEFAULT_SUBSCRIPTION.to_s
39
+ }
40
+ }
41
+ }
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,37 @@
1
+ require_relative 'base_object'
2
+
3
+ module LivePaper
4
+ class ShortTrigger < BaseObject
5
+ attr_accessor :subscription, :short_url
6
+
7
+ DEFAULT_SUBSCRIPTION = :month
8
+
9
+ def parse(data)
10
+ data = JSON.parse(data, symbolize_names: true)[:trigger]
11
+ assign_attributes data
12
+ self.short_url=data[:link].select { |item| item[:rel] == "shortURL" }.first[:href]
13
+ self
14
+ end
15
+
16
+ def self.api_url
17
+ "#{LP_API_HOST}/api/v1/triggers"
18
+ end
19
+
20
+ private
21
+ def validate_attributes!
22
+ raise ArgumentError, 'Required Attributes needed: name' unless all_present? [@name]
23
+ end
24
+
25
+ def create_body
26
+ {
27
+ trigger: {
28
+ name: @name,
29
+ type: "shorturl",
30
+ subscription: {
31
+ package: DEFAULT_SUBSCRIPTION.to_s
32
+ }
33
+ }
34
+ }
35
+ end
36
+ end
37
+ end
@@ -1,3 +1,3 @@
1
1
  module LivePaper
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -0,0 +1,52 @@
1
+ require_relative 'base_object'
2
+
3
+ module LivePaper
4
+ class WmTrigger < BaseObject
5
+ attr_accessor :watermark, :subscription, :wm_url
6
+ WATERMARK_RESOLUTION = 75
7
+ WATERMARK_STRENGTH = 10
8
+ DEFAULT_SUBSCRIPTION = :month
9
+
10
+ def parse(data)
11
+ data = JSON.parse(data, symbolize_names: true)[:trigger]
12
+ assign_attributes data
13
+ self.wm_url=data[:link].select { |item| item[:rel] == "image" }.first[:href]
14
+ self
15
+ end
16
+
17
+ def self.api_url
18
+ "#{LP_API_HOST}/api/v1/triggers"
19
+ end
20
+
21
+ def download_watermark
22
+ WmTrigger.request_handling_auth(self.wm_url, 'GET') do |request|
23
+ response = WmTrigger.send_request(request)
24
+ response.body.empty? ? nil : response.body
25
+ end
26
+ end
27
+
28
+ private
29
+ def validate_attributes!
30
+ raise ArgumentError, 'Required Attributes needed: name and watermark.' unless all_present? [@name, @watermark]
31
+ raise ArgumentError, 'Required Attributes needed: watermark[:strength] and watermark[:imageURL].' unless all_keys_present? @watermark, [:strength, :imageURL]
32
+ end
33
+
34
+ def create_body
35
+ {
36
+ trigger: {
37
+ name: @name,
38
+ watermark: {
39
+ outputImageFormat: 'JPEG',
40
+ resolution: WATERMARK_RESOLUTION,
41
+ strength: @watermark[:strength],
42
+ imageURL: @watermark[:imageURL]
43
+ },
44
+ subscription: {
45
+ package: DEFAULT_SUBSCRIPTION.to_s
46
+ }
47
+ }
48
+ }
49
+ end
50
+
51
+ end
52
+ end
data/live_paper.gemspec CHANGED
@@ -19,7 +19,9 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_runtime_dependency "rest-client"
22
-
22
+
23
23
  spec.add_development_dependency "bundler", "~> 1.6"
24
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "webmock"
25
27
  end
@@ -0,0 +1,200 @@
1
+ require 'spec_helper'
2
+
3
+ def stub_unimplemented_methods
4
+ allow_any_instance_of(LivePaper::BaseObject).to receive(:validate_attributes!)
5
+ allow_any_instance_of(LivePaper::BaseObject).to receive(:create_body).and_return(@data)
6
+ allow_any_instance_of(LivePaper::BaseObject).to receive(:parse) { |data| data }
7
+ allow(LivePaper::BaseObject).to receive(:api_url).and_return(@api_url)
8
+ end
9
+
10
+ describe LivePaper::BaseObject do
11
+ before do
12
+ @api_url = "#{LivePaper::LP_API_HOST}/objects"
13
+ stub_request(:post, /.*livepaperapi.com\/auth\/token.*/).to_return(:body => lpp_auth_response_json, :status => 200)
14
+ stub_request(:post, @api_url).to_return(:body => lpp_richpayoff_response_json, :status => 200)
15
+
16
+ @data = {
17
+ name: 'name',
18
+ date_created: 'date_created',
19
+ date_modified: 'date_modified'
20
+ }
21
+
22
+ end
23
+
24
+ describe '#initialize' do
25
+ before do
26
+ @data = {
27
+ id: 'id',
28
+ name: 'name',
29
+ date_created: 'date_created',
30
+ date_modified: 'date_modified'
31
+ }
32
+ @object = LivePaper::BaseObject.new @data
33
+ end
34
+
35
+ it 'should map the id attribute.' do
36
+ expect(@object.id).to eq @data[:id]
37
+ end
38
+
39
+ it 'should map the name attribute.' do
40
+ expect(@object.name).to eq @data[:name]
41
+ end
42
+
43
+ it 'should map the date_created attribute.' do
44
+ expect(@object.date_created).to eq @data[:date_created]
45
+ end
46
+
47
+ it 'should map the date_modified attribute.' do
48
+ expect(@object.date_modified).to eq @data[:date_modified]
49
+ end
50
+ end
51
+
52
+ describe '#save' do
53
+ before do
54
+ stub_unimplemented_methods
55
+ @data = {
56
+ name: 'name',
57
+ date_created: 'date_created',
58
+ date_modified: 'date_modified'
59
+ }
60
+ @obj = LivePaper::BaseObject.new @data
61
+ end
62
+
63
+ it 'should not make a POST if the object has already an id.' do
64
+ @obj.id = 'id'
65
+ @obj.save
66
+ assert_not_requested :post, @api_url
67
+ end
68
+
69
+ it 'should re-assign the current object attributes.' do
70
+ @obj.save
71
+ expect(@obj.name).to eq @data[:name]
72
+ expect(@obj.date_created).to eq @data[:date_created]
73
+ expect(@obj.date_modified).to eq @data[:date_modified]
74
+ end
75
+
76
+ it 'should return the object instance.' do
77
+ obj = @obj.save
78
+ expect(obj).to eq @obj
79
+ end
80
+
81
+ it 'should make a POST to the api_url with the body provided.' do
82
+ @obj.save
83
+ assert_requested :post, @api_url, :body => @data.to_json
84
+ end
85
+ end
86
+
87
+ describe '.create' do
88
+ before do
89
+ stub_unimplemented_methods
90
+ @obj = LivePaper::BaseObject.create @data
91
+ end
92
+
93
+ it 'should return a object instance.' do
94
+ expect(@obj.class).to eq LivePaper::BaseObject
95
+ end
96
+
97
+ it 'should return the object instance with the provided + updated data.' do
98
+ expect(@obj.name).to eq @data[:name]
99
+ expect(@obj.date_created).to eq @data[:date_created]
100
+ expect(@obj.date_modified).to eq @data[:date_modified]
101
+ end
102
+ end
103
+
104
+ describe '.find' do
105
+ before do
106
+ allow(LivePaper::BaseObject).to receive(:api_url).and_return(@api_url)
107
+ @data = '"id": "id", "name": "name"'
108
+ stub_request(:get, "#{@api_url}/base_object").to_return(:body => @data, :status => 200)
109
+ stub_request(:get, "#{@api_url}/base_object_not_existent").to_return(:body => '{}', :status => 404)
110
+ end
111
+ context 'the requested base_object exists.' do
112
+ it 'should return the requested base object.' do
113
+ allow(@data).to receive(:body).and_return(@data)
114
+ expect(LivePaper::BaseObject).to receive(:parse).with(@data)
115
+ LivePaper::BaseObject.find('base_object')
116
+ end
117
+ end
118
+
119
+ context 'the requested base object does not exist or some error happened.' do
120
+ it 'should not raise error.' do
121
+ expect { LivePaper::BaseObject.find('base_object_not_existent') }.to_not raise_error
122
+ end
123
+
124
+ it 'should return nil.' do
125
+ expect(LivePaper::BaseObject.find('base_object_not_existent')).to eq nil
126
+ end
127
+ end
128
+ end
129
+
130
+ describe '#all_present?' do
131
+ before do
132
+ @all = [1, 2, {k: 'v'}, [1, 2]]
133
+ @some = [1, 2, {k: 'v'}, []]
134
+ @none = nil
135
+ end
136
+
137
+ it 'should return true if all elements are present.' do
138
+ expect(LivePaper::BaseObject.new.send(:all_present?, @all)).to eq true
139
+ end
140
+
141
+ it 'should return false if some element is not present.' do
142
+ expect(LivePaper::BaseObject.new.send(:all_present?, @some)).to eq false
143
+ end
144
+
145
+ it 'should return false if there is no array.' do
146
+ expect(LivePaper::BaseObject.new.send(:all_present?, @none)).to eq false
147
+ end
148
+ end
149
+
150
+ describe '#all_keys_present?' do
151
+ before do
152
+ @keys = [:x, :y, :z]
153
+ @all = {x: 10, y: 20, z: 30}
154
+ @some = {x: 10, y: 20}
155
+ @none = nil
156
+ end
157
+
158
+ it 'should return true if all keys are present.' do
159
+ expect(LivePaper::BaseObject.new.send(:all_keys_present?, @all, @keys)).to eq true
160
+ end
161
+
162
+ it 'should return false if some key is not present.' do
163
+ expect(LivePaper::BaseObject.new.send(:all_keys_present?, @some, @keys)).to eq false
164
+ end
165
+
166
+ it 'should return false if there is no hash.' do
167
+ expect(LivePaper::BaseObject.new.send(:all_keys_present?, @none, @keys)).to eq false
168
+ end
169
+ end
170
+
171
+ describe '#parse' do
172
+ it 'should raise exception' do
173
+ expect { LivePaper::BaseObject.parse('') }.to raise_error
174
+ end
175
+ end
176
+
177
+ describe '#save' do
178
+ it 'should raise exception' do
179
+ expect { LivePaper::BaseObject.new.save }.to raise_error
180
+ end
181
+ end
182
+
183
+ describe '#api_url' do
184
+ it 'should raise exception' do
185
+ expect { LivePaper::BaseObject.api_url }.to raise_error
186
+ end
187
+ end
188
+
189
+ describe '#validate_attributes!' do
190
+ it 'should raise exception' do
191
+ expect { LivePaper::BaseObject.new.send :validate_attributes! }.to raise_error
192
+ end
193
+ end
194
+
195
+ describe '#create_body' do
196
+ it 'should raise exception' do
197
+ expect { LivePaper::BaseObject.new.send :create_body }.to raise_error
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,182 @@
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, 'image/jpg', '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, 'image/jpg', '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, 'image/jpg', 'body')
43
+ end
44
+
45
+ it 'should check the response' do
46
+ allow(@request).to receive(:body=)
47
+ allow(@request).to receive(:[]=)
48
+ expect(@http_client).to receive(:check_response)
49
+ @http_client.send(:send_request, @request, 'image/jpg', 'body')
50
+ end
51
+ end
52
+
53
+ describe 'check_response' do
54
+
55
+ before(:each) do
56
+ @response = double('A mock for a response')
57
+ allow(@response).to receive(:body)
58
+ end
59
+
60
+ it 'should raise NotAuthenticatedError if the response code is 401' do
61
+ allow(@response).to receive(:code).and_return('401')
62
+ expect { @http_client.send(:check_response, @response) }.to raise_error NotAuthenticatedError
63
+ end
64
+
65
+ it 'should not raise any exception if the response code is 200..201' do
66
+ allow(@response).to receive(:code).and_return('201')
67
+ expect { @http_client.send(:check_response, @response) }.to_not raise_error
68
+ end
69
+
70
+ it 'should raise exception if the response code is other than 200..201|401' do
71
+ allow(@response).to receive(:code).and_return('500')
72
+ expect { @http_client.send(:check_response, @response) }.to raise_error
73
+ end
74
+ end
75
+
76
+ describe 'request_handling_auth' do
77
+
78
+ before(:each) do
79
+ @url = 'https://dev.livepaperapi.com/auth/token'
80
+ end
81
+
82
+ context 'when there is no access token' do
83
+
84
+ before(:each) do
85
+ @http_client.instance_variable_set(:@access_token, nil)
86
+ end
87
+
88
+ it 'should request the access token' do
89
+ expect(@http_client).to receive(:request_access_token)
90
+ @http_client.send(:request_handling_auth, @url, "POST") { |request|}
91
+ end
92
+ end
93
+
94
+ context 'when there is access token' do
95
+
96
+ before(:each) do
97
+ @http_client.instance_variable_set(:@access_token, 'TOPSECRET')
98
+ end
99
+
100
+ it 'should yield the given block' do
101
+ a_mock = double('Some mock')
102
+ expect(a_mock).to receive(:a_message)
103
+ @http_client.send(:request_handling_auth, @url, 'POST') do |request|
104
+ a_mock.a_message
105
+ end
106
+ end
107
+
108
+ context 'when the access token is invalid' do
109
+
110
+ before(:each) do
111
+ @http_client.instance_variable_set(:@access_token, 'INVALID')
112
+ end
113
+
114
+ it 'should retry max 3 times' do
115
+ a_mock = double('Some mock')
116
+ expect(a_mock).to receive(:a_message).at_least(:twice)
117
+ begin
118
+ @http_client.send(:request_handling_auth, @url, 'POST') do |request|
119
+ a_mock.a_message
120
+ raise NotAuthenticatedError.new
121
+ end
122
+ rescue NotAuthenticatedError => e
123
+ end
124
+ end
125
+
126
+ it 'should raise exception if retried more than 3 times' do
127
+ allow(@http_client).to receive (:request_access_token)
128
+ expect {
129
+ @http_client.send(:request_handling_auth, @url, 'POST') do |request|
130
+ raise NotAuthenticatedError.new
131
+ end
132
+ }.to raise_error
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ describe 'request_access_token' do
139
+
140
+ it 'should parse the response getting the accessToken entry' do
141
+ @http_client.send :request_access_token
142
+ expect(@http_client.instance_variable_get(:@access_token)).to eq 'SECRETTOKEN'
143
+ end
144
+ end
145
+
146
+ describe 'http_request' do
147
+
148
+ before do
149
+ @host = 'https://dev.livepaperapi.com/auth/token'
150
+ @http = double('Http mock')
151
+ allow(@http).to receive(:verify_mode=)
152
+ allow(@http).to receive(:use_ssl=)
153
+ end
154
+ it 'should create and return a Net::HTTP::Post instance if POST method is chosen.' do
155
+ expect(Net::HTTP::Post).to receive(:new).and_call_original
156
+ @http_client.send(:http_request, @host, 'POST')
157
+ end
158
+
159
+ it 'should create and return a Net::HTTP::Get instance if GET method is chosen.' do
160
+ expect(Net::HTTP::Get).to receive(:new).and_call_original
161
+ @http_client.send(:http_request, @host, 'GET')
162
+ end
163
+
164
+ it 'should use ssl' do
165
+ allow(Net::HTTP).to receive(:new).and_return(@http)
166
+ expect(@http).to receive(:use_ssl=)
167
+ @http_client.send(:http_request, @host, 'POST')
168
+ end
169
+
170
+ it 'should handle proxy settings' do
171
+ ENV['HTTP_PROXY'] = 'http://proxy.com:1234'
172
+ expect(Net::HTTP).to receive(:new).with('dev.livepaperapi.com', 443, 'proxy.com', '1234').and_return(@http)
173
+ @http_client.send(:http_request, @host, 'POST')
174
+ end
175
+
176
+ it 'should raise exception if the method provided is not supported.' do
177
+ expect { @http_client.send(:http_request, @host, 'NOT_SUPPORTED') }.to raise_error
178
+ end
179
+
180
+ end
181
+
182
+ end