chord 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82b29351f996510a42975aa338c2ece2744e49780e4c31b2c265593a2707824c
4
- data.tar.gz: 7bc61180788440328b85083997629d9d63ddf7496c2a5702869973a6ca4ce238
3
+ metadata.gz: 8f0f6af63adad1bbb028d80575a89a9edcbb424125446e7d8b7c6988d110afd9
4
+ data.tar.gz: ed4caff66b9252177a0bb9ee2a173db714f6da72e893146abab367c05965aa7a
5
5
  SHA512:
6
- metadata.gz: a9694514bb6710a41ab0a3331f9e9fdcbe0c36945304384e7b0b4207f93d323c1c65e10eed77cdbddaea566f325a2354918d43cbdf978d32224a57665826298e
7
- data.tar.gz: 2e2b02f34e13133d93f70693670e423250aa16090165b87484eee2927dca52d48b791ef1ac7ec914aa87dd6aca09e4726942a2d55a23ddf198f2df2f74e90ac5
6
+ metadata.gz: 5e7aa4ab708794d3a80bd61158973cfdd38d9aec7af9b320811af6699137043ef4ce5372fb26c0e1493f4a0100b77356aae555d3228a3b819324a3c7e65853bc
7
+ data.tar.gz: 418c1f595998d385289b4825ebd181e1f2abdfe823b8d6ea33348f7b3c9c7adf771864cb94545fcead076b75ab4b2570dfea0a3519bb03e5000bca3f818189b1
data/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  Major changes for each release. Please see the Git log for complete list of changes.
4
4
 
5
+ ## 0.0.8
6
+
7
+ * Don't preserve existing attributes when calling `expand!`.
8
+ * Add User#orders method.
9
+
10
+ ## 0.0.7
11
+
12
+ * Remove ActiveRecord dependency.
13
+ * Make some methods private.
14
+ * Raise APIError when no data returned.
15
+
5
16
  ## 0.0.6
6
17
 
7
18
  * Return User objects from Role#users.
data/README.md CHANGED
@@ -7,19 +7,16 @@ These classes provide simple read and write access to the Chord OMS API. Get sta
7
7
  api_key: '<key>'
8
8
  )
9
9
 
10
- u = Chord::User.find(1) # fetch user
10
+ u = Chord::User.find(1) # fetch user by ID
11
11
  u.email # view an attribute
12
12
  u.attributes # see all attributes (returns hash)
13
13
  u.update(name: 'Joe Smith', notes: 'Etc') # update attributes
14
14
  u.add_role(3) # add role (by ID) to the user
15
15
  u.remove_role(3) # remove role (by ID) from the user
16
+ u.orders # fetch the user's orders
16
17
  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
18
 
22
- o = Chord::Order.find(1) # fetch order
19
+ o = Chord::Order.find('R87234695') # fetch order by ID
23
20
  o.subscription_installment? # was the order a subscription installment?
24
21
  o.subscription_start? # did the order start a subscription?
25
22
 
@@ -32,12 +29,10 @@ The most basic way to get a collection of objects:
32
29
 
33
30
  Chord::Order.all
34
31
 
35
- You can also filter and sort, though the parameters are not well documented:
32
+ You can also filter results by using `where`:
36
33
 
37
- Chord::Order.where(
38
- 'q[completed_at_gt]' => '2022-09-14',
39
- 'q[s]' => 'completed_at desc'
40
- )
34
+ Chord::Order.where('q[completed_at_gt]' => '2022-09-14')
35
+ Chord::User.where('q[spree_roles_id_in]' => 8)
41
36
 
42
37
 
43
38
  ## Object attributes
@@ -54,7 +49,7 @@ will return a Chord::Order object with around 40 attributes, not the full set of
54
49
 
55
50
  # Configuration options
56
51
 
57
- To get your sensitive config data out of your code, you can put it in a YAML file, like so:
52
+ To get configuration data out of your code, put it in a YAML file, like so:
58
53
 
59
54
  # chord_config.yml
60
55
  base_url: https://...
@@ -64,7 +59,7 @@ and load it by calling:
64
59
 
65
60
  Chord.config_from_file('chord_config.yml')
66
61
 
67
- Or you can put your configuration in environment variables:
62
+ Or put your config in environment variables:
68
63
 
69
64
  CHORD_BASE_URL=https://...
70
65
  CHORD_API_KEY=...
@@ -77,3 +72,8 @@ Both config-loading methods return a boolean indicating whether configuration da
77
72
 
78
73
  Chord.config_from_env or Chord.config_from_file('chord_config.yml')
79
74
 
75
+
76
+ # To Do
77
+
78
+ * tests should use mocks instead of real API requests
79
+ * support more objects and methods
data/lib/chord/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Chord
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/chord.rb CHANGED
@@ -45,33 +45,22 @@ module Chord
45
45
  def per_page; 99999; end
46
46
 
47
47
  def all
