zaius 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +28 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/zaius/api_operations/list.rb +14 -0
- data/lib/zaius/api_operations/request.rb +41 -0
- data/lib/zaius/api_resource.rb +63 -0
- data/lib/zaius/customer.rb +9 -0
- data/lib/zaius/errors.rb +44 -0
- data/lib/zaius/list.rb +7 -0
- data/lib/zaius/list_object.rb +56 -0
- data/lib/zaius/subscription.rb +59 -0
- data/lib/zaius/util.rb +304 -0
- data/lib/zaius/version.rb +3 -0
- data/lib/zaius/zaius_client.rb +273 -0
- data/lib/zaius/zaius_object.rb +472 -0
- data/lib/zaius/zaius_response.rb +45 -0
- data/lib/zaius.rb +76 -0
- data/zaius.gemspec +29 -0
- metadata +138 -0
@@ -0,0 +1,472 @@
|
|
1
|
+
module Zaius
|
2
|
+
class ZaiusObject
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
@@permanent_attributes = Set.new([:id])
|
6
|
+
|
7
|
+
def initialize(id = nil, opts = {})
|
8
|
+
id, @retrieve_params = Util.normalize_id(id)
|
9
|
+
@opts = Util.normalize_opts(opts)
|
10
|
+
@original_values = {}
|
11
|
+
@values = {}
|
12
|
+
# This really belongs in APIResource, but not putting it there allows us
|
13
|
+
# to have a unified inspect method
|
14
|
+
@unsaved_values = Set.new
|
15
|
+
@transient_values = Set.new
|
16
|
+
@values[:id] = id if id
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.construct_from(values, opts = {})
|
20
|
+
values = Zaius::Util.symbolize_names(values)
|
21
|
+
|
22
|
+
# work around protected #initialize_from for now
|
23
|
+
new(values[:id]).send(:initialize_from, values, opts)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Determines the equality of two Zaius objects. Zaius objects are
|
27
|
+
# considered to be equal if they have the same set of values and each one
|
28
|
+
# of those values is the same.
|
29
|
+
def ==(other)
|
30
|
+
other.is_a?(ZaiusObject) && @values == other.instance_variable_get(:@values)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Indicates whether or not the resource has been deleted on the server.
|
34
|
+
# Note that some, but not all, resources can indicate whether they have
|
35
|
+
# been deleted.
|
36
|
+
def deleted?
|
37
|
+
@values.fetch(:deleted, false)
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_s(*_args)
|
41
|
+
JSON.pretty_generate(to_hash)
|
42
|
+
end
|
43
|
+
|
44
|
+
def inspect
|
45
|
+
id_string = respond_to?(:id) && !id.nil? ? " id=#{id}" : ""
|
46
|
+
"#<#{self.class}:0x#{object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Re-initializes the object based on a hash of values (usually one that's
|
50
|
+
# come back from an API call). Adds or removes value accessors as necessary
|
51
|
+
# and updates the state of internal data.
|
52
|
+
#
|
53
|
+
# Please don't use this method. If you're trying to do mass assignment, try
|
54
|
+
# #initialize_from instead.
|
55
|
+
def refresh_from(values, opts, partial = false)
|
56
|
+
initialize_from(values, opts, partial)
|
57
|
+
end
|
58
|
+
extend Gem::Deprecate
|
59
|
+
deprecate :refresh_from, "#update_attributes", 2016, 1
|
60
|
+
|
61
|
+
# Mass assigns attributes on the model.
|
62
|
+
#
|
63
|
+
# This is a version of +update_attributes+ that takes some extra options
|
64
|
+
# for internal use.
|
65
|
+
#
|
66
|
+
# ==== Attributes
|
67
|
+
#
|
68
|
+
# * +values+ - Hash of values to use to update the current attributes of
|
69
|
+
# the object.
|
70
|
+
# * +opts+ - Options for +ZaiusObject+ like an API key that will be reused
|
71
|
+
# on subsequent API calls.
|
72
|
+
#
|
73
|
+
# ==== Options
|
74
|
+
#
|
75
|
+
# * +:dirty+ - Whether values should be initiated as "dirty" (unsaved) and
|
76
|
+
# which applies only to new ZaiusObjects being initiated under this
|
77
|
+
# ZaiusObject. Defaults to true.
|
78
|
+
def update_attributes(values, opts = {}, method_options = {})
|
79
|
+
# Default to true. TODO: Convert to optional arguments after we're off
|
80
|
+
# 1.9 which will make this quite a bit more clear.
|
81
|
+
dirty = method_options.fetch(:dirty, true)
|
82
|
+
values.each do |k, v|
|
83
|
+
add_accessors([k], values) unless metaclass.method_defined?(k.to_sym)
|
84
|
+
@values[k] = Util.convert_to_zaius_object(v, opts)
|
85
|
+
dirty_value!(@values[k]) if dirty
|
86
|
+
@unsaved_values.add(k)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def [](k)
|
91
|
+
@values[k.to_sym]
|
92
|
+
end
|
93
|
+
|
94
|
+
def []=(k, v)
|
95
|
+
send(:"#{k}=", v)
|
96
|
+
end
|
97
|
+
|
98
|
+
def keys
|
99
|
+
@values.keys
|
100
|
+
end
|
101
|
+
|
102
|
+
def values
|
103
|
+
@values.values
|
104
|
+
end
|
105
|
+
|
106
|
+
def to_json(*_a)
|
107
|
+
JSON.generate(@values)
|
108
|
+
end
|
109
|
+
|
110
|
+
def as_json(*a)
|
111
|
+
@values.as_json(*a)
|
112
|
+
end
|
113
|
+
|
114
|
+
def to_hash
|
115
|
+
maybe_to_hash = lambda do |value|
|
116
|
+
value.respond_to?(:to_hash) ? value.to_hash : value
|
117
|
+
end
|
118
|
+
|
119
|
+
@values.each_with_object({}) do |(key, value), acc|
|
120
|
+
acc[key] = case value
|
121
|
+
when Array
|
122
|
+
value.map(&maybe_to_hash)
|
123
|
+
else
|
124
|
+
maybe_to_hash.call(value)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def each(&blk)
|
130
|
+
@values.each(&blk)
|
131
|
+
end
|
132
|
+
|
133
|
+
# Sets all keys within the ZaiusObject as unsaved so that they will be
|
134
|
+
# included with an update when #serialize_params is called. This method is
|
135
|
+
# also recursive, so any ZaiusObjects contained as values or which are
|
136
|
+
# values in a tenant array are also marked as dirty.
|
137
|
+
def dirty!
|
138
|
+
@unsaved_values = Set.new(@values.keys)
|
139
|
+
@values.each_value do |v|
|
140
|
+
dirty_value!(v)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# Implements custom encoding for Ruby's Marshal. The data produced by this
|
145
|
+
# method should be comprehendable by #marshal_load.
|
146
|
+
#
|
147
|
+
# This allows us to remove certain features that cannot or should not be
|
148
|
+
# serialized.
|
149
|
+
def marshal_dump
|
150
|
+
# The ZaiusClient instance in @opts is not serializable and is not
|
151
|
+
# really a property of the ZaiusObject, so we exclude it when
|
152
|
+
# dumping
|
153
|
+
opts = @opts.clone
|
154
|
+
opts.delete(:client)
|
155
|
+
[@values, opts]
|
156
|
+
end
|
157
|
+
|
158
|
+
# Implements custom decoding for Ruby's Marshal. Consumes data that's
|
159
|
+
# produced by #marshal_dump.
|
160
|
+
def marshal_load(data)
|
161
|
+
values, opts = data
|
162
|
+
initialize(values[:id])
|
163
|
+
initialize_from(values, opts)
|
164
|
+
end
|
165
|
+
|
166
|
+
def serialize_params(options = {})
|
167
|
+
update_hash = {}
|
168
|
+
|
169
|
+
@values.each do |k, v|
|
170
|
+
# There are a few reasons that we may want to add in a parameter for
|
171
|
+
# update:
|
172
|
+
#
|
173
|
+
# 1. The `force` option has been set.
|
174
|
+
# 2. We know that it was modified.
|
175
|
+
# 3. Its value is a ZaiusObject. A ZaiusObject may contain modified
|
176
|
+
# values within in that its parent ZaiusObject doesn't know about.
|
177
|
+
#
|
178
|
+
unsaved = @unsaved_values.include?(k)
|
179
|
+
if options[:force] || unsaved || v.is_a?(ZaiusObject)
|
180
|
+
update_hash[k.to_sym] =
|
181
|
+
serialize_params_value(@values[k], @original_values[k], unsaved, options[:force], key: k)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
# a `nil` that makes it out of `#serialize_params_value` signals an empty
|
186
|
+
# value that we shouldn't appear in the serialized form of the object
|
187
|
+
update_hash.reject! { |_, v| v.nil? }
|
188
|
+
|
189
|
+
update_hash
|
190
|
+
end
|
191
|
+
|
192
|
+
class << self
|
193
|
+
# This class method has been deprecated in favor of the instance method
|
194
|
+
# of the same name.
|
195
|
+
def serialize_params(obj, options = {})
|
196
|
+
obj.serialize_params(options)
|
197
|
+
end
|
198
|
+
extend Gem::Deprecate
|
199
|
+
deprecate :serialize_params, "#serialize_params", 2016, 9
|
200
|
+
end
|
201
|
+
|
202
|
+
# A protected field is one that doesn't get an accessor assigned to it
|
203
|
+
# (i.e. `obj.public = ...`) and one which is not allowed to be updated via
|
204
|
+
# the class level `Model.update(id, { ... })`.
|
205
|
+
def self.protected_fields
|
206
|
+
[]
|
207
|
+
end
|
208
|
+
|
209
|
+
protected
|
210
|
+
|
211
|
+
def metaclass
|
212
|
+
class << self; self; end
|
213
|
+
end
|
214
|
+
|
215
|
+
def remove_accessors(keys)
|
216
|
+
# not available in the #instance_eval below
|
217
|
+
protected_fields = self.class.protected_fields
|
218
|
+
|
219
|
+
metaclass.instance_eval do
|
220
|
+
keys.each do |k|
|
221
|
+
next if protected_fields.include?(k)
|
222
|
+
next if @@permanent_attributes.include?(k)
|
223
|
+
|
224
|
+
# Remove methods for the accessor's reader and writer.
|
225
|
+
[k, :"#{k}=", :"#{k}?"].each do |method_name|
|
226
|
+
remove_method(method_name) if method_defined?(method_name)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
def add_accessors(keys, values)
|
233
|
+
# not available in the #instance_eval below
|
234
|
+
protected_fields = self.class.protected_fields
|
235
|
+
|
236
|
+
metaclass.instance_eval do
|
237
|
+
keys.each do |k|
|
238
|
+
next if protected_fields.include?(k)
|
239
|
+
next if @@permanent_attributes.include?(k)
|
240
|
+
|
241
|
+
if k == :method
|
242
|
+
# Object#method is a built-in Ruby method that accepts a symbol
|
243
|
+
# and returns the corresponding Method object. Because the API may
|
244
|
+
# also use `method` as a field name, we check the arity of *args
|
245
|
+
# to decide whether to act as a getter or call the parent method.
|
246
|
+
define_method(k) { |*args| args.empty? ? @values[k] : super(*args) }
|
247
|
+
else
|
248
|
+
define_method(k) { @values[k] }
|
249
|
+
end
|
250
|
+
|
251
|
+
define_method(:"#{k}=") do |v|
|
252
|
+
if v == ""
|
253
|
+
raise ArgumentError, "You cannot set #{k} to an empty string. " \
|
254
|
+
"We interpret empty strings as nil in requests. " \
|
255
|
+
"You may set (object).#{k} = nil to delete the property."
|
256
|
+
end
|
257
|
+
@values[k] = Util.convert_to_zaius_object(v, @opts)
|
258
|
+
dirty_value!(@values[k])
|
259
|
+
@unsaved_values.add(k)
|
260
|
+
end
|
261
|
+
|
262
|
+
if [FalseClass, TrueClass].include?(values[k].class)
|
263
|
+
define_method(:"#{k}?") { @values[k] }
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
def method_missing(name, *args)
|
270
|
+
# TODO: only allow setting in updateable classes.
|
271
|
+
if name.to_s.end_with?("=")
|
272
|
+
attr = name.to_s[0...-1].to_sym
|
273
|
+
|
274
|
+
# Pull out the assigned value. This is only used in the case of a
|
275
|
+
# boolean value to add a question mark accessor (i.e. `foo?`) for
|
276
|
+
# convenience.
|
277
|
+
val = args.first
|
278
|
+
|
279
|
+
# the second argument is only required when adding boolean accessors
|
280
|
+
add_accessors([attr], attr => val)
|
281
|
+
|
282
|
+
begin
|
283
|
+
mth = method(name)
|
284
|
+
rescue NameError
|
285
|
+
raise NoMethodError, "Cannot set #{attr} on this object. HINT: you can't set: #{@@permanent_attributes.to_a.join(', ')}"
|
286
|
+
end
|
287
|
+
return mth.call(args[0])
|
288
|
+
elsif @values.key?(name)
|
289
|
+
return @values[name]
|
290
|
+
end
|
291
|
+
|
292
|
+
begin
|
293
|
+
super
|
294
|
+
rescue NoMethodError => e
|
295
|
+
# If we notice the accessed name if our set of transient values we can
|
296
|
+
# give the user a slightly more helpful error message. If not, just
|
297
|
+
# raise right away.
|
298
|
+
raise unless @transient_values.include?(name)
|
299
|
+
|
300
|
+
raise NoMethodError, e.message + ". HINT: The '#{name}' attribute was set in the past, however. It was then wiped when refreshing the object with the result returned by Zaius's API, probably as a result of a save(). The attributes currently available on this object are: #{@values.keys.join(', ')}"
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
def respond_to_missing?(symbol, include_private = false)
|
305
|
+
@values && @values.key?(symbol) || super
|
306
|
+
end
|
307
|
+
|
308
|
+
# Re-initializes the object based on a hash of values (usually one that's
|
309
|
+
# come back from an API call). Adds or removes value accessors as necessary
|
310
|
+
# and updates the state of internal data.
|
311
|
+
#
|
312
|
+
# Protected on purpose! Please do not expose.
|
313
|
+
#
|
314
|
+
# ==== Options
|
315
|
+
#
|
316
|
+
# * +:values:+ Hash used to update accessors and values.
|
317
|
+
# * +:opts:+ Options for ZaiusObject like an API key.
|
318
|
+
# * +:partial:+ Indicates that the re-initialization should not attempt to
|
319
|
+
# remove accessors.
|
320
|
+
def initialize_from(values, opts, partial = false)
|
321
|
+
@opts = Util.normalize_opts(opts)
|
322
|
+
|
323
|
+
# the `#send` is here so that we can keep this method private
|
324
|
+
@original_values = self.class.send(:deep_copy, values)
|
325
|
+
|
326
|
+
removed = partial ? Set.new : Set.new(@values.keys - values.keys)
|
327
|
+
added = Set.new(values.keys - @values.keys)
|
328
|
+
|
329
|
+
# Wipe old state before setting new. This is useful for e.g. updating a
|
330
|
+
# customer, where there is no persistent card parameter. Mark those values
|
331
|
+
# which don't persist as transient
|
332
|
+
|
333
|
+
remove_accessors(removed)
|
334
|
+
add_accessors(added, values)
|
335
|
+
|
336
|
+
removed.each do |k|
|
337
|
+
@values.delete(k)
|
338
|
+
@transient_values.add(k)
|
339
|
+
@unsaved_values.delete(k)
|
340
|
+
end
|
341
|
+
|
342
|
+
update_attributes(values, opts, dirty: false)
|
343
|
+
values.each_key do |k|
|
344
|
+
@transient_values.delete(k)
|
345
|
+
@unsaved_values.delete(k)
|
346
|
+
end
|
347
|
+
|
348
|
+
self
|
349
|
+
end
|
350
|
+
|
351
|
+
def serialize_params_value(value, original, unsaved, force, key: nil)
|
352
|
+
if value.nil?
|
353
|
+
""
|
354
|
+
|
355
|
+
# The logic here is that essentially any object embedded in another
|
356
|
+
# object that had a `type` is actually an API resource of a different
|
357
|
+
# type that's been included in the response. These other resources must
|
358
|
+
# be updated from their proper endpoints, and therefore they are not
|
359
|
+
# included when serializing even if they've been modified.
|
360
|
+
#
|
361
|
+
# There are _some_ known exceptions though.
|
362
|
+
#
|
363
|
+
# For example, if the value is unsaved (meaning the user has set it), and
|
364
|
+
# it looks like the API resource is persisted with an ID, then we include
|
365
|
+
# the object so that parameters are serialized with a reference to its
|
366
|
+
# ID.
|
367
|
+
#
|
368
|
+
# Another example is that on save API calls it's sometimes desirable to
|
369
|
+
# update a customer's default source by setting a new card (or other)
|
370
|
+
# object with `#source=` and then saving the customer. The
|
371
|
+
# `#save_with_parent` flag to override the default behavior allows us to
|
372
|
+
# handle these exceptions.
|
373
|
+
#
|
374
|
+
# We throw an error if a property was set explicitly but we can't do
|
375
|
+
# anything with it because the integration is probably not working as the
|
376
|
+
# user intended it to.
|
377
|
+
elsif value.is_a?(APIResource) && !value.save_with_parent
|
378
|
+
if !unsaved
|
379
|
+
nil
|
380
|
+
elsif value.respond_to?(:id) && !value.id.nil?
|
381
|
+
value
|
382
|
+
else
|
383
|
+
raise ArgumentError, "Cannot save property `#{key}` containing " \
|
384
|
+
"an API resource. It doesn't appear to be persisted and is " \
|
385
|
+
"not marked as `save_with_parent`."
|
386
|
+
end
|
387
|
+
|
388
|
+
elsif value.is_a?(Array)
|
389
|
+
update = value.map { |v| serialize_params_value(v, nil, true, force) }
|
390
|
+
|
391
|
+
# This prevents an array that's unchanged from being resent.
|
392
|
+
update if update != serialize_params_value(original, nil, true, force)
|
393
|
+
|
394
|
+
# Handle a Hash for now, but in the long run we should be able to
|
395
|
+
# eliminate all places where hashes are stored as values internally by
|
396
|
+
# making sure any time one is set, we convert it to a ZaiusObject. This
|
397
|
+
# will simplify our model by making data within an object more
|
398
|
+
# consistent.
|
399
|
+
#
|
400
|
+
# For now, you can still run into a hash if someone appends one to an
|
401
|
+
# existing array being held by a ZaiusObject. This could happen for
|
402
|
+
# example by appending a new hash onto `additional_owners` for an
|
403
|
+
# account.
|
404
|
+
elsif value.is_a?(Hash)
|
405
|
+
Util.convert_to_zaius_object(value, @opts).serialize_params
|
406
|
+
|
407
|
+
elsif value.is_a?(ZaiusObject)
|
408
|
+
update = value.serialize_params(force: force)
|
409
|
+
|
410
|
+
# If the entire object was replaced, then we need blank each field of
|
411
|
+
# the old object that held a value. The new serialized values will
|
412
|
+
# override any of these empty values.
|
413
|
+
update = empty_values(original).merge(update) if original && unsaved
|
414
|
+
|
415
|
+
update
|
416
|
+
|
417
|
+
else
|
418
|
+
value
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
private
|
423
|
+
|
424
|
+
# Produces a deep copy of the given object including support for arrays,
|
425
|
+
# hashes, and ZaiusObjects.
|
426
|
+
def self.deep_copy(obj)
|
427
|
+
case obj
|
428
|
+
when Array
|
429
|
+
obj.map { |e| deep_copy(e) }
|
430
|
+
when Hash
|
431
|
+
obj.each_with_object({}) do |(k, v), copy|
|
432
|
+
copy[k] = deep_copy(v)
|
433
|
+
copy
|
434
|
+
end
|
435
|
+
when ZaiusObject
|
436
|
+
obj.class.construct_from(
|
437
|
+
deep_copy(obj.instance_variable_get(:@values)),
|
438
|
+
obj.instance_variable_get(:@opts).select do |k, _v|
|
439
|
+
Util::OPTS_COPYABLE.include?(k)
|
440
|
+
end
|
441
|
+
)
|
442
|
+
else
|
443
|
+
obj
|
444
|
+
end
|
445
|
+
end
|
446
|
+
private_class_method :deep_copy
|
447
|
+
|
448
|
+
def dirty_value!(value)
|
449
|
+
case value
|
450
|
+
when Array
|
451
|
+
value.map { |v| dirty_value!(v) }
|
452
|
+
when ZaiusObject
|
453
|
+
value.dirty!
|
454
|
+
end
|
455
|
+
end
|
456
|
+
|
457
|
+
# Returns a hash of empty values for all the values that are in the given
|
458
|
+
# ZaiusObject.
|
459
|
+
def empty_values(obj)
|
460
|
+
values = case obj
|
461
|
+
when Hash then obj
|
462
|
+
when ZaiusObject then obj.instance_variable_get(:@values)
|
463
|
+
else
|
464
|
+
raise ArgumentError, "#empty_values got unexpected object type: #{obj.class.name}"
|
465
|
+
end
|
466
|
+
|
467
|
+
values.each_with_object({}) do |(k, _), update|
|
468
|
+
update[k] = ""
|
469
|
+
end
|
470
|
+
end
|
471
|
+
end
|
472
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Zaius
|
4
|
+
# ZaiusResponse encapsulates some vitals of a response that came back from
|
5
|
+
# the Zaius API.
|
6
|
+
class ZaiusResponse
|
7
|
+
# The data contained by the HTTP body of the response deserialized from
|
8
|
+
# JSON.
|
9
|
+
attr_accessor :data
|
10
|
+
|
11
|
+
# The raw HTTP body of the response.
|
12
|
+
attr_accessor :http_body
|
13
|
+
|
14
|
+
# A Hash of the HTTP headers of the response.
|
15
|
+
attr_accessor :http_headers
|
16
|
+
|
17
|
+
# The integer HTTP status code of the response.
|
18
|
+
attr_accessor :http_status
|
19
|
+
|
20
|
+
# Initializes a ZaiusResponse object from a Hash like the kind returned as
|
21
|
+
# part of a Faraday exception.
|
22
|
+
#
|
23
|
+
# This may throw JSON::ParserError if the response body is not valid JSON.
|
24
|
+
def self.from_faraday_hash(http_resp)
|
25
|
+
resp = ZaiusResponse.new
|
26
|
+
resp.data = JSON.parse(http_resp[:body], symbolize_names: true)
|
27
|
+
resp.http_body = http_resp[:body]
|
28
|
+
resp.http_headers = http_resp[:headers]
|
29
|
+
resp.http_status = http_resp[:status]
|
30
|
+
resp
|
31
|
+
end
|
32
|
+
|
33
|
+
# Initializes a ZaiusResponse object from a Faraday HTTP response object.
|
34
|
+
#
|
35
|
+
# This may throw JSON::ParserError if the response body is not valid JSON.
|
36
|
+
def self.from_faraday_response(http_resp)
|
37
|
+
resp = ZaiusResponse.new
|
38
|
+
resp.data = JSON.parse(http_resp.body, symbolize_names: true)
|
39
|
+
resp.http_body = http_resp.body
|
40
|
+
resp.http_headers = http_resp.headers
|
41
|
+
resp.http_status = http_resp.status
|
42
|
+
resp
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/zaius.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "logger"
|
3
|
+
|
4
|
+
require "zaius/api_operations/list"
|
5
|
+
require "zaius/api_operations/request"
|
6
|
+
require "zaius/zaius_object"
|
7
|
+
require "zaius/zaius_client"
|
8
|
+
require "zaius/zaius_response"
|
9
|
+
require "zaius/list_object"
|
10
|
+
|
11
|
+
require "zaius/version"
|
12
|
+
|
13
|
+
require "zaius/api_resource"
|
14
|
+
require "zaius/util"
|
15
|
+
|
16
|
+
require "zaius/customer"
|
17
|
+
require "zaius/list"
|
18
|
+
require "zaius/subscription"
|
19
|
+
|
20
|
+
require "zaius/errors"
|
21
|
+
|
22
|
+
require "json"
|
23
|
+
|
24
|
+
module Zaius
|
25
|
+
@api_base = "https://api.zaius.com/v3"
|
26
|
+
@logger = nil
|
27
|
+
|
28
|
+
class << self
|
29
|
+
attr_accessor :api_key, :api_base
|
30
|
+
end
|
31
|
+
|
32
|
+
# map to the same values as the standard library's logger
|
33
|
+
LEVEL_DEBUG = Logger::DEBUG
|
34
|
+
LEVEL_ERROR = Logger::ERROR
|
35
|
+
LEVEL_INFO = Logger::INFO
|
36
|
+
|
37
|
+
# When set prompts the library to log some extra information to $stdout and
|
38
|
+
# $stderr about what it's doing. For example, it'll produce information about
|
39
|
+
# requests, responses, and errors that are received. Valid log levels are
|
40
|
+
# `debug` and `info`, with `debug` being a little more verbose in places.
|
41
|
+
#
|
42
|
+
# Use of this configuration is only useful when `.logger` is _not_ set. When
|
43
|
+
# it is, the decision what levels to print is entirely deferred to the logger.
|
44
|
+
def self.log_level
|
45
|
+
@log_level
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.log_level=(val)
|
49
|
+
# Backwards compatibility for values that we briefly allowed
|
50
|
+
if val == "debug"
|
51
|
+
val = LEVEL_DEBUG
|
52
|
+
elsif val == "info"
|
53
|
+
val = LEVEL_INFO
|
54
|
+
end
|
55
|
+
|
56
|
+
if !val.nil? && ![LEVEL_DEBUG, LEVEL_ERROR, LEVEL_INFO].include?(val)
|
57
|
+
raise ArgumentError, "log_level should only be set to `nil`, `debug` or `info`"
|
58
|
+
end
|
59
|
+
@log_level = val
|
60
|
+
end
|
61
|
+
|
62
|
+
# Sets a logger to which logging output will be sent. The logger should
|
63
|
+
# support the same interface as the `Logger` class that's part of Ruby's
|
64
|
+
# standard library (hint, anything in `Rails.logger` will likely be
|
65
|
+
# suitable).
|
66
|
+
#
|
67
|
+
# If `.logger` is set, the value of `.log_level` is ignored. The decision on
|
68
|
+
# what levels to print is entirely deferred to the logger.
|
69
|
+
def self.logger
|
70
|
+
@logger
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.logger=(val)
|
74
|
+
@logger = val
|
75
|
+
end
|
76
|
+
end
|
data/zaius.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "zaius/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "zaius"
|
8
|
+
spec.version = Zaius::VERSION
|
9
|
+
spec.authors = ["Chris Anderson"]
|
10
|
+
spec.email = ["chris@galleyfoods.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Zaius api ruby gem.}
|
13
|
+
spec.homepage = "https://www.galleyfoods.com"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.add_dependency("faraday", "~> 0.10")
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
28
|
+
spec.add_development_dependency "byebug"
|
29
|
+
end
|