soapy_cake 1.13.1 → 1.14.0

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: dcc3c764f74e351a63fd14ff47ccf24960fa1682
4
- data.tar.gz: 03003ea0a497c6823a25b61f7c9f95b5e63cccc6
3
+ metadata.gz: d99924dd593a9f0d8fd215d02a44177a7919cb13
4
+ data.tar.gz: da5d81a7738efdd7cfc4f1dd8372c1f4c7faa5d0
5
5
  SHA512:
6
- metadata.gz: bea2ce89ada7266e4c9e34c2e3e8c37e76942dbcd9754c7560242ec280541d072cb6a2bfb398b62e5711c21379599be0ba9edcfe9d1d1130ecb27158ab08589e
7
- data.tar.gz: 1313bcf0d0a1ffd99ec015cbd82f2812713b2f9c96c6dd16c33d57224d660fb8a62333182d159b38fe554f2657d4af3464016250a4ae3edd015cef7ef333599d
6
+ metadata.gz: a03fac64800f777a61b8c59969b16d133f252be0f8857b88c419496fc629a1fd639f260c0a87dcab706651e7dfaea7a7ab0c03a6b58e689e2c4fcb84b9095f5e
7
+ data.tar.gz: 470188fc1c53c5f2c798f32edc8a438ee7aae6f585798f1a585f72b248125a113f5be268d8b6de852cb33af91ff53c95aa910969d5a27251471ad5b6fc03c12b
@@ -113,8 +113,9 @@ module SoapyCake
113
113
  run Request.new(:admin, :signup, :affiliate, opts)
114
114
  end
115
115
 
116
- def media_types
117
- run(Request.new(:admin, :signup, :get_media_types, {}))[:MediaType]
116
+ def media_types(opts = {})
117
+ result = run(Request.new(:admin, :signup, :get_media_types, opts))
118
+ xml_response? ? result : result.try(:[], :MediaType)
118
119
  end
119
120
 
120
121
  def verticals
@@ -4,6 +4,10 @@ module SoapyCake
4
4
  advertisers affiliates campaigns offers creatives clicks conversions events
5
5
  )
6
6
 
7
+ def initialize(opts = {})
8
+ @opts = opts
9
+ end
10
+
7
11
  class BatchedRequest
8
12
  # Both 0 and 1 return the first element. We need to set it to 1,
9
13
  # otherwise we get an overlap in the next call. This is not documented in the API spec.
@@ -29,13 +33,12 @@ module SoapyCake
29
33
  Enumerator.new do |y|
30
34
  loop do
31
35
  result = next_batch
32
- @offset += limit
36
+ # raises StopIteration when less than `limit` elements are present
37
+ # which is then rescued by `loop`
33
38
  limit.times { y << result.next }
39
+ @offset += limit
34
40
  end
35
41
  end
36
- # we know we received less than limit objects when we see a stop iteration exception from
37
- # the underlying result enumerator and therefore know that we're done.
38
- rescue StopIteration # rubocop:disable Lint/HandleExceptions
39
42
  end
40
43
 
41
44
  private
@@ -47,16 +50,18 @@ module SoapyCake
47
50
  attr_reader :admin, :method, :opts, :offset, :limit
48
51
  end
49
52
 
50
- def method_missing(name, opts = {}, limit = nil)
53
+ def method_missing(name, method_opts = {}, limit = nil)
51
54
  fail Error, "Invalid method #{name}" unless ALLOWED_METHODS.include?(name)
52
55
 
53
- BatchedRequest.new(admin, name, opts, limit).to_enum
56
+ BatchedRequest.new(admin, name, method_opts, limit).to_enum
54
57
  end
55
58
 
56
59
  private
57
60
 
61
+ attr_reader :opts
62
+
58
63
  def admin
59
- @admin ||= Admin.new
64
+ @admin ||= Admin.new(opts)
60
65
  end
61
66
  end
62
67
  end
@@ -18,16 +18,33 @@ module SoapyCake
18
18
  request.api_key = api_key
19
19
  request.time_offset = time_offset
20
20
 
