zanox_publisher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +3 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +178 -0
  7. data/Rakefile +10 -0
  8. data/lib/zanox_publisher/ad_medium.rb +232 -0
  9. data/lib/zanox_publisher/ad_space.rb +159 -0
  10. data/lib/zanox_publisher/base.rb +69 -0
  11. data/lib/zanox_publisher/category.rb +57 -0
  12. data/lib/zanox_publisher/connection.rb +291 -0
  13. data/lib/zanox_publisher/exclusive_incentive.rb +135 -0
  14. data/lib/zanox_publisher/format.rb +19 -0
  15. data/lib/zanox_publisher/incentive.rb +135 -0
  16. data/lib/zanox_publisher/incentive_base.rb +89 -0
  17. data/lib/zanox_publisher/policy.rb +42 -0
  18. data/lib/zanox_publisher/prize.rb +18 -0
  19. data/lib/zanox_publisher/product.rb +263 -0
  20. data/lib/zanox_publisher/profile.rb +123 -0
  21. data/lib/zanox_publisher/program.rb +241 -0
  22. data/lib/zanox_publisher/program_application.rb +135 -0
  23. data/lib/zanox_publisher/tracking_link.rb +42 -0
  24. data/lib/zanox_publisher/version.rb +3 -0
  25. data/lib/zanox_publisher/vertical.rb +19 -0
  26. data/lib/zanox_publisher.rb +39 -0
  27. data/spec/config/credentials.example.yml +2 -0
  28. data/spec/lib/requirements_spec.rb +9 -0
  29. data/spec/lib/zanox_publisher/ad_medium_spec.rb +162 -0
  30. data/spec/lib/zanox_publisher/ad_space_spec.rb +56 -0
  31. data/spec/lib/zanox_publisher/base_spec.rb +35 -0
  32. data/spec/lib/zanox_publisher/connection_spec.rb +68 -0
  33. data/spec/lib/zanox_publisher/exclusive_incentive_spec.rb +104 -0
  34. data/spec/lib/zanox_publisher/incentive_spec.rb +112 -0
  35. data/spec/lib/zanox_publisher/product_spec.rb +223 -0
  36. data/spec/lib/zanox_publisher/profile_spec.rb +26 -0
  37. data/spec/lib/zanox_publisher/program_application_spec.rb +51 -0
  38. data/spec/lib/zanox_publisher/program_spec.rb +192 -0
  39. data/spec/lib/zanox_ruby_spec.rb +17 -0
  40. data/spec/spec_helper.rb +21 -0
  41. data/spec/support/credentials.rb +3 -0
  42. data/spec/support/vcr.rb +8 -0
  43. data/spec/vcr_cassettes/.gitignore +4 -0
  44. data/zanox_publisher.gemspec +27 -0
  45. metadata +203 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c6bf86e78f08a2c0843e4bbd65559a199ecafd8
