flowcommerce 0.0.12 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1044 +0,0 @@
1
- # Generated by apidoc - http://www.apidoc.me
2
- # Service version: 0.0.36
3
- # apidoc:0.11.29 http://www.apidoc.me/flow/experience/0.0.36/ruby_client
4
-
5
- require 'cgi'
6
- require 'net/http'
7
- require 'net/https'
8
- require 'uri'
9
- require 'base64'
10
-
11
- require 'date'
12
- require 'rubygems'
13
- require 'json'
14
- require 'bigdecimal'
15
-
16
- # An experience defines all of the configuration and settings that are relevant
17
- # to a particular country
18
- module Io
19
- module Flow
20
- module Experience
21
- module V0
22
-
23
- class Client
24
-
25
- module Constants
26
-
27
- BASE_URL = 'https://experience.api.flow.io' unless defined?(Constants::BASE_URL)
28
- NAMESPACE = 'io.flow.experience.v0' unless defined?(Constants::NAMESPACE)
29
- USER_AGENT = 'apidoc:0.11.29 http://www.apidoc.me/flow/experience/0.0.36/ruby_client' unless defined?(Constants::USER_AGENT)
30
- VERSION = '0.0.36' unless defined?(Constants::VERSION)
31
- VERSION_MAJOR = 0 unless defined?(VERSION_MAJOR)
32
-
33
- end
34
-
35
- attr_reader :url
36
-
37
- def initialize(url, opts={})
38
- @url = HttpClient::Preconditions.assert_class('url', url, String)
39
- @authorization = HttpClient::Preconditions.assert_class_or_nil('authorization', opts.delete(:authorization), HttpClient::Authorization)
40
- @default_headers = HttpClient::Preconditions.assert_class('default_headers', opts.delete(:default_headers) || {}, Hash)
41
- HttpClient::Preconditions.assert_empty_opts(opts)
42
- HttpClient::Preconditions.check_state(url.match(/http.+/i), "URL[%s] must start with http" % url)
43
- end
44
-
45
- # Creates an instance of the client using the base url specified in the API spec.
46
- def Client.at_base_url(opts={})
47
- Client.new(Constants::BASE_URL, opts)
48
- end
49
-
50
- def request(path=nil)
51
- HttpClient::Preconditions.assert_class_or_nil('path', path, String)
52
- request = HttpClient::Request.new(URI.parse(@url + path.to_s)).with_header('User-Agent', Constants::USER_AGENT).with_header('X-Apidoc-Version', Constants::VERSION).with_header('X-Apidoc-Version-Major', Constants::VERSION_MAJOR)
53
-
54
- @default_headers.each do |key, value|
55
- request = request.with_header(key, value)
56
- end
57
-
58
- if @authorization
59
- request = request.with_auth(@authorization)
60
- end
61
-
62
- request
63
- end
64
-
65
- def displays
66
- @displays ||= ::Io::Flow::Experience::V0::Clients::Displays.new(self)
67
- end
68
-
69
- def experiences
70
- @experiences ||= ::Io::Flow::Experience::V0::Clients::Experiences.new(self)
71
- end
72
- end
73
-
74
- module Clients
75
-
76
- class Displays
77
-
78
- def initialize(client)
79
- @client = HttpClient::Preconditions.assert_class('client', client, ::Io::Flow::Experience::V0::Client)
80
- end
81
-
82
- # Provides visibility into recent changes of each object, including deletion
83
- def get_versions(organization, incoming={})
84
- HttpClient::Preconditions.assert_class('organization', organization, String)
85
- opts = HttpClient::Helper.symbolize_keys(incoming)
86
- query = {
87
- :id => (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, Array).map { |v| HttpClient::Preconditions.assert_class('id', v, String) }),
88
- :key => (x = opts.delete(:key); x.nil? ? nil : HttpClient::Preconditions.assert_class('key', x, Array).map { |v| HttpClient::Preconditions.assert_class('key', v, String) }),
89
- :name => (x = opts.delete(:name); x.nil? ? nil : HttpClient::Preconditions.assert_class('name', x, Array).map { |v| HttpClient::Preconditions.assert_class('name', v, String) }),
90
- :limit => HttpClient::Preconditions.assert_class('limit', (x = opts.delete(:limit); x.nil? ? 25 : x), Integer),
91
- :offset => HttpClient::Preconditions.assert_class('offset', (x = opts.delete(:offset); x.nil? ? 0 : x), Integer),
92
- :sort => HttpClient::Preconditions.assert_class('sort', (x = opts.delete(:sort); x.nil? ? "journal_timestamp" : x), String)
93
- }.delete_if { |k, v| v.nil? }
94
- r = @client.request("/#{CGI.escape(organization)}/displays/versions").with_query(query).get
95
- r.map { |x| ::Io::Flow::Experience::V0::Models::DisplayVersion.new(x) }
96
- end
97
-
98
- # Search displays. Always paginated.
99
- def get(organization, incoming={})
100
- HttpClient::Preconditions.assert_class('organization', organization, String)
101
- opts = HttpClient::Helper.symbolize_keys(incoming)
102
- query = {
103
- :id => (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, Array).map { |v| HttpClient::Preconditions.assert_class('id', v, String) }),
104
- :key => (x = opts.delete(:key); x.nil? ? nil : HttpClient::Preconditions.assert_class('key', x, Array).map { |v| HttpClient::Preconditions.assert_class('key', v, String) }),
105
- :name => (x = opts.delete(:name); x.nil? ? nil : HttpClient::Preconditions.assert_class('name', x, Array).map { |v| HttpClient::Preconditions.assert_class('name', v, String) }),
106
- :limit => HttpClient::Preconditions.assert_class('limit', (x = opts.delete(:limit); x.nil? ? 25 : x), Integer),
107
- :offset => HttpClient::Preconditions.assert_class('offset', (x = opts.delete(:offset); x.nil? ? 0 : x), Integer),
108
- :sort => HttpClient::Preconditions.assert_class('sort', (x = opts.delete(:sort); x.nil? ? "name" : x), String)
109
- }.delete_if { |k, v| v.nil? }
110
- r = @client.request("/#{CGI.escape(organization)}/displays").with_query(query).get
111
- r.map { |x| ::Io::Flow::Experience::V0::Models::Display.new(x) }
112
- end
113
-
114
- # Returns information about a specific display.
115
- def get_by_key(organization, key)
116
- HttpClient::Preconditions.assert_class('organization', organization, String)
117
- HttpClient::Preconditions.assert_class('key', key, String)
118
- r = @client.request("/#{CGI.escape(organization)}/displays/#{CGI.escape(key)}").get
119
- ::Io::Flow::Experience::V0::Models::Display.new(r)
120
- end
121
-
122
- # Add display
123
- def post(organization, display_form)
124
- HttpClient::Preconditions.assert_class('organization', organization, String)
125
- HttpClient::Preconditions.assert_class('display_form', display_form, ::Io::Flow::Experience::V0::Models::DisplayForm)
126
- r = @client.request("/#{CGI.escape(organization)}/displays").with_json(display_form.to_json).post
127
- ::Io::Flow::Experience::V0::Models::Display.new(r)
128
- end
129
-
130
- # Update display with the specified key, creating if it does not exist.
131
- def put_by_key(organization, key, display_form)
132
- HttpClient::Preconditions.assert_class('organization', organization, String)
133
- HttpClient::Preconditions.assert_class('key', key, String)
134
- HttpClient::Preconditions.assert_class('display_form', display_form, ::Io::Flow::Experience::V0::Models::DisplayForm)
135
- r = @client.request("/#{CGI.escape(organization)}/displays/#{CGI.escape(key)}").with_json(display_form.to_json).put
136
- ::Io::Flow::Experience::V0::Models::Display.new(r)
137
- end
138
-
139
- # Delete the display with this key
140
- def delete_by_key(organization, key)
141
- HttpClient::Preconditions.assert_class('organization', organization, String)
142
- HttpClient::Preconditions.assert_class('key', key, String)
143
- r = @client.request("/#{CGI.escape(organization)}/displays/#{CGI.escape(key)}").delete
144
- nil
145
- end
146
-
147
- end
148
-
149
- class Experiences
150
-
151
- def initialize(client)
152
- @client = HttpClient::Preconditions.assert_class('client', client, ::Io::Flow::Experience::V0::Client)
153
- end
154
-
155
- # Provides visibility into recent changes of each object, including deletion
156
- def get_versions(organization, incoming={})
157
- HttpClient::Preconditions.assert_class('organization', organization, String)
158
- opts = HttpClient::Helper.symbolize_keys(incoming)
159
- query = {
160
- :id => (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, Array).map { |v| HttpClient::Preconditions.assert_class('id', v, String) }),
161
- :key => (x = opts.delete(:key); x.nil? ? nil : HttpClient::Preconditions.assert_class('key', x, Array).map { |v| HttpClient::Preconditions.assert_class('key', v, String) }),
162
- :name => (x = opts.delete(:name); x.nil? ? nil : HttpClient::Preconditions.assert_class('name', x, Array).map { |v| HttpClient::Preconditions.assert_class('name', v, String) }),
163
- :limit => HttpClient::Preconditions.assert_class('limit', (x = opts.delete(:limit); x.nil? ? 25 : x), Integer),
164
- :offset => HttpClient::Preconditions.assert_class('offset', (x = opts.delete(:offset); x.nil? ? 0 : x), Integer),
165
- :sort => HttpClient::Preconditions.assert_class('sort', (x = opts.delete(:sort); x.nil? ? "journal_timestamp" : x), String)
166
- }.delete_if { |k, v| v.nil? }
167
- r = @client.request("/#{CGI.escape(organization)}/experiences/versions").with_query(query).get
168
- r.map { |x| ::Io::Flow::Experience::V0::Models::ExperienceVersion.new(x) }
169
- end
170
-
171
- # Search experiences. Always paginated.
172
- def get(organization, incoming={})
173
- HttpClient::Preconditions.assert_class('organization', organization, String)
174
- opts = HttpClient::Helper.symbolize_keys(incoming)
175
- query = {
176
- :id => (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, Array).map { |v| HttpClient::Preconditions.assert_class('id', v, String) }),
177
- :country => (x = opts.delete(:country); x.nil? ? nil : HttpClient::Preconditions.assert_class('country', x, Array).map { |v| HttpClient::Preconditions.assert_class('country', v, String) }),
178
- :currency => (x = opts.delete(:currency); x.nil? ? nil : HttpClient::Preconditions.assert_class('currency', x, Array).map { |v| HttpClient::Preconditions.assert_class('currency', v, String) }),
179
- :display => (x = opts.delete(:display); x.nil? ? nil : HttpClient::Preconditions.assert_class('display', x, Array).map { |v| HttpClient::Preconditions.assert_class('display', v, String) }),
180
- :key => (x = opts.delete(:key); x.nil? ? nil : HttpClient::Preconditions.assert_class('key', x, Array).map { |v| HttpClient::Preconditions.assert_class('key', v, String) }),
181
- :name => (x = opts.delete(:name); x.nil? ? nil : HttpClient::Preconditions.assert_class('name', x, Array).map { |v| HttpClient::Preconditions.assert_class('name', v, String) }),
182
- :subcatalog => (x = opts.delete(:subcatalog); x.nil? ? nil : HttpClient::Preconditions.assert_class('subcatalog', x, Array).map { |v| HttpClient::Preconditions.assert_class('subcatalog', v, String) }),
183
- :limit => HttpClient::Preconditions.assert_class('limit', (x = opts.delete(:limit); x.nil? ? 25 : x), Integer),
184
- :offset => HttpClient::Preconditions.assert_class('offset', (x = opts.delete(:offset); x.nil? ? 0 : x), Integer),
185
- :sort => HttpClient::Preconditions.assert_class('sort', (x = opts.delete(:sort); x.nil? ? "name" : x), String)
186
- }.delete_if { |k, v| v.nil? }
187
- r = @client.request("/#{CGI.escape(organization)}/experiences").with_query(query).get
188
- r.map { |x| ::Io::Flow::Experience::V0::Models::Experience.new(x) }
189
- end
190
-
191
- # Compose items and prices. Always paginated.
192
- def get_items(organization, incoming={})
193
- HttpClient::Preconditions.assert_class('organization', organization, String)
194
- opts = HttpClient::Helper.symbolize_keys(incoming)
195
- query = {
196
- :number => (x = opts.delete(:number); x.nil? ? nil : HttpClient::Preconditions.assert_class('number', x, Array).map { |v| HttpClient::Preconditions.assert_class('number', v, String) }),
197
- :destination => (x = opts.delete(:destination); x.nil? ? nil : HttpClient::Preconditions.assert_class('destination', x, String)),
198
- :from_country => (x = opts.delete(:from_country); x.nil? ? nil : HttpClient::Preconditions.assert_class('from_country', x, String)),
199
- :ip => (x = opts.delete(:ip); x.nil? ? nil : HttpClient::Preconditions.assert_class('ip', x, String)),
200
- :context => (x = opts.delete(:context); x.nil? ? nil : HttpClient::Preconditions.assert_class('context', x, String)),
201
- :currency => (x = opts.delete(:currency); x.nil? ? nil : HttpClient::Preconditions.assert_class('currency', x, String)),
202
- :language => (x = opts.delete(:language); x.nil? ? nil : HttpClient::Preconditions.assert_class('language', x, String)),
203
- :limit => HttpClient::Preconditions.assert_class('limit', (x = opts.delete(:limit); x.nil? ? 25 : x), Integer),
204
- :offset => HttpClient::Preconditions.assert_class('offset', (x = opts.delete(:offset); x.nil? ? 0 : x), Integer),
205
- :sort => HttpClient::Preconditions.assert_class('sort', (x = opts.delete(:sort); x.nil? ? "-created_at" : x), String)
206
- }.delete_if { |k, v| v.nil? }
207
- r = @client.request("/#{CGI.escape(organization)}/experiences/items").with_query(query).get
208
- r.map { |x| ::Io::Flow::Catalog::V0::Models::Item.new(x) }
209
- end
210
-
211
- # Checkout, aggregate items and prices.
212
- def post_checkout(organization, checkout_form)
213
- HttpClient::Preconditions.assert_class('organization', organization, String)
214
- HttpClient::Preconditions.assert_class('checkout_form', checkout_form, ::Io::Flow::Experience::V0::Models::CheckoutForm)
215
- r = @client.request("/#{CGI.escape(organization)}/experiences/checkout").with_json(checkout_form.to_json).post
216
- ::Io::Flow::Experience::V0::Models::Checkout.new(r)
217
- end
218
-
219
- # Returns information about a specific experience.
220
- def get_by_key(organization, key)
221
- HttpClient::Preconditions.assert_class('organization', organization, String)
222
- HttpClient::Preconditions.assert_class('key', key, String)
223
- r = @client.request("/#{CGI.escape(organization)}/experiences/#{CGI.escape(key)}").get
224
- ::Io::Flow::Experience::V0::Models::Experience.new(r)
225
- end
226
-
227
- # Add experience
228
- def post(organization, experience_form)
229
- HttpClient::Preconditions.assert_class('organization', organization, String)
230
- HttpClient::Preconditions.assert_class('experience_form', experience_form, ::Io::Flow::Experience::V0::Models::ExperienceForm)
231
- r = @client.request("/#{CGI.escape(organization)}/experiences").with_json(experience_form.to_json).post
232
- ::Io::Flow::Experience::V0::Models::Experience.new(r)
233
- end
234
-
235
- # Update experience with the specified key, creating if it does not exist.
236
- def put_by_key(organization, key, experience_form)
237
- HttpClient::Preconditions.assert_class('organization', organization, String)
238
- HttpClient::Preconditions.assert_class('key', key, String)
239
- HttpClient::Preconditions.assert_class('experience_form', experience_form, ::Io::Flow::Experience::V0::Models::ExperienceForm)
240
- r = @client.request("/#{CGI.escape(organization)}/experiences/#{CGI.escape(key)}").with_json(experience_form.to_json).put
241
- ::Io::Flow::Experience::V0::Models::Experience.new(r)
242
- end
243
-
244
- # Remove the experience display with this key
245
- def put_displays_by_key_and_display_key(organization, key, display_key, hash)
246
- HttpClient::Preconditions.assert_class('organization', organization, String)
247
- HttpClient::Preconditions.assert_class('key', key, String)
248
- HttpClient::Preconditions.assert_class('display_key', display_key, String)
249
- HttpClient::Preconditions.assert_class('hash', hash, Hash)
250
- r = @client.request("/#{CGI.escape(organization)}/experiences/#{CGI.escape(key)}/displays/#{CGI.escape(display_key)}").with_json(hash.to_json).put
251
- ::Io::Flow::Experience::V0::Models::Experience.new(r)
252
- end
253
-
254
- # Delete the experience with this key
255
- def delete_by_key(organization, key)
256
- HttpClient::Preconditions.assert_class('organization', organization, String)
257
- HttpClient::Preconditions.assert_class('key', key, String)
258
- r = @client.request("/#{CGI.escape(organization)}/experiences/#{CGI.escape(key)}").delete
259
- nil
260
- end
261
-
262
- end
263
-
264
- end
265
-
266
- module Models
267
-
268
- # Representation of information indicating landed cost and how it was derived
269
- class Checkout
270
-
271
- attr_reader :id, :deliveries, :prices, :total
272
-
273
- def initialize(incoming={})
274
- opts = HttpClient::Helper.symbolize_keys(incoming)
275
- HttpClient::Preconditions.require_keys(opts, [:id, :deliveries, :prices, :total], 'Checkout')
276
- @id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
277
- @deliveries = HttpClient::Preconditions.assert_class('deliveries', opts.delete(:deliveries), Array).map { |v| (x = v; x.is_a?(::Io::Flow::Fulfillment::V0::Models::Delivery) ? x : ::Io::Flow::Fulfillment::V0::Models::Delivery.new(x)) }
278
- @prices = HttpClient::Preconditions.assert_class('prices', opts.delete(:prices), Array).map { |v| (x = v; x.is_a?(::Io::Flow::Catalog::V0::Models::LocalizedPrice) ? x : ::Io::Flow::Catalog::V0::Models::LocalizedPrice.new(x)) }
279
- @total = (x = opts.delete(:total); x.is_a?(::Io::Flow::Catalog::V0::Models::LocalizedPrice) ? x : ::Io::Flow::Catalog::V0::Models::LocalizedPrice.new(x))
280
- end
281
-
282
- def to_json
283
- JSON.dump(to_hash)
284
- end
285
-
286
- def copy(incoming={})
287
- Checkout.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
288
- end
289
-
290
- def to_hash
291
- {
292
- :id => id,
293
- :deliveries => deliveries.map { |o| o.to_hash },
294
- :prices => prices.map { |o| o.to_hash },
295
- :total => total.to_hash
296
- }
297
- end
298
-
299
- end
300
-
301
- class CheckoutForm
302
-
303
- attr_reader :destination, :items
304
-
305
- def initialize(incoming={})
306
- opts = HttpClient::Helper.symbolize_keys(incoming)
307
- HttpClient::Preconditions.require_keys(opts, [:destination, :items], 'CheckoutForm')
308
- @destination = (x = opts.delete(:destination); x.is_a?(::Io::Flow::Common::V0::Models::Location) ? x : ::Io::Flow::Common::V0::Models::Location.new(x))
309
- @items = HttpClient::Preconditions.assert_class('items', opts.delete(:items), Array).map { |v| (x = v; x.is_a?(::Io::Flow::Experience::V0::Models::Line) ? x : ::Io::Flow::Experience::V0::Models::Line.new(x)) }
310
- end
311
-
312
- def to_json
313
- JSON.dump(to_hash)
314
- end
315
-
316
- def copy(incoming={})
317
- CheckoutForm.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
318
- end
319
-
320
- def to_hash
321
- {
322
- :destination => destination.to_hash,
323
- :items => items.map { |o| o.to_hash }
324
- }
325
- end
326
-
327
- end
328
-
329
- class Component
330
-
331
- attr_reader :adjustments, :levies
332
-
333
- def initialize(incoming={})
334
- opts = HttpClient::Helper.symbolize_keys(incoming)
335
- HttpClient::Preconditions.require_keys(opts, [:adjustments, :levies], 'Component')
336
- @adjustments = HttpClient::Preconditions.assert_class('adjustments', opts.delete(:adjustments), Array).map { |v| (x = v; x.is_a?(::Io::Flow::Catalog::V0::Models::AdjustmentType) ? x : ::Io::Flow::Catalog::V0::Models::AdjustmentType.apply(x)) }
337
- @levies = HttpClient::Preconditions.assert_class('levies', opts.delete(:levies), Array).map { |v| (x = v; x.is_a?(::Io::Flow::Catalog::V0::Models::Levy) ? x : ::Io::Flow::Catalog::V0::Models::Levy.apply(x)) }
338
- end
339
-
340
- def to_json
341
- JSON.dump(to_hash)
342
- end
343
-
344
- def copy(incoming={})
345
- Component.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
346
- end
347
-
348
- def to_hash
349
- {
350
- :adjustments => adjustments.map { |o| o.value },
351
- :levies => levies.map { |o| o.value }
352
- }
353
- end
354
-
355
- end
356
-
357
- # Defines elements of a localized cost available for display (e.g on a localized
358
- # product page or checkout)
359
- class Display
360
-
361
- attr_reader :id, :key, :name, :context, :component
362
-
363
- def initialize(incoming={})
364
- opts = HttpClient::Helper.symbolize_keys(incoming)
365
- HttpClient::Preconditions.require_keys(opts, [:id, :key, :name, :context, :component], 'Display')
366
- @id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
367
- @key = HttpClient::Preconditions.assert_class('key', opts.delete(:key), String)
368
- @name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String)
369
- @context = HttpClient::Preconditions.assert_class('context', opts.delete(:context), Array).map { |v| HttpClient::Preconditions.assert_class('context', v, String) }
370
- @component = (x = opts.delete(:component); x.is_a?(::Io::Flow::Experience::V0::Models::Component) ? x : ::Io::Flow::Experience::V0::Models::Component.new(x))
371
- end
372
-
373
- def to_json
374
- JSON.dump(to_hash)
375
- end
376
-
377
- def copy(incoming={})
378
- Display.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
379
- end
380
-
381
- def to_hash
382
- {
383
- :id => id,
384
- :key => key,
385
- :name => name,
386
- :context => context,
387
- :component => component.to_hash
388
- }
389
- end
390
-
391
- end
392
-
393
- class DisplayForm
394
-
395
- attr_reader :name, :component, :context
396
-
397
- def initialize(incoming={})
398
- opts = HttpClient::Helper.symbolize_keys(incoming)
399
- HttpClient::Preconditions.require_keys(opts, [:name, :component], 'DisplayForm')
400
- @name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String)
401
- @component = (x = opts.delete(:component); x.is_a?(::Io::Flow::Experience::V0::Models::Component) ? x : ::Io::Flow::Experience::V0::Models::Component.new(x))
402
- @context = (x = opts.delete(:context); x.nil? ? nil : HttpClient::Preconditions.assert_class('context', x, Array).map { |v| HttpClient::Preconditions.assert_class('context', v, String) })
403
- end
404
-
405
- def to_json
406
- JSON.dump(to_hash)
407
- end
408
-
409
- def copy(incoming={})
410
- DisplayForm.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
411
- end
412
-
413
- def to_hash
414
- {
415
- :name => name,
416
- :component => component.to_hash,
417
- :context => context.nil? ? nil : context
418
- }
419
- end
420
-
421
- end
422
-
423
- class DisplaySummary
424
-
425
- attr_reader :id, :key
426
-
427
- def initialize(incoming={})
428
- opts = HttpClient::Helper.symbolize_keys(incoming)
429
- HttpClient::Preconditions.require_keys(opts, [:id, :key], 'DisplaySummary')
430
- @id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
431
- @key = HttpClient::Preconditions.assert_class('key', opts.delete(:key), String)
432
- end
433
-
434
- def to_json
435
- JSON.dump(to_hash)
436
- end
437
-
438
- def copy(incoming={})
439
- DisplaySummary.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
440
- end
441
-
442
- def to_hash
443
- {
444
- :id => id,
445
- :key => key
446
- }
447
- end
448
-
449
- end
450
-
451
- class DisplayVersion
452
-
453
- attr_reader :id, :timestamp, :type, :display
454
-
455
- def initialize(incoming={})
456
- opts = HttpClient::Helper.symbolize_keys(incoming)
457
- HttpClient::Preconditions.require_keys(opts, [:id, :timestamp, :type, :display], 'DisplayVersion')
458
- @id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
459
- @timestamp = HttpClient::Preconditions.assert_class('timestamp', HttpClient::Helper.to_date_time_iso8601(opts.delete(:timestamp)), DateTime)
460
- @type = (x = opts.delete(:type); x.is_a?(::Io::Flow::Common::V0::Models::ChangeType) ? x : ::Io::Flow::Common::V0::Models::ChangeType.apply(x))
461
- @display = (x = opts.delete(:display); x.is_a?(::Io::Flow::Experience::V0::Models::Display) ? x : ::Io::Flow::Experience::V0::Models::Display.new(x))
462
- end
463
-
464
- def to_json
465
- JSON.dump(to_hash)
466
- end
467
-
468
- def copy(incoming={})
469
- DisplayVersion.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
470
- end
471
-
472
- def to_hash
473
- {
474
- :id => id,
475
- :timestamp => timestamp,
476
- :type => type.value,
477
- :display => display.to_hash
478
- }
479
- end
480
-
481
- end
482
-
483
- # Properties that define a customized local experience for a given country
484
- class Experience
485
-
486
- attr_reader :id, :key, :country, :currency, :displays, :language, :measurement_system, :name, :subcatalog_id
487
-
488
- def initialize(incoming={})
489
- opts = HttpClient::Helper.symbolize_keys(incoming)
490
- HttpClient::Preconditions.require_keys(opts, [:id, :key, :country, :currency, :displays, :language, :measurement_system, :name, :subcatalog_id], 'Experience')
491
- @id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
492
- @key = HttpClient::Preconditions.assert_class('key', opts.delete(:key), String)
493
- @country = HttpClient::Preconditions.assert_class('country', opts.delete(:country), String)
494
- @currency = HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String)
495
- @displays = HttpClient::Preconditions.assert_class('displays', opts.delete(:displays), Array).map { |v| (x = v; x.is_a?(::Io::Flow::Experience::V0::Models::DisplaySummary) ? x : ::Io::Flow::Experience::V0::Models::DisplaySummary.new(x)) }
496
- @language = HttpClient::Preconditions.assert_class('language', opts.delete(:language), String)
497
- @measurement_system = HttpClient::Preconditions.assert_class('measurement_system', opts.delete(:measurement_system), String)
498
- @name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String)
499
- @subcatalog_id = HttpClient::Preconditions.assert_class('subcatalog_id', opts.delete(:subcatalog_id), String)
500
- end
501
-
502
- def to_json
503
- JSON.dump(to_hash)
504
- end
505
-
506
- def copy(incoming={})
507
- Experience.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
508
- end
509
-
510
- def to_hash
511
- {
512
- :id => id,
513
- :key => key,
514
- :country => country,
515
- :currency => currency,
516
- :displays => displays.map { |o| o.to_hash },
517
- :language => language,
518
- :measurement_system => measurement_system,
519
- :name => name,
520
- :subcatalog_id => subcatalog_id
521
- }
522
- end
523
-
524
- end
525
-
526
- class ExperienceForm
527
-
528
- attr_reader :country, :name, :currency, :display_key, :language, :measurement_system, :subcatalog_id
529
-
530
- def initialize(incoming={})
531
- opts = HttpClient::Helper.symbolize_keys(incoming)
532
- HttpClient::Preconditions.require_keys(opts, [:country, :name], 'ExperienceForm')
533
- @country = HttpClient::Preconditions.assert_class('country', opts.delete(:country), String)
534
- @name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String)
535
- @currency = (x = opts.delete(:currency); x.nil? ? nil : HttpClient::Preconditions.assert_class('currency', x, String))
536
- @display_key = (x = opts.delete(:display_key); x.nil? ? nil : HttpClient::Preconditions.assert_class('display_key', x, Array).map { |v| HttpClient::Preconditions.assert_class('display_key', v, String) })
537
- @language = (x = opts.delete(:language); x.nil? ? nil : HttpClient::Preconditions.assert_class('language', x, String))
538
- @measurement_system = (x = opts.delete(:measurement_system); x.nil? ? nil : HttpClient::Preconditions.assert_class('measurement_system', x, String))
539
- @subcatalog_id = (x = opts.delete(:subcatalog_id); x.nil? ? nil : HttpClient::Preconditions.assert_class('subcatalog_id', x, String))
540
- end
541
-
542
- def to_json
543
- JSON.dump(to_hash)
544
- end
545
-
546
- def copy(incoming={})
547
- ExperienceForm.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
548
- end
549
-
550
- def to_hash
551
- {
552
- :country => country,
553
- :name => name,
554
- :currency => currency,
555
- :display_key => display_key.nil? ? nil : display_key,
556
- :language => language,
557
- :measurement_system => measurement_system,
558
- :subcatalog_id => subcatalog_id
559
- }
560
- end
561
-
562
- end
563
-
564
- class ExperienceVersion
565
-
566
- attr_reader :id, :timestamp, :type, :experience
567
-
568
- def initialize(incoming={})
569
- opts = HttpClient::Helper.symbolize_keys(incoming)
570
- HttpClient::Preconditions.require_keys(opts, [:id, :timestamp, :type, :experience], 'ExperienceVersion')
571
- @id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
572
- @timestamp = HttpClient::Preconditions.assert_class('timestamp', HttpClient::Helper.to_date_time_iso8601(opts.delete(:timestamp)), DateTime)
573
- @type = (x = opts.delete(:type); x.is_a?(::Io::Flow::Common::V0::Models::ChangeType) ? x : ::Io::Flow::Common::V0::Models::ChangeType.apply(x))
574
- @experience = (x = opts.delete(:experience); x.is_a?(::Io::Flow::Experience::V0::Models::Experience) ? x : ::Io::Flow::Experience::V0::Models::Experience.new(x))
575
- end
576
-
577
- def to_json
578
- JSON.dump(to_hash)
579
- end
580
-
581
- def copy(incoming={})
582
- ExperienceVersion.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
583
- end
584
-
585
- def to_hash
586
- {
587
- :id => id,
588
- :timestamp => timestamp,
589
- :type => type.value,
590
- :experience => experience.to_hash
591
- }
592
- end
593
-
594
- end
595
-
596
- # Represents an order line for checkout
597
- class Line
598
-
599
- attr_reader :number, :quantity
600
-
601
- def initialize(incoming={})
602
- opts = HttpClient::Helper.symbolize_keys(incoming)
603
- HttpClient::Preconditions.require_keys(opts, [:number, :quantity], 'Line')
604
- @number = HttpClient::Preconditions.assert_class('number', opts.delete(:number), String)
605
- @quantity = HttpClient::Preconditions.assert_class('quantity', opts.delete(:quantity), Integer)
606
- end
607
-
608
- def to_json
609
- JSON.dump(to_hash)
610
- end
611
-
612
- def copy(incoming={})
613
- Line.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
614
- end
615
-
616
- def to_hash
617
- {
618
- :number => number,
619
- :quantity => quantity
620
- }
621
- end
622
-
623
- end
624
-
625
- end
626
-
627
- # ===== END OF SERVICE DEFINITION =====
628
- module HttpClient
629
-
630
- class Request
631
-
632
- def initialize(uri)
633
- @uri = Preconditions.assert_class('uri', uri, URI)
634
- @params = nil
635
- @body = nil
636
- @auth = nil
637
- @headers = {}
638
- @header_keys_lower_case = []
639
- end
640
-
641
- def with_header(name, value)
642
- Preconditions.check_not_blank('name', name, "Header name is required")
643
- Preconditions.check_not_blank('value', value, "Header value is required")
644
- Preconditions.check_state(!@headers.has_key?(name),
645
- "Duplicate header named[%s]" % name)
646
- @headers[name] = value
647
- @header_keys_lower_case << name.downcase
648
- self
649
- end
650
-
651
- def with_auth(auth)
652
- Preconditions.assert_class('auth', auth, HttpClient::Authorization)
653
- Preconditions.check_state(@auth.nil?, "auth previously set")
654
-
655
- if auth.scheme.name == AuthScheme::BASIC.name
656
- @auth = auth
657
- else
658
- raise "Auth Scheme[#{auth.scheme.name}] not supported"
659
- end
660
- self
661
- end
662
-
663
- def with_query(params)
664
- Preconditions.assert_class('params', params, Hash)
665
- Preconditions.check_state(@params.nil?, "Already have query parameters")
666
- @params = params
667
- self
668
- end
669
-
670
- # Wrapper to set Content-Type header to application/json and set
671
- # the provided json document as the body
672
- def with_json(json)
673
- @headers['Content-Type'] ||= 'application/json; charset=UTF-8'
674
- with_body(json)
675
- end
676
-
677
- def with_body(body)
678
- Preconditions.check_not_blank('body', body)
679
- @body = body
680
- self
681
- end
682
-
683
- # Creates a new Net:HTTP client. The client returned should be
684
- # fully configured to make a request.
685
- def new_http_client
686
- client = Net::HTTP.new(@uri.host, @uri.port)
687
- if @uri.scheme == "https"
688
- configure_ssl(client)
689
- end
690
- client
691
- end
692
-
693
- # If HTTPS is required, this method accepts an HTTP Client and configures SSL
694
- def configure_ssl(http)
695
- Preconditions.assert_class('http', http, Net::HTTP)
696
- http.use_ssl = true
697
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
698
- http.cert_store = OpenSSL::X509::Store.new
699
- http.cert_store.set_default_paths
700
- end
701
-
702
- def get(&block)
703
- do_request(Net::HTTP::Get, &block)
704
- end
705
-
706
- def delete(&block)
707
- do_request(Net::HTTP::Delete, &block)
708
- end
709
-
710
- def options(&block)
711
- do_request(Net::HTTP::Options, &block)
712
- end
713
-
714
- def post(&block)
715
- do_request(Net::HTTP::Post, &block)
716
- end
717
-
718
- def put(&block)
719
- do_request(Net::HTTP::Put, &block)
720
- end
721
-
722
- class PATCH < Net::HTTP::Put
723
- METHOD = "PATCH"
724
- end
725
-
726
- def patch(&block)
727
- do_request(PATCH, &block)
728
- end
729
-
730
- def do_request(klass)
731
- Preconditions.assert_class('klass', klass, Class)
732
-
733
- uri = @uri.to_s
734
- if q = to_query(@params)
735
- uri += "?%s" % q
736
- end
737
-
738
- request = klass.send(:new, uri)
739
-
740
- curl = ['curl']
741
- if klass != Net::HTTP::Get
742
- curl << "-X%s" % klass.name.split("::").last.upcase
743
- end
744
-
745
- if @body
746
- # DEBUG path = "/tmp/rest_client.tmp"
747
- # DEBUG File.open(path, "w") { |os| os << @body.to_s }
748
- # DEBUG curl << "-d@%s" % path
749
- request.body = @body
750
- end
751
-
752
- if @auth
753
- curl << "-u \"%s:%s\"" % [@auth.username, @auth.password]
754
- Preconditions.check_state(!@header_keys_lower_case.include?("authorization"),
755
- "Cannot specify both an Authorization header and an auth instance")
756
- user_pass = "%s:%s" % [@auth.username, @auth.password]
757
- encoded = Base64.encode64(user_pass).to_s.split("\n").map(&:strip).join
758
- request.add_field("Authorization", "Basic %s" % encoded)
759
- end
760
-
761
- @headers.each { |key, value|
762
- curl << "-H \"%s: %s\"" % [key, value]
763
- request.add_field(key, value)
764
- }
765
-
766
- curl << "'%s'" % uri
767
- # DEBUG puts curl.join(" ")
768
-
769
- raw_response = http_request(request)
770
- response = raw_response.to_s == "" ? nil : JSON.parse(raw_response)
771
-
772
- if block_given?
773
- yield response
774
- else
775
- response
776
- end
777
- end
778
-
779
- private
780
- def to_query(params={})
781
- parts = (params || {}).map { |k,v|
782
- if v.respond_to?(:each)
783
- v.map { |el| "%s=%s" % [k, CGI.escape(el.to_s)] }
784
- else
785
- "%s=%s" % [k, CGI.escape(v.to_s)]
786
- end
787
- }
788
- parts.empty? ? nil : parts.join("&")
789
- end
790
-
791
- def http_request(request)
792
- response = begin
793
- new_http_client.request(request)
794
- rescue SocketError => e
795
- raise Exception.new("Error accessing uri[#{@uri}]: #{e}")
796
- end
797
-
798
- case response
799
- when Net::HTTPSuccess
800
- response.body
801
- else
802
- body = response.body rescue nil
803
- raise HttpClient::ServerError.new(response.code.to_i, response.message, :body => body, :uri => @uri.to_s)
804
- end
805
- end
806
- end
807
-
808
- class ServerError < StandardError
809
-
810
- attr_reader :code, :details, :body, :uri
811
-
812
- def initialize(code, details, incoming={})
813
- opts = HttpClient::Helper.symbolize_keys(incoming)
814
- @code = HttpClient::Preconditions.assert_class('code', code, Integer)
815
- @details = HttpClient::Preconditions.assert_class('details', details, String)
816
- @body = HttpClient::Preconditions.assert_class_or_nil('body', opts.delete(:body), String)
817
- @uri = HttpClient::Preconditions.assert_class_or_nil('uri', opts.delete(:uri), String)
818
- HttpClient::Preconditions.assert_empty_opts(opts)
819
- super(self.message)
820
- end
821
-
822
- def message
823
- m = "%s %s" % [@code, @details]
824
- if @body
825
- m << ": %s" % @body
826
- end
827
- m
828
- end
829
-
830
- def body_json
831
- JSON.parse(@body)
832
- end
833
-
834
- end
835
-
836
- class PreconditionException < Exception
837
-
838
- attr_reader :message
839
-
840
- def initialize(message)
841
- super(message)
842
- @message = message
843
- end
844
-
845
- end
846
-
847
- module Preconditions
848
-
849
- def Preconditions.check_argument(expression, error_message=nil)
850
- if !expression
851
- raise PreconditionException.new(error_message || "check_argument failed")
852
- end
853
- nil
854
- end
855
-
856
- def Preconditions.check_state(expression, error_message=nil)
857
- if !expression
858
- raise PreconditionException.new(error_message || "check_state failed")
859
- end
860
- nil
861
- end
862
-
863
- def Preconditions.check_not_nil(field_name, reference, error_message=nil)
864
- if reference.nil?
865
- raise PreconditionException.new(error_message || "argument for %s cannot be nil" % field_name)
866
- end
867
- reference
868
- end
869
-
870
- def Preconditions.check_not_blank(field_name, reference, error_message=nil)
871
- if reference.to_s.strip == ""
872
- raise PreconditionException.new(error_message || "argument for %s cannot be blank" % field_name)
873
- end
874
- reference
875
- end
876
-
877
- # Throws an error if opts is not empty. Useful when parsing
878
- # arguments to a function
879
- def Preconditions.assert_empty_opts(opts)
880
- if !opts.empty?
881
- raise PreconditionException.new("Invalid opts: #{opts.keys.inspect}\n#{opts.inspect}")
882
- end
883
- end
884
-
885
- # Requires that the provided hash has the specified keys.
886
- # @param fields A list of symbols
887
- def Preconditions.require_keys(hash, fields, error_prefix=nil)
888
- missing = fields.select { |f| !hash.has_key?(f) }
889
- if !missing.empty?
890
- msg = "Missing required fields: " + missing.join(", ")
891
- raise PreconditionException.new(error_prefix.empty? ? msg : "#{error_prefix}: #{msg}")
892
- end
893
- end
894
-
895
- # Asserts that value is not nill and is_?(klass). Returns
896
- # value. Common use is
897
- #
898
- # amount = Preconditions.assert_class('amount', amount, BigDecimal)
899
- def Preconditions.assert_class(field_name, value, klass)
900
- Preconditions.check_not_nil('field_name', field_name)
901
- Preconditions.check_not_nil('klass', klass)
902
- Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of class %s" % [field_name, klass.name])
903
- Preconditions.check_state(value.is_a?(klass),
904
- "Value for #{field_name} is of type[#{value.class}] - class[#{klass}] is required. value[#{value.inspect.to_s}]")
905
- value
906
- end
907
-
908
- def Preconditions.assert_class_or_nil(field_name, value, klass)
909
- if !value.nil?
910
- Preconditions.assert_class(field_name, value, klass)
911
- end
912
- end
913
-
914
- def Preconditions.assert_boolean(field_name, value)
915
- Preconditions.check_not_nil('field_name', field_name)
916
- Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of TrueClass or FalseClass" % field_name)
917
- Preconditions.check_state(value.is_a?(TrueClass) || value.is_a?(FalseClass),
918
- "Value for #{field_name} is of type[#{value.class}] - class[TrueClass or FalseClass] is required. value[#{value.inspect.to_s}]")
919
- value
920
- end
921
-
922
- def Preconditions.assert_boolean_or_nil(field_name, value)
923
- if !value.nil?
924
- Preconditions.assert_boolean(field_name, value)
925
- end
926
- end
927
-
928
- def Preconditions.assert_collection_of_class(field_name, values, klass)
929
- Preconditions.assert_class(field_name, values, Array)
930
- values.each { |v| Preconditions.assert_class(field_name, v, klass) }
931
- end
932
-
933
- def Preconditions.assert_hash_of_class(field_name, hash, klass)
934
- Preconditions.assert_class(field_name, hash, Hash)
935
- values.each { |k, v| Preconditions.assert_class(field_name, v, klass) }
936
- end
937
-
938
- end
939
-
940
- class AuthScheme
941
-
942
- attr_reader :name
943
-
944
- def initialize(name)
945
- @name = HttpClient::Preconditions.check_not_blank('name', name)
946
- end
947
-
948
- BASIC = AuthScheme.new("basic") unless defined?(BASIC)
949
-
950
- end
951
-
952
- class Authorization
953
-
954
- attr_reader :scheme, :username, :password
955
-
956
- def initialize(scheme, username, opts={})
957
- @scheme = HttpClient::Preconditions.assert_class('schema', scheme, AuthScheme)
958
- @username = HttpClient::Preconditions.check_not_blank('username', username, "username is required")
959
- @password = HttpClient::Preconditions.assert_class_or_nil('password', opts.delete(:password), String)
960
- HttpClient::Preconditions.assert_empty_opts(opts)
961
- end
962
-
963
- def Authorization.basic(username, password=nil)
964
- Authorization.new(AuthScheme::BASIC, username, :password => password)
965
- end
966
-
967
- end
968
-
969
- module Helper
970
-
971
- def Helper.symbolize_keys(hash)
972
- Preconditions.assert_class('hash', hash, Hash)
973
- new_hash = {}
974
- hash.each { |k, v|
975
- new_hash[k.to_sym] = v
976
- }
977
- new_hash
978
- end
979
-
980
- def Helper.to_big_decimal(value)
981
- value ? BigDecimal.new(value.to_s) : nil
982
- end
983
-
984
- def Helper.to_object(value)
985
- value ? JSON.parse(value) : nil
986
- end
987
-
988
- def Helper.to_uuid(value)
989
- Preconditions.check_state(value.nil? || value.match(/^\w\w\w\w\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\w\w\w\w\w\w\w\w$/),
990
- "Invalid guid[%s]" % value)
991
- value
992
- end
993
-
994
- def Helper.to_date_iso8601(value)
995
- if value.is_a?(Date)
996
- value
997
- elsif value
998
- Date.parse(value.to_s)
999
- else
1000
- nil
1001
- end
1002
- end
1003
-
1004
- def Helper.to_date_time_iso8601(value)
1005
- if value.is_a?(DateTime)
1006
- value
1007
- elsif value
1008
- DateTime.parse(value.to_s)
1009
- else
1010
- nil
1011
- end
1012
- end
1013
-
1014
- def Helper.date_iso8601_to_string(value)
1015
- value.nil? ? nil : value.strftime('%Y-%m-%d')
1016
- end
1017
-
1018
- def Helper.date_time_iso8601_to_string(value)
1019
- value.nil? ? nil : value.strftime('%Y-%m-%dT%H:%M:%S%z')
1020
- end
1021
-
1022
- TRUE_STRINGS = ['t', 'true', 'y', 'yes', 'on', '1', 'trueclass'] unless defined?(TRUE_STRINGS)
1023
- FALSE_STRINGS = ['f', 'false', 'n', 'no', 'off', '0', 'falseclass'] unless defined?(FALSE_STRINGS)
1024
-
1025
- def Helper.to_boolean(field_name, value)
1026
- string = value.to_s.strip.downcase
1027
- if TRUE_STRINGS.include?(string)
1028
- true
1029
- elsif FALSE_STRINGS.include?(string)
1030
- false
1031
- elsif string != ""
1032
- raise PreconditionException.new("Unsupported boolean value[#{string}]. For true, must be one of: #{TRUE_STRINGS.inspect}. For false, must be one of: #{FALSE_STRINGS.inspect}")
1033
- else
1034
- nil
1035
- end
1036
- end
1037
-
1038
- end
1039
-
1040
- end
1041
- end
1042
- end
1043
- end
1044
- end