21
- response = Response.new(http_response(request), request.short_response?)
22
- response.time_offset = time_offset
23
- response.to_enum
21
+ Response
22
+ .new(response_body(request), request.short_response?, time_offset)
23
+ .public_send(:"to_#{xml_response? ? 'xml' : 'enum'}")
24
24
  end
25
25
 
26
26
  private
27
27
 
28
+ def xml_response?
29
+ opts[:xml_response] == true
30
+ end
31
+
32
+ def response_body(request)
33
+ if request.opts[:response].present?
34
+ request.opts[:response]
35
+ else
36
+ http_response(request)
37
+ end
38
+ end
39
+
28
40
  def http_response(request)
29
41
  url = "https://#{domain}#{request.path}"
30
- HTTParty.post(url, headers: headers, body: request.xml, timeout: NET_TIMEOUT).body
42
+ http_response = HTTParty.post(url, headers: headers, body: request.xml, timeout: NET_TIMEOUT)
43
+
44
+ fail RequestFailed, "Request failed with HTTP #{http_response.code}: " \
45
+ "#{http_response.body}" unless http_response.success?
46
+
47
+ http_response
31
48
  end
32
49
 
33
50
  def headers
@@ -2,28 +2,39 @@ module SoapyCake
2
2
  class Response
3
3
  include Helper
4
4
 
5
- attr_accessor :time_offset
6
- attr_reader :body, :short_response
5
+ SHORT_ELEMENT_DEPTH = 3
6
+ ELEMENTS_DEPTH = 5
7
7
 
8
- def initialize(body, short_response)
8
+ attr_reader :body, :short_response, :time_offset
9
+
10
+ def initialize(body, short_response, time_offset)
9
11
  @body = body
10
12
  @short_response = short_response
13
+ @time_offset = time_offset
11
14
  end
12
15
 
13
16
  def to_enum
14
17
  check_errors!
15
18
 
16
- return typed_element(sax.at_depth(3).first) if short_response
19
+ return typed_element(sax.at_depth(SHORT_ELEMENT_DEPTH).first) if short_response
17
20
 
18
21
  Enumerator.new do |y|
19
- sax.at_depth(5).each do |element|
22
+ sax.at_depth(ELEMENTS_DEPTH).each do |element|
20
23
  y << typed_element(element)
21
24
  end
22
25
  end
23
26
  end
24
27
 
28
+ def to_xml
29
+ (empty? ? [] : [body.to_s]).to_enum
30
+ end
31
+
25
32
  private
26
33
 
34
+ def empty?
35
+ sax.at_depth(ELEMENTS_DEPTH).first.nil?
36
+ end
37
+
27
38
  def typed_element(element)
28
39
  walk_tree(element) do |value, key|
29
40
  ResponseValue.new(key, value, time_offset: time_offset).parse
@@ -1,3 +1,3 @@
1
1
  module SoapyCake
