live_paper 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54159788ca4d35d52a44ed05de7e2fdfad88998e
4
- data.tar.gz: 4f319ab3b4ad6f7380828d0bc68b06ca1979433a
3
+ metadata.gz: 150824532bc144af1c7efd5005578e51d843a966
4
+ data.tar.gz: 68b48ce5fdc74376878078de4c9c16c3fc221023
5
5
  SHA512:
6
- metadata.gz: a8a4256e05ff2fbf38165b9ed6098673d3602b3a387de482cc4a7f911a82961f148bf118867d7bff796f12a0ed88a3e938a30741a0edd1dcac3a4e5436ae3a33
7
- data.tar.gz: c9416a9a5d40f8eabb7d3897b775c390a56346cb2b629e92616a364d2af098d4a124778fef1b6dde36c0868741d059885e6e80bce8597c0a696c0a8630c296a4
6
+ metadata.gz: 65f7056b9a7d6d9490051fb4e12d39808e6806948783224b05fec4888a7c439933545e1630cd9588a2edfb4421d94563da8bd6c7f1c3296ced411c383fe6a947
7
+ data.tar.gz: 3f53715224edf9e668d23d3988f7678e45fcd22a982a052e1557399be7c8ab4d2a505ba7baaa653e0112fab52d5bf60308b05463ff166c401a980fe942f1dd74
data/lib/live_paper.rb CHANGED
@@ -23,6 +23,11 @@ module LivePaper
23
23
  def initialize(auth)
24
24
  #todo: tdd, verify hash
25
25
  $lpp_basic_auth = Base64.strict_encode64("#{auth[:id]}:#{auth[:secret]}")
26
+ @remote_resources={}
27
+ end
28
+
29
+ def resources
30
+ @remote_resources
26
31
  end
27
32
 
28
33
  def smart_link(dest, image=nil)
@@ -44,14 +49,16 @@ module LivePaper
44
49
  def shorten(dest)
45
50
  t=ShortTrigger.create(name: 'short trigger')
46
51
  p=Payoff.create(name: 'name', type: Payoff::TYPE[:WEB], url: dest)
47
- Link.create(payoff_id: p.id, trigger_id: t.id, name: "link")
52
+ l=Link.create(payoff_id: p.id, trigger_id: t.id, name: "link")
53
+ @remote_resources={link: l, payoff: p, trigger: t}
48
54
  t.short_url
49
55
  end
50
56
 
51
57
  def qr_bytes(dest)
52
58
  t=QrTrigger.create(name: 'QR code trigger')
53
59
  p=Payoff.create(name: 'name', type: Payoff::TYPE[:WEB], url: dest)
54
- Link.create(payoff_id: p.id, trigger_id: t.id, name: "link")
60
+ l=Link.create(payoff_id: p.id, trigger_id: t.id, name: "link")
61
+ @remote_resources={link: l, payoff: p, trigger: t}
55
62
  t.download_qrcode
56
63
  end
57
64
 
@@ -60,7 +67,8 @@ module LivePaper
60
67
 
61
68
  t=WmTrigger.create(name: 'watermark', watermark: {strength: 10, resolution: 75, imageURL: image})
62
69
  p=Payoff.create(name: 'name', type: Payoff::TYPE[:WEB], url: dest)
63
- Link.create(payoff_id: p.id, trigger_id: t.id, name: "link")
70
+ l=Link.create(payoff_id: p.id, trigger_id: t.id, name: "link")
71
+ @remote_resources={link: l, payoff: p, trigger: t}
64
72
  t.download_watermark
65
73
  rescue Exception => e
66
74
  puts "Exception!"
@@ -24,7 +24,7 @@ module LivePaper
24
24
  def save
25
25
  validate_attributes!
26
26
  BaseObject.request_handling_auth(self.class.api_url, 'POST') do |request|
27
- response = BaseObject.send_request(request, 'application/json', create_body.to_json)
27
+ response = BaseObject.send_request(request, content_type: 'application/json', body: create_body.to_json)
28
28
  parse(response.body)