4
+ data.tar.gz: cf5642ccc19e98e22ed773532b3e46c29dad6a70
5
+ SHA512:
6
+ metadata.gz: 735892e5eda13333689055bd9bb34566c7f24e5ea1dd9813aa051145198520686894bc8061d52646964ce3f7ffd98f5ce1c509021963bf5fe522fcdb835ac087
7
+ data.tar.gz: 9086e6044aa019dd1f197b6f3fd1a012d07a10181d0ceadefe444073aeb27466105320f564b0baa8a11c00b0b28221bb6cdc494c4b14ca6df4c3dbebd824159e
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ spec/config/credentials.yml
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zanox_ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Oliver Prater
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,178 @@
1
+ # ZanoxPublisher
2
+
3
+ This is a ruby wrapper for the [ZANOX Publisher API](https://developer.zanox.com/web/guest/publisher-api-2011), released on March 1, 2011.
4
+
5
+ ## Installation
6
+
7
+ The simplest way to install ZanoxPublisher is with [Bundler](http://gembundler.com/).
8
+
9
+ Add this line to your application's `Gemfile`:
10
+
11
+ ```ruby
12
+ gem 'zanox_publisher'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself:
20
+
21
+ $ gem install zanox_publisher
22
+
23
+ ## Configuration
24
+
25
+ The Zanox Publisher API requires a connect ID and a secret key to make requests directly related with the account.
26
+ General information can often be retrieved with just the connect ID.
27
+
28
+ The required information for each request can be found in the [documentation][documentation].
29
+
30
+ The connect ID and secret key can be set with the authenticate method:
31
+
32
+ ```ruby
33
+ ZanoxPublisher.authenticate('Connect ID', 'Secret key')
34
+ ```
35
+
36
+ Or directly on ZanoxPublisher.
37
+
38
+ ```ruby
39
+ ZanoxPublisher.connect_id = 'Connect ID'
40
+ ZanoxPublisher.secret_key = 'Secret key'
41
+ ```
42
+
43
+ ## Usage
44
+
45
+ The gem is designed for integratation with Ruby on Rails in mind.
46
+ The method names try to follow the common methods found on an ActiveRecord object.
47
+
48
+ ### Usage with Rails
49
+
50
+ Install ZanoxPublisher by adding it to your projects `Gemfile`.
51
+
52
+ ```ruby
53
+ gem 'zanox_publisher'
54
+ ```
55
+
56
+ And then execute:
57
+
58
+ $ bundle
59
+
60
+ Finally, create an initializer file `config/initializers/zanox_publisher.rb` to hold the configuration.
61
+
62
+ ```ruby
63
+ ZanoxPublisher.authenticate('Connect ID', 'Secret key')
64
+ ```
65
+
66
+ To make an API call in your application simply use the gems corresponding ruby object.
67
+
68
+ ### Usage outside of Rails
69
+
70
+ To use ZanoxPublisher in plain ruby require the gem and set the configuration before running your code.
71
+
72
+ ```ruby
73
+ require 'zanox_publisher'
74
+
75
+ ZanoxPublisher.authenticate('Connect ID', 'Secret key')
76
+
77
+ # Your code here
78
+ ```
79
+
80
+ ### Basic examples
81
+
82
+ ZanoxPublisher is designed to be a full ruby object representation for each of the Zanox API endpoints.
83
+ The matching of ruby objects to Zanox API method name's is given in the List of Objects.
84
+
85
+ The examples below expect that ZanoxPublisher is correctly configured and show only some basic usage examples.
86
+ Check the [documentation][documentation] for the complete reference.
87
+
88
+ **Search for a program that sells hats**
89
+
90
+ ```ruby
91
+ programs_that_sell_hats = ZanoxPublisher::Program.all q: 'hats', has_products: true
92
+ #=> [<Program>,...]
93
+
94
+ programs_that_sell_hats.count
95
+ #=> 84
96
+
97
+ first_program = programs_that_sell_hats.first
98
+ #=> <Program>
99
+
100
+ first_program.name
101
+ #=> "Asos.com DE"
102
+
103
+ first_program.products
104
+ #=> 50129
105
+ ```
106
+
107
+ **Search for a fitting product to an comment**
108
+
109
+ ```ruby
110
+ adspaces = ZanoxPublisher::AdSpace.all
111
+ #=> [<AdSpace>,...]
112
+
113
+ adspace = adspaces.select { |adspace| adspace.url == 'http://www.my-blog.com/' }.first
114
+ #=> <AdSpace>
115
+
116
+ my_programs = ZanoxPublisher::ProgramApplication.all(status: 'confirmed').map(&:program)
117
+ #=> [<Program>,...]
118
+
119
+ comment = params['comment']
120
+ #=> 'I also want a baseball cap.'
121
+
122
+ products = ZanoxPublisher::Product.page 0, per_page: 1, query: comment, adspace: adspace, programs: my_programs
123
+ #=> [<Product>]
124
+
125
+ product = products.first
126
+ #=> <Product>
127
+
128
+ product.tracking_links.first.ppc
129
+ #=> "http://ad.zanox.com/ppv/?..."
130
+ ```
131
+
132
+ ### List of Objects (Implimented)
133
+
134
+ * `ZanoxPublisher::AdMedium`: GetAdmedia, GetAdmedium
135
+ * `ZanoxPublisher::AdSpace`: GetAdspaces, GetAdspace
136
+ * `ZanoxPublisher::Incentive`: SearchIncentives, GetIncentive
137
+ * `ZanoxPublisher::ExclusiveIncentive`: SearchExclusiveIncentives, GetExclusiveIncentive
138
+ * `ZanoxPublisher::Product`: SearchProducts, GetProduct
139
+ * `ZanoxPublisher::Profile`: getProfiles
140
+ * `ZanoxPublisher::Program`: SearchPrograms, GetProgram
141
+ * `ZanoxPublisher::ProgramApplication`: GetProgramApplications
142
+
143
+ ## Documentation
144
+
145
+ [http://www.rubydoc.info/github/oliverprater/zanox_publisher/master][documentation]
146
+
147
+ [documentation]: http://www.rubydoc.info/github/oliverprater/zanox_publisher/master
148
+
149
+ ## Versioning
150
+
151
+ This library aims to adhere to [Semantic Versioning 2.0.0][semver]. Violations
152
+ of this scheme should be reported as bugs. Specifically, if a minor or patch
153
+ version is released that breaks backward compatibility, that version should be
154
+ immediately yanked and/or a new version should be immediately released that
155
+ restores compatibility. Breaking changes to the public API will only be
156
+ introduced with new major versions. As a result of this policy, you can (and
157
+ should) specify a dependency on this gem using the [Pessimistic Version
158
+ Constraint][pvc] with two digits of precision. For example:
159
+
160
+ 'zanox_publisher', '~> 0.1'
161
+
162
+ [semver]: http://semver.org/
163
+ [pvc]: http://docs.rubygems.org/read/chapter/16#page74
164
+
165
+ ## Contributing
166
+
167
+ 1. Fork it ( https://github.com/oliverprater/zanox_publisher/fork )
168
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
169
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
170
+ 4. Push to the branch (`git push origin my-new-feature`)
171
+ 5. Create a new Pull Request
172
+
173
+ ## Copyright
174
+
175
+ Copyright (c) 2015 Oliver Prater.
176
+ See [LICENSE][license] for details.
177
+
178
+ [license]: LICENSE.md
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ # Console for active development
4
+ # http://stackoverflow.com/questions/4493114/developing-gems-and-testing
5
+ #
6
+ # => rake console
7
+ #
8
+ task :console do
9
+ exec "irb -r zanox_publisher -I ./lib"
10
+ end
@@ -0,0 +1,232 @@
1
+ module ZanoxPublisher
2
+ # AdMedium
3
+ #
4
+ # Get banners and links, including tracking links
5
+ #
6
+ # @attr [Integer] id The admediumItem's identifer from Zanox
7
+ # @attr [String] name The name for the ad medium
8
+ # @attr [Fixnum] adrank The adrank of the ad medium
9
+ # @attr [String] admedium_type The type of ad medium
10
+ # @attr [Program] program The program to which the ad medium belongs
11
+ # @attr [String] title The title of the ad medium
12
+ # @attr [Integer] height The height in pixel of the image
13
+ # @attr [Integer] width The width in pixel of the image
14
+ # @attr [Format] format The formats' Zanox identifer and name
15
+ # @attr [String] code The code of the ad medium
16
+ # @attr [String] description The description from the advertiser
17
+ # @attr [String] instruction The instructions from the advertiser
18
+ # @attr [String] purpose The purpose assigned to the ad medium
19
+ # @attr [Array<Category>] category The merchants' given category to the ad medium
20
+ # @attr [Hash] group The group given to the ad medium
21
+ # @attr [Array] tags A list of tags for the ad medium
22
+ # @attr [Array<TrackingLink>] tracking_links The tracking links of the ad medium for each ad space
23
+ class AdMedium < Base
24
+ RESOURCE_PATH = '/admedia'
25
+
26
+ # ENUM types
27
+ @@admedia_types = %w(html script lookatMedia image imageText text)
28
+ @@admedia_purposes = %w(startPage productDeeplink categoryDeeplink searchDeeplink)
29
+
30
+ class << self
31
+ # Returns the Zanox API admediaTypeEnum datatype
32
+ #
33
+ # @return [Array<String>]
34
+ def admedia_types
35
+ @@admedia_types
36
+ end
37
+
38
+ # Returns the Zanox API admediaPurposeEnum datatype
39
+ #
40
+ # @return [Array<String>]
41
+ def admedia_purposes
42
+ @@admedia_purposes
43
+ end
44
+
45
+ # Retrieves all affiliate link's dependent on search parameters.
46
+ #
47
+ # This is equivalent to the Zanox API method GetAdmedia.
48
+ # The method documentation can be found under {https://developer.zanox.com/web/guest/publisher-api-2011/get-admedia}.
49
+ #
50
+ # Authentication: Requires connect ID.
51
+ #
52
+ # This can require multiple requests, as internally every page is pulled.
53
+ # The ZanoxPublisher::AdMedium.page function can be used to better control the requests made.
54
+ #
55
+ # @param program [Integer] Limits results to a particular program ID.
56
+ # @param region [String] Limits results to programs from a particular region.
57
+ # @param format [String] Limits results to an ad media format.
58
+ # @param admedium_type [String] Limits results to an ad media type (API equivalent is admediumtype).
59
+ # @param admediumtype [String] Limits results to an ad media type (API name).
60
+ # @param purpose [String] Limits results to a type of link to the advertiser shop.
61
+ # @param partnership [String] Limits results to either programs to whom the publisher has successfully applied ("direct"), or to those who belong to zanox's publicly available ad pool ("indirect").
62
+ # @param category [String] Limits results to one of the program's ad media categories. Ad media categories are defined by each advertiser for their program, and can be retrieved using GetAdmediumCategories.
63
+ # @param adspace [AdSpace, Integer] Limits results to tracking links associated with this ad space.
64
+ #
65
+ # @return [Array<AdMedium>]
66
+ def all(options = {})
67
+ retval = []
68
+ current_page = 0
69
+ options.merge!({ per_page: maximum_per_page })
70
+
71
+ begin
72
+ retval += self.page(current_page, options)
73
+ current_page += 1
74
+ end while AdMedium.total > retval.size
75
+
76
+ retval
77
+ end
78
+
79
+ # Retrieves the requested page of admedium items dependent on search parameters.
80
+ #
81
+ # This is equivalent to the Zanox API method GetAdmedia.
82
+ # The method documentation can be found under {https://developer.zanox.com/web/guest/publisher-api-2011/get-admedia}.
83
+ #
84
+ # Authentication: Requires connect ID.
85
+ #
86
+ # @param page [Integer] the page position
87
+ # @param per_page [Integer] number of items in the result set (API equivalent is items)
88
+ # @param items [Integer] number of items in the result set (API name)
89
+ # @param program [Program, Integer] Limits results to a particular program ID.
90
+ # @param region [String] Limits results to programs from a particular region.
91
+ # @param format [String] Limits results to an ad media format.
92
+ # @param admedium_type [String] Limits results to an ad media type (API equivalent is admediumtype).
93
+ # @param admediumtype [String] Limits results to an ad media type (API name).
94
+ # @param purpose [String] Limits results to a type of link to the advertiser shop.
95
+ # @param partnership [String] Limits results to either programs to whom the publisher has successfully applied ("direct"), or to those who belong to zanox's publicly available ad pool ("indirect").
96
+ # @param category [Category, String] Limits results to one of the program's ad media categories. Ad media categories are defined by each advertiser for their program, and can be retrieved using GetAdmediumCategories.
97
+ # @param adspace [AdSpace, Integer] Limits results to tracking links associated with this ad space.
98
+ #
99
+ # @return [Array<AdSpace>]
100
+ def page(page = 0, options = {})
101
+ params = { query: { page: page } }
102
+
103
+ per_page = nil
104
+ per_page = options[:per_page] if per_page.nil?
105
+ per_page = options[:items] if per_page.nil?
106
+ per_page = AdMedium.per_page if per_page.nil?
107
+ params[:query].merge!({ items: per_page })
108
+
109
+ program = options[:program]
110
+ program = program.to_i unless program.nil?
111
+
112
+ region = options[:region]
113
+
114
+ format = options[:format]
115
+
116
+ admedium_type = options[:admedium_type]
117
+ admedium_type = options[:admediumtype] if admedium_type.nil?
118
+ admedium_type = nil unless @@admedia_types.include? admedium_type
119
+
120
+ purpose = options[:purpose]
121
+ purpose = nil unless @@admedia_purposes.include? purpose
122
+
123
+ partnership = options[:partnership]
124
+
125
+ # Limits results to one of the program's ad media categories. Ad media categories are defined by each advertiser for their program, and can be retrieved using GetAdmediumCategories.
126
+ category = options[:category]
127
+ category = category.to_i unless category.nil?
128
+
129
+ adspace = options[:adspace]
130
+ adspace = adspace.to_i unless adspace.nil?
131
+
132
+ # Build the query on hand of the options received
133
+ params[:query].merge!({ program: program }) unless program.nil?
134
+ params[:query].merge!({ region: region }) unless region.nil?
135
+ params[:query].merge!({ format: format }) unless format.nil?
136
+ params[:query].merge!({ admediumtype: admedium_type }) unless admedium_type.nil?
137
+ params[:query].merge!({ purpose: purpose }) unless purpose.nil?
138
+ params[:query].merge!({ partnership: partnership }) unless partnership.nil?
139
+ params[:query].merge!({ category: category }) unless category.nil?
140
+ params[:query].merge!({ adspace: adspace }) unless adspace.nil?
141
+
142
+ retval = []
143
+
144
+ response = self.connection.get(RESOURCE_PATH, params)
145
+
146
+ AdMedium.total = response.fetch('total')
147
+
148
+ admedia = []
149
+ admedia = response.fetch('admediumItems', {}).fetch('admediumItem', []) if AdMedium.total > 0
150
+
151
+ admedia.each do |admedium|
152
+ retval << AdMedium.new(admedium)
153
+ end
154
+
155
+ retval
156
+ end
157
+
158
+ # Returns a single admediumItem, as queried by its ID.
159
+ #
160
+ # The use of the trackingLink of the ID is not supported at the moment.
161
+ #
162
+ # This is equivalent to the Zanox API method GetAdmedium.
163
+ # The method documentation can be found under {https://developer.zanox.com/web/guest/publisher-api-2011/get-admedia-admedium}.
164
+ #
165
+ # Authentication: Requires connect ID.
166
+ #
167
+ # @param id [Integer] the ID of the adspace you want to get.
168
+ # @param adspace [AdSpace, Integer] if you would like tracking links for only one of your publisher ad spaces, pass its ID in this parameter.
169
+ #
170
+ # @return [<AdMedium>]
171
+ def find(id, options = {})
172
+ params = {}
173
+
174
+ adspace = options[:adspace]
175
+ adspace = adspace.to_i unless adspace.nil?
176
+
177
+ params = { query: { adspace: adspace } } unless adspace.nil?
178
+
179
+ response = self.connection.get(RESOURCE_PATH + "/admedium/#{id}", params)
180
+ admedium = response.fetch('admediumItem', [])
181
+
182
+ AdMedium.new(admedium)
183
+ end
184
+
185
+ # A connection instance with AdMediums' relative_path
186
+ #
187
+ # @return [Connection]
188
+ def connection
189
+ @connection ||= Connection.new(RESOURCE_PATH)
190
+ end
191
+ end
192
+
193
+ def initialize(data = {})
194
+ @id = data.fetch('@id').to_i
195
+ @name = data.fetch('name')
196
+ @adrank = data.fetch('adrank')
197
+ @admedium_type = data.fetch('admediumType')
198
+ @program = Program.new(data.fetch('program'))
199
+ # Optionally returned data
200
+ @title = data.fetch('title', nil)
201
+ @height = data.fetch('height', nil)
202
+ @width = data.fetch('width', nil)
203
+ @format = data.fetch('format', nil)
204
+ @format = Format.new(@format) unless @format.nil?
205
+ @code = data.fetch('code', nil)
206
+ @description = data.fetch('description', nil)
207
+ @instruction = data.fetch('instruction', nil)
208
+ @purpose = data.fetch('purpose', nil)
209
+ @category = data.fetch('category', nil)
210
+ @category = Category.new(@category) unless @category.nil?
211
+ @group = data.fetch('group', nil)
212
+ @tags = data.fetch('tags', nil)
213
+ @tracking_links = TrackingLink.fetch(data.fetch('trackingLinks', {})['trackingLink'])
214
+ end
215
+
216
+ # Returns the admediumItems' ID as integer representation
217
+ #
218
+ # @return [Integer]
219
+ def to_i
220
+ @id
221
+ end
222
+
223
+ attr_accessor :id, :name, :adrank, :admedium_type, :program,
224
+ :title, :height, :width, :format, :code,
225
+ :description, :instruction, :purpose, :category,
226
+ :group, :tags, :tracking_links
227
+
228
+ # make API names available
229
+ alias admediumType admedium_type
230
+ alias trackingLinks tracking_links
231
+ end
232
+ end
@@ -0,0 +1,159 @@
1
+ module ZanoxPublisher
2
+ # AdSpaces
3
+ #
4
+ # Access and modify your AdSpace information
5
+ #
6
+ # @attr [Integer] id The profileItem's identifer from Zanox
7
+ # @attr [String] name The name of your ad space
8
+ # @attr [String] url The url of your ad space
9
+ # @attr [String] description The description for your ad space
10
+ # @attr [String] adspace_type The type of ad space
11
+ # @attr [Integer] visitors The number of visitors to your ad space
12
+ # @attr [Integer] impressions The number of impressions of your ad space
13
+ # @attr [String] scope The scope of your ad space
14
+ # @attr [Array<String>] regions The regions for the ad space
15
+ # @attr [Array<Category>] categories The categories of your ad space
16
+ # @attr [String] language The language of your ad space
17
+ # @attr [Integer] check_number The check number for this ad space
18
+ class AdSpace < Base
19
+ RESOURCE_PATH = '/adspaces'
20
+
21
+ ADSPACE_TYPE_ENUM = %w(website email searchengine)
22
+ ADSPACE_SCOPE_ENUM = %w(private business)
23
+
24
+ class << self
25
+ # Retrieves all adspace items related to the publisher account.
26
+ #
27
+ # This is equivalent to the Zanox API method GetAdspaces.
28
+ # The method documentation can be found under {https://developer.zanox.com/web/guest/publisher-api-2011/get-adspaces}.
29
+ #
30
+ # Authentication: Requires signature.
31
+ #
32
+ # This can require multiple requests, as internally every page is pulled.
33
+ # The ZanoxPublisher::AdSpace.page function can be used to better control the requests made.
34
+ #
35
+ # @return [Array<AdSpace>]
36
+ def all
37
+ retval = []
38
+ current_page = 0
39
+
40
+ begin
41
+ retval += self.page(current_page, { per_page: maximum_per_page })
42
+ current_page += 1
43
+ end while AdSpace.total > retval.size
44
+
45
+ retval
46
+ end
47
+
48
+ # Retrieves the requested page of adspace items related to the publisher account.
49
+ #
50
+ # This is equivalent to the Zanox API method GetAdspaces.
51
+ # The method documentation can be found under {https://developer.zanox.com/web/guest/publisher-api-2011/get-adspaces}.
52
+ #
53
+ # Authentication: Requires signature.
54
+ #
55
+ # @param page [Integer] the page position
56
+ # @param per_page [Integer] number of items in the result set (API equivalent is items)
57
+ # @param items [Integer] number of items in the result set (API option name)
58
+ #
59
+ # @example
60
+ # ZanoxPublisher::AdSpace.page(1, per_page: 50) #=> [<AdSpace...>]
61
+ #
62
+ # @example
63
+ # ZanoxPublisher::AdSpace.page(2) #=> [<AdSpace...>]
64
+ #
65
+ # @example
66
+ # ZanoxPublisher::AdSpace.page #=> [<AdSpace...>]
67
+ #
68
+ # @return [Array<AdSpace>]
69
+ def page(page = 0, options = {})
70
+ params = { query: { page: page } }
71
+
72
+ per_page = nil
73
+ per_page = options[:per_page] if per_page.nil?
74
+ per_page = options[:items] if per_page.nil?
75
+ per_page = AdSpace.per_page if per_page.nil?
76
+ params[:query].merge!({ items: per_page })
77
+
78
+ retval = []
79
+
80
+ response = self.connection.signature_get(RESOURCE_PATH, params)
81
+
82
+ AdSpace.total = response.fetch('total')
83
+ adspaces = response.fetch('adspaceItems', []).fetch('adspaceItem', [])
84
+
85
+ adspaces.each do |adspace|
86
+ retval << AdSpace.new(adspace)
87
+ end
88
+
89
+ retval
90
+ end
91
+
92
+ # Returns a single adspaceItem, as queried by its ID.
93
+ #
94
+ # This is equivalent to the Zanox API method GetAdspace.
95
+ # The method documentation can be found under {https://developer.zanox.com/web/guest/publisher-api-2011/get-adspaces-adspace}.
96
+ #
97
+ # Authentication: Requires signature.
98
+ #
99
+ # @param id [Integer] the ID of the adspace you want to get
100
+ #
101
+ # @return [<AdSpace>]
102
+ def find(id)
103
+ response = self.connection.signature_get(RESOURCE_PATH + "/adspace/#{id}")
104
+ adspace = response.fetch('adspaceItem', []).first
105
+
106
+ AdSpace.new(adspace)
107
+ end
108
+
109
+ # A connection instance with AdSpaces' relative_path
110
+ #
111
+ # @return [Connection]
112
+ def connection
113
+ @connection ||= Connection.new(RESOURCE_PATH)
114
+ end
115
+ end
116
+
117
+ # TODO: POST {https://developer.zanox.com/web/guest/publisher-api-2011/post-adspaces-adspace}
118
+ # TODO: PUT {https://developer.zanox.com/web/guest/publisher-api-2011/put-adspaces-adspace}
119
+ # TODO: DELETE {https://developer.zanox.com/web/guest/publisher-api-2011/delete-adspaces-adspace}
120
+ #
121
+ def initialize(data = {})
122
+ @id = data.fetch('@id').to_i
123
+
124
+ # Depending on short or long representation of object
125
+ if data.fetch('$', nil).nil?
126
+ @name = data.fetch('name')
127
+ @url = data.fetch('url')
128
+ @description = data.fetch('description')
129
+ @adspace_type = data.fetch('adspaceType')
130
+ @visitors = data.fetch('visitors')
131
+ @impressions = data.fetch('impressions')
132
+ @scope = data.fetch('scope', nil)
133
+ @regions = data.fetch('regions', []).first
134
+ @regions = @regions.fetch('region') unless @regions.nil?
135
+ @regions = [@regions] if @regions.is_a? String
136
+ @categories = Category.fetch(data['categories'])
137
+ @language = data.fetch('language')
138
+ @check_number = data.fetch('checkNumber')
139
+ else
140
+ @name = data.fetch('$')
141
+ end
142
+ end
143
+
144
+ # Returns the adspaceItems' ID as integer representation
145
+ #
146
+ # @return [Integer]
147
+ def to_i
148
+ @id
149
+ end
150
+
151
+ attr_accessor :id, :name, :url, :description, :adspace_type,
152
+ :visitors, :impressions, :scope, :regions,
153
+ :categories, :language, :check_number
154
+
155
+ # make API names available
156
+ alias adspaceType adspace_type
157
+ alias checkNumber check_number
158
+ end
159
+ end