chord 0.0.4 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -1
- data/README.md +61 -10
- data/lib/chord/version.rb +1 -1
- data/lib/chord.rb +46 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82b29351f996510a42975aa338c2ece2744e49780e4c31b2c265593a2707824c
|
4
|
+
data.tar.gz: 7bc61180788440328b85083997629d9d63ddf7496c2a5702869973a6ca4ce238
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9694514bb6710a41ab0a3331f9e9fdcbe0c36945304384e7b0b4207f93d323c1c65e10eed77cdbddaea566f325a2354918d43cbdf978d32224a57665826298e
|
7
|
+
data.tar.gz: 2e2b02f34e13133d93f70693670e423250aa16090165b87484eee2927dca52d48b791ef1ac7ec914aa87dd6aca09e4726942a2d55a23ddf198f2df2f74e90ac5
|
data/CHANGELOG.md
CHANGED
@@ -2,9 +2,19 @@
|
|
2
2
|
|
3
3
|
Major changes for each release. Please see the Git log for complete list of changes.
|
4
4
|
|
5
|
+
## 0.0.6
|
6
|
+
|
7
|
+
* Return User objects from Role#users.
|
8
|
+
* Add config methods.
|
9
|
+
|
10
|
+
## 0.0.5
|
11
|
+
|
12
|
+
* Add expand! method.
|
13
|
+
* Fix Order object IDs.
|
14
|
+
|
5
15
|
## 0.0.4
|
6
16
|
|
7
|
-
* Load attribute if missing.
|
17
|
+
* Load subscription_in_cart attribute if missing when it's needed.
|
8
18
|
|
9
19
|
## 0.0.3
|
10
20
|
|
data/README.md
CHANGED
@@ -8,21 +8,72 @@ These classes provide simple read and write access to the Chord OMS API. Get sta
|
|
8
8
|
)
|
9
9
|
|
10
10
|
u = Chord::User.find(1) # fetch user
|
11
|
-
u.
|
12
|
-
u.
|
13
|
-
u.update(
|
14
|
-
# hash is merged into the existing value and
|
15
|
-
# keys and values are stringified (since
|
16
|
-
# metadata is stored in OMS as a JSON string)
|
11
|
+
u.email # view an attribute
|
12
|
+
u.attributes # see all attributes (returns hash)
|
13
|
+
u.update(name: 'Joe Smith', notes: 'Etc') # update attributes
|
17
14
|
u.add_role(3) # add role (by ID) to the user
|
18
15
|
u.remove_role(3) # remove role (by ID) from the user
|
19
16
|
u.subscriptions # fetch the user's subscriptions
|
17
|
+
u.update(metadata: {legacy_id: 415}) # when updating 'metadata' attribute, the given
|
18
|
+
# hash is merged into the existing value and
|
19
|
+
# keys and values are stringified (since
|
20
|
+
# metadata is stored in OMS as a JSON string)
|
21
|
+
|
22
|
+
o = Chord::Order.find(1) # fetch order
|
23
|
+
o.subscription_installment? # was the order a subscription installment?
|
24
|
+
o.subscription_start? # did the order start a subscription?
|
25
|
+
|
26
|
+
For complete/current list of supported objects and methods, please see the code.
|
27
|
+
|
28
|
+
|
29
|
+
## Querying
|
30
|
+
|
31
|
+
The most basic way to get a collection of objects:
|
32
|
+
|
33
|
+
Chord::Order.all
|
34
|
+
|
35
|
+
You can also filter and sort, though the parameters are not well documented:
|
36
|
+
|
37
|
+
Chord::Order.where(
|
38
|
+
'q[completed_at_gt]' => '2022-09-14',
|
39
|
+
'q[s]' => 'completed_at desc'
|
40
|
+
)
|
41
|
+
|
42
|
+
|
43
|
+
## Object attributes
|
20
44
|
|
21
45
|
Objects are constructed in a way that minimizes API calls but occasionally yields objects that seem incomplete. For example:
|
22
46
|
|
23
|
-
|
24
|
-
|
25
|
-
|
47
|
+
orders = Chord::Order.all
|
48
|
+
o = orders.first
|
49
|
+
|
50
|
+
will return a Chord::Order object with around 40 attributes, not the full set of 55 (it's missing line items, for example), because the `/orders` endpoint returns abbreviated objects. To load the full set of data for such an object, use the `expand!` method:
|
51
|
+
|
52
|
+
o.expand!
|
53
|
+
|
54
|
+
|
55
|
+
# Configuration options
|
56
|
+
|
57
|
+
To get your sensitive config data out of your code, you can put it in a YAML file, like so:
|
58
|
+
|
59
|
+
# chord_config.yml
|
60
|
+
base_url: https://...
|
61
|
+
api_key: ...
|
62
|
+
|
63
|
+
and load it by calling:
|
64
|
+
|
65
|
+
Chord.config_from_file('chord_config.yml')
|
66
|
+
|
67
|
+
Or you can put your configuration in environment variables:
|
68
|
+
|
69
|
+
CHORD_BASE_URL=https://...
|
70
|
+
CHORD_API_KEY=...
|
71
|
+
|
72
|
+
and load it by calling:
|
73
|
+
|
74
|
+
Chord.config_from_env
|
75
|
+
|
76
|
+
Both config-loading methods return a boolean indicating whether configuration data was found and loaded, so you can easily fall back from one method to the other, for example:
|
26
77
|
|
27
|
-
|
78
|
+
Chord.config_from_env or Chord.config_from_file('chord_config.yml')
|
28
79
|
|
data/lib/chord/version.rb
CHANGED
data/lib/chord.rb
CHANGED
@@ -7,8 +7,8 @@ module Chord
|
|
7
7
|
attr_accessor :api_key
|
8
8
|
|
9
9
|
def config(options)
|
10
|
-
self.base_url = options[:base_url]
|
11
|
-
self.api_key = options[:api_key]
|
10
|
+
self.base_url = options[:base_url] if options[:base_url]
|
11
|
+
self.api_key = options[:api_key] if options[:api_key]
|
12
12
|
end
|
13
13
|
|
14
14
|
def config_from_file(filepath)
|
@@ -19,6 +19,20 @@ module Chord
|
|
19
19
|
base_url: config[:base_url],
|
20
20
|
api_key: config[:api_key]
|
21
21
|
)
|
22
|
+
else
|
23
|
+
false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def config_from_env
|
28
|
+
if ENV['CHORD_BASE_URL'] or ENV['CHORD_API_KEY']
|
29
|
+
Chord.config(
|
30
|
+
base_url: ENV['CHORD_BASE_URL'],
|
31
|
+
api_key: ENV['CHORD_API_KEY']
|
32
|
+
)
|
33
|
+
true
|
34
|
+
else
|
35
|
+
false
|
22
36
|
end
|
23
37
|
end
|
24
38
|
end
|
@@ -28,20 +42,28 @@ module Chord
|
|
28
42
|
|
29
43
|
class << self
|
30
44
|
attr_writer :per_page
|
31
|
-
def per_page;
|
45
|
+
def per_page; 99999; end
|
32
46
|
|
33
47
|
def all
|
34
|
-
@all ||= fetch_all_data[base_path].map{ |i| new(i[
|
48
|
+
@all ||= fetch_all_data[base_path].map{ |i| new(i[id_attribute], i) }
|
35
49
|
end
|
36
50
|
|
37
51
|
def where(query_options = {})
|
38
|
-
fetch_all_data(query_options)[base_path].map{ |i| new(i[
|
52
|
+
fetch_all_data(query_options)[base_path].map{ |i| new(i[id_attribute], i) }
|
39
53
|
end
|
40
54
|
|
41
55
|
def find(id)
|
42
56
|
return nil if id.nil? or id == ''
|
43
|
-
attrs =
|
44
|
-
attrs.include?('error') ? nil : new(
|
57
|
+
attrs = fetch_attributes(id)
|
58
|
+
attrs.include?('error') ? nil : new(attrs[id_attribute], attrs)
|
59
|
+
end
|
60
|
+
|
61
|
+
def fetch_attributes(id)
|
62
|
+
get(base_url + "#{base_path}/#{id}", http_options).parsed_response
|
63
|
+
end
|
64
|
+
|
65
|
+
def id_attribute
|
66
|
+
'id'
|
45
67
|
end
|
46
68
|
|
47
69
|
def fetch_all_data(query_options = {})
|
@@ -109,6 +131,13 @@ module Chord
|
|
109
131
|
self.class.delete(base_url + "#{base_path}/#{id}", http_options).parsed_response
|
110
132
|
end
|
111
133
|
|
134
|
+
# fetch all attributes, but don't overwrite existing ones,
|
135
|
+
# in case changes have been made
|
136
|
+
def expand!
|
137
|
+
all_attributes = self.class.fetch_attributes(id)
|
138
|
+
@attributes = all_attributes.merge(@attributes)
|
139
|
+
end
|
140
|
+
|
112
141
|
def method_missing(method, *args, &block)
|
113
142
|
if attributes.include?(method.to_s)
|
114
143
|
attributes[method.to_s]
|
@@ -175,8 +204,12 @@ module Chord
|
|
175
204
|
'orders'
|
176
205
|
end
|
177
206
|
|
207
|
+
def self.id_attribute
|
208
|
+
'number'
|
209
|
+
end
|
210
|
+
|
178
211
|
def user
|
179
|
-
Chord::User.find(attributes['user_id'])
|
212
|
+
@user ||= Chord::User.find(attributes['user_id'])
|
180
213
|
end
|
181
214
|
|
182
215
|
def payments
|
@@ -198,9 +231,7 @@ module Chord
|
|
198
231
|
end
|
199
232
|
|
200
233
|
def subscription_start?
|
201
|
-
unless attributes.include?('subscription_in_cart')
|
202
|
-
attributes['subscription_in_cart'] = Chord::Order.find(number).subscription_in_cart
|
203
|
-
end
|
234
|
+
expand! unless attributes.include?('subscription_in_cart')
|
204
235
|
subscription_in_cart
|
205
236
|
end
|
206
237
|
end
|
@@ -211,6 +242,10 @@ module Chord
|
|
211
242
|
def self.base_path
|
212
243
|
'roles'
|
213
244
|
end
|
245
|
+
|
246
|
+
def users
|
247
|
+
attributes['users'].map{ |u| Chord::User.new(u['id'], u) }
|
248
|
+
end
|
214
249
|
end
|
215
250
|
|
216
251
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Reisner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-09-
|
11
|
+
date: 2022-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|