29
29
  end unless present? @id
30
30
  self
@@ -32,11 +32,38 @@ module LivePaper
32
32
 
33
33
  def self.get(id)
34
34
  request_handling_auth("#{api_url}/#{id}", 'GET') do |request|
35
- response = send_request(request, 'application/json')
35
+ response = send_request(request, content_type: 'application/json')
36
36
  parse response.body
37
37
  end rescue nil
38
38
  end
39
39
 
40
+ def delete
41
+ response_code = nil
42
+ if self.id
43
+ BaseObject.request_handling_auth("#{self.class.api_url}/#{id}", 'DELETE') do |request|
44
+ response = BaseObject.send_request(request, content_type: 'application/json', allow_codes: [200, 409])
45
+ response_code = case response.code.to_i
46
+ when 200
47
+ 'OK'
48
+ when 409
49
+ @errors=response.body
50
+ 'Conflict'
51
+ end
52
+ end
53
+ else
54
+ response_code = "Object Invalid"
55
+ end
56
+ response_code
57
+ end
58
+
59
+ def errors
60
+ begin
61
+ JSON.parse(@errors)
62
+ rescue
63
+ @errors
64
+ end
65
+ end
66
+
40
67
  def self.parse(data)
41
68
  self.new().parse(data)
42
69
  end
@@ -9,18 +9,19 @@ module LivePaper
9
9
  LP_API_HOST="https://www.livepaperapi.com"
10
10
  AUTH_URL = "#{LP_API_HOST}/auth/token"
11
11
 
12
- def send_request(request, content_type = nil, body = nil)
13
- request['Content-type'] = content_type if content_type
14
- request.body = body if body
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]
15
16
  response = @http.request(request)
16
- check_response(response)
17
+ check_response(response, options[:allow_codes])
17
18
  response
18
19
  end
19
20
 
20
- def check_response(response)
21
+ def check_response(response, allow_codes)
21
22
  status = response.code.to_i
22
23
  raise NotAuthenticatedError.new("Unauthorized") if status == 401
23
- unless Array(200..201).include?(status)
24
+ unless allow_codes.include?(status)
24
25
  raise "Request failed with code #{status}"
25
26
  end
26
27
  end
@@ -62,6 +63,8 @@ module LivePaper
62
63
  Net::HTTP::Post.new(uri.request_uri)
63
64
  when 'GET'
64
65
  Net::HTTP::Get.new(uri.request_uri)
66
+ when 'DELETE'
67
+ Net::HTTP::Delete.new(uri.request_uri)
65
68
  else
66
69
  raise "Method '#{method}' not supported."
67
70
  end
@@ -1,3 +1,3 @@
1
1
  module LivePaper
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
@@ -9,7 +9,7 @@ end
9
9
 
10
10
  describe LivePaper::BaseObject do
11
11
  before do
12
- @api_url = "#{LivePaper::LP_API_HOST}/objects"
12
+ @api_url = "#{LivePaper::LP_API_HOST}/v99/objects"
13
13
  stub_request(:post, /.*livepaperapi.com\/auth\/token.*/).to_return(:body => lpp_auth_response_json, :status => 200)
14
14
  stub_request(:post, @api_url).to_return(:body => lpp_richpayoff_response_json, :status => 200)
15
15
 
@@ -127,19 +127,68 @@ describe LivePaper::BaseObject do
127
127
  end
128
128
  end
129
129
 