2
- VERSION = '1.13.1'
2
+ VERSION = '1.14.0'
3
3
  end
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetMediaTypesResponse xmlns="http://cakemarketing.com/api/1/"><GetMediaTypesResult><MediaType><media_type_id>15</media_type_id><type_name>Adware</type_name></MediaType><MediaType><media_type_id>7</media_type_id><type_name>Banner</type_name></MediaType><MediaType><media_type_id>17</media_type_id><type_name>Content</type_name></MediaType><MediaType><media_type_id>6</media_type_id><type_name>Co-Reg</type_name></MediaType><MediaType><media_type_id>3</media_type_id><type_name>Email</type_name></MediaType><MediaType><media_type_id>12</media_type_id><type_name>Incentivized</type_name></MediaType><MediaType><media_type_id>2</media_type_id><type_name>Network</type_name></MediaType><MediaType><media_type_id>13</media_type_id><type_name>Other</type_name></MediaType><MediaType><media_type_id>8</media_type_id><type_name>PopUnder</type_name></MediaType><MediaType><media_type_id>4</media_type_id><type_name>PPC</type_name></MediaType><MediaType><media_type_id>9</media_type_id><type_name>Radio</type_name></MediaType><MediaType><media_type_id>5</media_type_id><type_name>SEO</type_name></MediaType><MediaType><media_type_id>16</media_type_id><type_name>Social Media</type_name></MediaType><MediaType><media_type_id>10</media_type_id><type_name>TV</type_name></MediaType><MediaType><media_type_id>11</media_type_id><type_name>Upsell</type_name></MediaType><MediaType><media_type_id>1</media_type_id><type_name>YouTube_Twitch</type_name></MediaType></GetMediaTypesResult></GetMediaTypesResponse></soap:Body></soap:Envelope>
@@ -0,0 +1,48 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://cake-partner-domain.com/api/1/signup.asmx
6
+ body:
7
+ encoding: UTF-8
8
+ string: |
9
+ <?xml version="1.0"?>
10
+ <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:cake="http://cakemarketing.com/api/1/">
11
+ <env:Header/>
12
+ <env:Body>
13
+ <cake:GetMediaTypes>
14
+ <cake:api_key>cake-api-key</cake:api_key>
15
+ </cake:GetMediaTypes>
16
+ </env:Body>
17
+ </env:Envelope>
18
+ headers:
19
+ Content-Type:
20
+ - application/soap+xml;charset=UTF-8
21
+ response:
22
+ status:
23
+ code: 200
24
+ message: OK
25
+ headers:
26
+ Cache-Control:
27
+ - private, max-age=0
28
+ Content-Type:
29
+ - application/soap+xml; charset=utf-8
30
+ Server:
31
+ - Microsoft-IIS/8.5
32
+ X-Aspnet-Version:
33
+ - 4.0.30319
34
+ X-Powered-By:
35
+ - ASP.NET
36
+ Date:
37
+ - Mon, 24 Aug 2015 12:29:10 GMT
38
+ Content-Length:
39
+ - '1736'
40
+ body:
41
+ encoding: UTF-8
42
+ string: <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
43
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetMediaTypesResponse
44
+ xmlns="http://cakemarketing.com/api/1/"><GetMediaTypesResult><MediaType><media_type_id>15</media_type_id><type_name>Adware</type_name></MediaType><MediaType><media_type_id>7</media_type_id><type_name>Banner</type_name></MediaType><MediaType><media_type_id>17</media_type_id><type_name>Content</type_name></MediaType><MediaType><media_type_id>6</media_type_id><type_name>Co-Reg</type_name></MediaType><MediaType><media_type_id>3</media_type_id><type_name>Email</type_name></MediaType><MediaType><media_type_id>12</media_type_id><type_name>Incentivized</type_name></MediaType><MediaType><media_type_id>2</media_type_id><type_name>Network</type_name></MediaType><MediaType><media_type_id>13</media_type_id><type_name>Other</type_name></MediaType><MediaType><media_type_id>8</media_type_id><type_name>PopUnder</type_name></MediaType><MediaType><media_type_id>4</media_type_id><type_name>PPC</type_name></MediaType><MediaType><media_type_id>9</media_type_id><type_name>Radio</type_name></MediaType><MediaType><media_type_id>5</media_type_id><type_name>SEO</type_name></MediaType><MediaType><media_type_id>16</media_type_id><type_name>Social
45
+ Media</type_name></MediaType><MediaType><media_type_id>10</media_type_id><type_name>TV</type_name></MediaType><MediaType><media_type_id>11</media_type_id><type_name>Upsell</type_name></MediaType><MediaType><media_type_id>1</media_type_id><type_name>YouTube_Twitch</type_name></MediaType></GetMediaTypesResult></GetMediaTypesResponse></soap:Body></soap:Envelope>
46
+ http_version:
47
+ recorded_at: Mon, 15 Jun 2015 12:00:00 GMT
48
+ recorded_with: VCR 2.9.3
@@ -92,4 +92,14 @@ RSpec.describe SoapyCake::Admin do
92
92
  type_name: 'Adware'
93
93
  )
94
94
  end
95
+
96
+ context 'XML responses' do
97
+ subject { described_class.new(xml_response: true) }
98
+
99
+ it 'returns an XML string', :vcr do
100
+ result = subject.media_types
101
+
102
+ expect(result.next).to eq(File.read('spec/fixtures/raw_response.xml').strip)
103
+ end
104
+ end
95
105
  end
@@ -188,5 +188,34 @@ RSpec.describe SoapyCake::Admin do
188
188
  let(:cake_method) { :affiliate }
189
189
  it_behaves_like 'a cake admin method'
190
190
  end
191
+
192
+ describe '#media_types' do
193
+ let(:method) { :media_types }
194
+ let(:cake_method) { :get_media_types }
195
+ it_behaves_like 'a cake admin method'
196
+
197
+ it 'uses a local response' do
198
+ expect(subject.media_types(response: File.read('spec/fixtures/raw_response.xml'))).to eq(
199
+ [
200
+ { media_type_id: 15, type_name: 'Adware' },
201
+ { media_type_id: 7, type_name: 'Banner' },
202
+ { media_type_id: 17, type_name: 'Content' },
203
+ { media_type_id: 6, type_name: 'Co-Reg' },
204
+ { media_type_id: 3, type_name: 'Email' },
205
+ { media_type_id: 12, type_name: 'Incentivized' },
206
+ { media_type_id: 2, type_name: 'Network' },
207
+ { media_type_id: 13, type_name: 'Other' },
208
+ { media_type_id: 8, type_name: 'PopUnder' },
209
+ { media_type_id: 4, type_name: 'PPC' },
210
+ { media_type_id: 9, type_name: 'Radio' },
211
+ { media_type_id: 5, type_name: 'SEO' },
212
+ { media_type_id: 16, type_name: 'Social Media' },
213
+ { media_type_id: 10, type_name: 'TV' },
214
+ { media_type_id: 11, type_name: 'Upsell' },
215
+ { media_type_id: 1, type_name: 'YouTube_Twitch' }
216
+ ]
217
+ )
218
+ end
219
+ end
191
220
  end
192
221
  end
@@ -23,7 +23,7 @@ RSpec.describe SoapyCake::Response do
23
23
  EOD
24
24
  end
25
25
 
26
- subject { described_class.new(xml.strip, false) }
26
+ subject { described_class.new(xml.strip, false, 0) }
27
27
 
28
28
  it 'returns an enumerator' do
29
29
  expect(subject.to_enum).to be_a(Enumerator)
@@ -1,6 +1,7 @@
1
1
  RSpec.shared_examples_for 'a cake admin method' do
2
+ let(:request) { double('request') }
3
+
2
4
  it 'runs the request' do
3
- request = double('request')
4
5
  expect(SoapyCake::Request).to receive(:new)
5
6
  .with(:admin, service, cake_method, cake_opts || {}).and_return(request)
6
7
  expect(subject).to receive(:run).with(request)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soapy_cake
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.13.1
4
+ version: 1.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ad2games GmbH
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-25 00:00:00.000000000 Z
11
+ date: 2015-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -195,6 +195,8 @@ files:
195
195
  - lib/soapy_cake/response_value.rb
196
196
  - lib/soapy_cake/version.rb
197
197
  - soapy_cake.gemspec
198
+ - spec/fixtures/raw_response.xml
199
+ - spec/fixtures/vcr_cassettes/SoapyCake_Admin/XML_responses/returns_an_XML_string.yml
198
200
  - spec/fixtures/vcr_cassettes/SoapyCake_Admin/creates_an_affiliate_and_returns_the_ID.yml
199
201
  - spec/fixtures/vcr_cassettes/SoapyCake_Admin/does_not_parse_a_transaction_id_as_an_integer.yml
200
202
  - spec/fixtures/vcr_cassettes/SoapyCake_Admin/raises_if_there_is_an_error.yml
@@ -253,6 +255,8 @@ signing_key:
253
255
  specification_version: 4
254
256
  summary: Simple client for the CAKE API
255
257
  test_files:
258
+ - spec/fixtures/raw_response.xml
259
+ - spec/fixtures/vcr_cassettes/SoapyCake_Admin/XML_responses/returns_an_XML_string.yml
256
260
  - spec/fixtures/vcr_cassettes/SoapyCake_Admin/creates_an_affiliate_and_returns_the_ID.yml
257
261
  - spec/fixtures/vcr_cassettes/SoapyCake_Admin/does_not_parse_a_transaction_id_as_an_integer.yml
258
262
  - spec/fixtures/vcr_cassettes/SoapyCake_Admin/raises_if_there_is_an_error.yml