48
+ check_for_config!
48
49
  @all ||= fetch_all_data[base_path].map{ |i| new(i[id_attribute], i) }
49
50
  end
50
51
 
51
52
  def where(query_options = {})
53
+ check_for_config!
52
54
  fetch_all_data(query_options)[base_path].map{ |i| new(i[id_attribute], i) }
53
55
  end
54
56
 
55
57
  def find(id)
58
+ check_for_config!
56
59
  return nil if id.nil? or id == ''
57
60
  attrs = fetch_attributes(id)
58
61
  attrs.include?('error') ? nil : new(attrs[id_attribute], attrs)
59
62
  end
60
63
 
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'
67
- end
68
-
69
- def fetch_all_data(query_options = {})
70
- query_options = { per_page: per_page }.merge(query_options)
71
- url = base_url + base_path + '?' + hash_to_query(query_options)
72
- get(url, http_options).parsed_response
73
- end
74
-
75
64
  def base_url
76
65
  Chord.base_url
77
66
  end
@@ -85,6 +74,28 @@ module Chord
85
74
 
86
75
  private # --------------------------------------------------------------
87
76
 
77
+ def id_attribute
78
+ 'id'
79
+ end
80
+
81
+ def fetch_attributes(id)
82
+ check_for_config!
83
+ get(base_url + "#{base_path}/#{id}", http_options).parsed_response or raise APIError, 'No data returned by API'
84
+ end
85
+
86
+ def fetch_all_data(query_options = {})
87
+ check_for_config!
88
+ query_options = { per_page: per_page }.merge(query_options)
89
+ url = base_url + base_path + '?' + hash_to_query(query_options)
90
+ get(url, http_options).parsed_response or raise APIError, 'No data returned by API'
91
+ end
92
+
93
+ def check_for_config!
94
+ if Chord.base_url.nil? or Chord.api_key.nil?
95
+ raise ConfigurationError, 'Please configure Chord by calling Chord.config(base_url: ..., api_key: ...)'
96
+ end
97
+ end
98
+
88
99
  def hash_to_query(hash)
89
100
  require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
90
101
  hash.collect{ |p|
@@ -114,14 +125,6 @@ module Chord
114
125
  end
115
126
 
116
127
  def update(new_attributes)
117
- new_attributes.stringify_keys!
118
- # merge values into existing metadata
119
- if new_attributes.include?('metadata')
120
- # Chord expects all metadata values to be strings
121
- new_metadata = new_attributes['metadata'].map{ |k,v| [k.to_s, v.to_s] }.to_h
122
- new_attributes['metadata'] = (attributes['metadata'] || {}).merge(new_metadata)
123
- # TODO: delete entries with empty value?
124
- end
125
128
  self.attributes = self.class.patch(base_url + "#{base_path}/#{id}",
126
129
  http_options.merge(body: new_attributes.to_json)
127
130
  ).parsed_response
@@ -134,8 +137,7 @@ module Chord
134
137
  # fetch all attributes, but don't overwrite existing ones,
135
138
  # in case changes have been made
136
139
  def expand!
137
- all_attributes = self.class.fetch_attributes(id)
138
- @attributes = all_attributes.merge(@attributes)
140
+ self.attributes = self.class.send(:fetch_attributes, id)
139
141
  end
140
142
 
141
143
  def method_missing(method, *args, &block)
@@ -150,36 +152,14 @@ module Chord
150
152
 
151
153
  class User < Base
152
154
 
153
- #
154
- # For avoiding API calls.
155
- #
156
- def self.all_by_id
157
- unless @all_by_id
158
- @all_by_id = {}
159
- all.each do |u|
160
- @all_by_id[u.id] = u
161
- end
162
- end
163
- @all_by_id
164
- end
165
-
166
- #
167
- # For mapping users on our old site to Chord.
168
- #
169
- def self.all_by_email
170
- unless @all_by_email
171
- @all_by_email = {}
172
- all.each do |u|
173
- @all_by_email[u.email] = u
174
- end
175
- end
176
- @all_by_email
177
- end
178
-
179
155
  def self.base_path
180
156
  'users'
181
157
  end
182
158
 
159
+ def orders
160
+ Order.where('q[user_id_eq]' => id)
161
+ end
162
+
183
163
  def add_role(role_id)
184
164
  self.class.put(base_url + "roles/#{role_id}/add/#{id}", http_options).parsed_response
185
165
  end
@@ -266,4 +246,17 @@ module Chord
266
246
  'products'
267
247
  end
268
248
  end
249
+
250
+ class Variant < Base
251
+
252
+ def self.base_path
253
+ 'variants'
254
+ end
255
+ end
256
+
257
+ class ConfigurationError < StandardError
258
+ end
259
+
260
+ class APIError < StandardError
261
+ end
269
262
  end
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.6
4
+ version: 0.0.8
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-21 00:00:00.000000000 Z
11
+ date: 2022-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty