flowcommerce 0.0.10 → 0.0.11

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