130
+ describe '.delete' do
131
+ before do
132
+ stub_unimplemented_methods
133
+ @data = {
134
+ name: 'name',
135
+ id: 'obj_id',
136
+ date_created: 'date_created',
137
+ date_modified: 'date_modified',
138
+ link: [
139
+ { :rel => "self", :href => "/api/v1/objects/obj_id" },
140
+ { :rel => "analytics", :href => "/analytics/v1/objects/obj_id" }
141
+ ]
142
+ }
143
+ @obj = LivePaper::BaseObject.create @data
144
+ @self_link = "#{@api_url}/#{@obj.id}"
145
+ end
146
+
147
+ it 'should not DELETE if the object does not have an id.' do
148
+ @obj.id = nil
149
+ ret_val = @obj.delete
150
+ assert_not_requested :delete, @self_link
151
+ expect(ret_val).to eq 'Object Invalid'
152
+ end
153
+
154
+ context 'successful delete' do
155
+ before do
156
+ stub_request(:delete, @self_link).to_return(:status => 200, :body => "")
157
+ end
158
+ it 'should DELETE when there is an ID' do
159
+ result=@obj.delete
160
+ assert_requested :delete, "#{@api_url}/#{@obj.id}"
161
+ expect(result).to eq 'OK'
162
+ end
163
+ end
164
+
165
+ context 'when link points to object' do
166
+ before do
167
+ @bodee = lpp_delete_error_response
168
+ stub_request(:delete, @self_link).to_return(:status => 409, :body => @bodee)
169
+ end
170
+ it 'should fail' do
171
+ result=@obj.delete
172
+ assert_requested :delete, "#{@api_url}/#{@obj.id}"
173
+ expect(result).to eq 'Conflict'
174
+ expect(@obj.errors).to eq JSON.parse @bodee
175
+ end
176
+ end
177
+ end
178
+
130
179
  describe '#rel' do
131
180
  before do
132
181
  stub_unimplemented_methods
133
- @data = {
134
- name: 'name',
135
- date_created: 'date_created',
136
- date_modified: 'date_modified',
137
- link: [
138
- { :rel => "self", :href => "/api/v1/payoffs/payoff_id" },
139
- { :rel => "analytics", :href => "/analytics/v1/payoffs/payoff_id" }
140
- ]
141
- }
142
- @obj = LivePaper::BaseObject.create @data
182
+ @data = {
183
+ name: 'name',
184
+ date_created: 'date_created',
185
+ date_modified: 'date_modified',
186
+ link: [
187
+ { :rel => "self", :href => "/api/v1/payoffs/payoff_id" },
188
+ { :rel => "analytics", :href => "/analytics/v1/payoffs/payoff_id" }
189
+ ]
190
+ }
191
+ @obj = LivePaper::BaseObject.create @data
143
192
  end
144
193
 
145
194
  it 'should return href for rel link' do
@@ -26,27 +26,35 @@ describe LivePaper::HttpClient do
26
26
  it 'should add the content type to the request' do
27
27
  expect(@request).to receive(:[]=).with('Content-type', 'image/jpg')
28
28
  allow(@request).to receive(:body=)
29
- @http_client.send_request @request, 'image/jpg', 'body'
29
+ @http_client.send_request @request, content_type: 'image/jpg', body: 'body'
30
30
  end
31
31
 
32
32
  it 'should add the body to the request' do
33
33
  expect(@request).to receive(:body=).with('body')
34
34
  allow(@request).to receive(:[]=)
35
- @http_client.send(:send_request, @request, 'image/jpg', 'body')
35
+ @http_client.send(:send_request, @request, content_type: 'image/jpg', body: 'body')
36
36
  end
37
37
 
38
38
  it 'should call the request method from the http instance' do
39
39
  allow(@request).to receive(:body=)
40
40
  allow(@request).to receive(:[]=)
41
41
  expect(@http).to receive(:request)
42
- @http_client.send(:send_request, @request, 'image/jpg', 'body')
42
+ @http_client.send(:send_request, @request, content_type: 'image/jpg', body: 'body')
43
43
  end
44
44
 
45
- it 'should check the response' do
45
+ it 'should check the response with default allow_codes' do
46
46
  allow(@request).to receive(:body=)
47
47
  allow(@request).to receive(:[]=)
48
- expect(@http_client).to receive(:check_response)
49
- @http_client.send(:send_request, @request, 'image/jpg', 'body')
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)
50
58
  end
51
59
  end
52
60
 
@@ -55,21 +63,22 @@ describe LivePaper::HttpClient do
55
63
  before(:each) do
56
64
  @response = double('A mock for a response')
57
65
  allow(@response).to receive(:body)
66
+ @allow=[200, 201]
58
67
  end
59
68
 
60
69
  it 'should raise NotAuthenticatedError if the response code is 401' do
61
70
  allow(@response).to receive(:code).and_return('401')
62
- expect { @http_client.send(:check_response, @response) }.to raise_error NotAuthenticatedError
71
+ expect { @http_client.send(:check_response, @response, @allow) }.to raise_error NotAuthenticatedError
63
72
  end
64
73
 
65
74
  it 'should not raise any exception if the response code is 200..201' do
66
75
  allow(@response).to receive(:code).and_return('201')
67
- expect { @http_client.send(:check_response, @response) }.to_not raise_error
76
+ expect { @http_client.send(:check_response, @response, @allow) }.to_not raise_error
68
77
  end
69
78
 
70
79
  it 'should raise exception if the response code is other than 200..201|401' do
71
80
  allow(@response).to receive(:code).and_return('500')
72
- expect { @http_client.send(:check_response, @response) }.to raise_error
81
+ expect { @http_client.send(:check_response, @response, @allow) }.to raise_error
73
82
  end
74
83
  end
75
84
 
@@ -161,6 +170,11 @@ describe LivePaper::HttpClient do
161
170
  @http_client.send(:http_request, @host, 'GET')
162
171
  end
163
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
+
164
178
  it 'should use ssl' do
165
179
  allow(Net::HTTP).to receive(:new).and_return(@http)
166
180
  expect(@http).to receive(:use_ssl=)
@@ -108,7 +108,7 @@ def lpp_delete_error_response
108
108
  { "associatedLinks": "1",
109
109
  "link": [{
110
110
  "rel": "associatedLinks",
111
- "href": "https://dev.livepaperapi.com/api/v1/links?trigger=uMTGQ0xoS7exSL3iujHJSA"
111
+ "href": "https://dev.livepaperapi.com/api/v1/links?trigger=my_trigger_id"
112
112
  }],
113
113
  "error": {
114
114
  "title": "409 Conflict",
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.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Whitmarsh
@@ -9,76 +9,76 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-03 00:00:00.000000000 Z
12
+ date: 2014-11-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ">="
18
+ - - '>='
19
19
  - !ruby/object:Gem::Version
20
20
  version: '0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ">="
25
+ - - '>='
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: bundler
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "~>"
32
+ - - ~>
33
33
  - !ruby/object:Gem::Version
34
34
  version: '1.6'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - "~>"
39
+ - - ~>
40
40
  - !ruby/object:Gem::Version
41
41
  version: '1.6'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rake
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ">="
46
+ - - '>='
47
47
  - !ruby/object:Gem::Version
48
48
  version: '0'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ">="
53
+ - - '>='
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: rspec
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ">="
60
+ - - '>='
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ">="
67
+ - - '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: webmock
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ">="
74
+ - - '>='
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - ">="
81
+ - - '>='
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  description: Ruby interface to use the Live Paper service by HP for creating watermarked
@@ -88,7 +88,7 @@ executables: []
88
88
  extensions: []
89
89
  extra_rdoc_files: []
90
90
  files:
91
- - ".gitignore"
91
+ - .gitignore
92
92
  - Gemfile
93
93
  - LICENSE.txt
94
94
  - README.md
@@ -122,12 +122,12 @@ require_paths:
122
122
  - lib
123
123
  required_ruby_version: !ruby/object:Gem::Requirement
124
124
  requirements:
125
- - - ">="
125
+ - - '>='
126
126
  - !ruby/object:Gem::Version
127
127
  version: '0'
128
128
  required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  requirements:
130
- - - ">="
130
+ - - '>='
131
131
  - !ruby/object:Gem::Version
132
132
  version: '0'
133
133
  requirements: []