chord 0.0.5 → 0.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6da5791c0fe9f32d72d827bc87ae15186cadbff18bfceecee81b98f17c3111a0
4
- data.tar.gz: 037ad832506b87180563ce7ab0d8fbf591bcef211e1a72abe1110292329052dc
3
+ metadata.gz: 4caa956b7d9f279a1973f2a5ad5e63f0078697ecb1fc129fcff09c10dfcdbbc3
4
+ data.tar.gz: c4a93aa0e26f11d6bb42b7345b1dead92f685eafb43d96fa34516d05eb3a09a2
5
5
  SHA512:
6
- metadata.gz: afd04a08a2150ee30708c5ded7ec9a795839cfe606e17e9633691a76e2820346a7127995374639a8bbda0a20b39d529380b1b60810fee7993ad68983ae904fc5
7
- data.tar.gz: d9c99564446ead46f5155f6efa8ae6efed6caa28d6ac64241f4be70fb6f4f674f3f9b228610ebf8a1d41dd8cf7d81b279942d5bcb25368ba877dd90d88506dbf
6
+ metadata.gz: 8fa44ce53bc77f079140bab0838a66196314f103f3c515ea8dd90c9341af047fa5afb19925189a864652590d4fcf2f431095ceb4cbbde99725085dd72477206e
7
+ data.tar.gz: f30e7aa9d991c7822ab93777a8a6de9c9f6df943a83d8bf7c629a2b1f3ba18bc485d570a2aa152ca14f2e01d6b3e794925d1cf931199ad76f78a3ddb868b9feb
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.7
6
+
7
+ * Remove ActiveRecord dependency.
8
+ * Make some methods private.
9
+ * Raise APIError when no data returned.
10
+
11
+ ## 0.0.6
12
+
13
+ * Return User objects from Role#users.
14
+ * Add config methods.
15
+
5
16
  ## 0.0.5
6
17
 
7
18
  * Add expand! method.
data/README.md CHANGED
@@ -8,22 +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.attributes # see attributes hash
12
- u.update(name: 'New Name', notes: 'Etc') # update attributes
13
- u.update(metadata: {ambassador_id: 415}) # when updating 'metadata' attribute, the given
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
20
17
 
18
+ o = Chord::Order.find(1) # fetch order
19
+ o.subscription_installment? # was the order a subscription installment?
20
+ o.subscription_start? # did the order start a subscription?
21
+
22
+ For complete/current list of supported objects and methods, please see the code.
23
+
24
+
25
+ ## Querying
26
+
27
+ The most basic way to get a collection of objects:
28
+
29
+ Chord::Order.all
30
+
31
+ You can also filter and sort, though the parameters are not well documented:
32
+
33
+ Chord::Order.where(
34
+ 'q[completed_at_gt]' => '2022-09-14',
35
+ 'q[s]' => 'completed_at desc'
36
+ )
37
+
38
+
39
+ ## Object attributes
40
+
21
41
  Objects are constructed in a way that minimizes API calls but occasionally yields objects that seem incomplete. For example:
22
42
 
23
43
  orders = Chord::Order.all
24
44
  o = orders.first
25
45
 
26
- 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 these objects, use the `expand!` method:
46
+ 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:
27
47
 
28
48
  o.expand!
29
49
 
50
+
51
+ # Configuration options
52
+
53
+ To get your sensitive config data out of your code, you can put it in a YAML file, like so:
54
+
55
+ # chord_config.yml
56
+ base_url: https://...
57
+ api_key: ...
58
+
59
+ and load it by calling:
60
+
61
+ Chord.config_from_file('chord_config.yml')
62
+
63
+ Or you can put your configuration in environment variables:
64
+
65
+ CHORD_BASE_URL=https://...
66
+ CHORD_API_KEY=...
67
+
68
+ and load it by calling:
69
+
70
+ Chord.config_from_env
71
+
72
+ 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:
73
+
74
+ Chord.config_from_env or Chord.config_from_file('chord_config.yml')
75
+
76
+
77
+ # To Do
78
+
79
+ * tests should use mocks instead of real API requests
data/lib/chord/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Chord
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.7"
3
3
  end
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
@@ -31,33 +45,22 @@ module Chord
31
45
  def per_page; 99999; end
32
46
 
33
47
  def all
48
+ check_for_config!
34
49
  @all ||= fetch_all_data[base_path].map{ |i| new(i[id_attribute], i) }
35
50
  end
36
51
 
37
52
  def where(query_options = {})
53
+ check_for_config!
38
54
  fetch_all_data(query_options)[base_path].map{ |i| new(i[id_attribute], i) }
39
55
  end
40
56
 
41
57
  def find(id)
58
+ check_for_config!
42
59
  return nil if id.nil? or id == ''
43
60
  attrs = fetch_attributes(id)
44
61
  attrs.include?('error') ? nil : new(attrs[id_attribute], attrs)
45
62
  end
46
63
 
47
- def fetch_attributes(id)
48
- get(base_url + "#{base_path}/#{id}", http_options).parsed_response
49
- end
50
-
51
- def id_attribute
52
- 'id'
53
- end
54
-
55
- def fetch_all_data(query_options = {})
56
- query_options = { per_page: per_page }.merge(query_options)
57
- url = base_url + base_path + '?' + hash_to_query(query_options)
58
- get(url, http_options).parsed_response
59
- end
60
-
61
64
  def base_url
62
65
  Chord.base_url
63
66
  end
@@ -71,6 +74,28 @@ module Chord
71
74
 
72
75
  private # --------------------------------------------------------------
73
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
+
74
99
  def hash_to_query(hash)
75
100
  require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
76
101
  hash.collect{ |p|
@@ -100,14 +125,6 @@ module Chord
100
125
  end
101
126
 
102
127
  def update(new_attributes)
103
- new_attributes.stringify_keys!
104
- # merge values into existing metadata
105
- if new_attributes.include?('metadata')
106
- # Chord expects all metadata values to be strings
107
- new_metadata = new_attributes['metadata'].map{ |k,v| [k.to_s, v.to_s] }.to_h
108
- new_attributes['metadata'] = (attributes['metadata'] || {}).merge(new_metadata)
109
- # TODO: delete entries with empty value?
110
- end
111
128
  self.attributes = self.class.patch(base_url + "#{base_path}/#{id}",
112
129
  http_options.merge(body: new_attributes.to_json)
113
130
  ).parsed_response
@@ -120,7 +137,7 @@ module Chord
120
137
  # fetch all attributes, but don't overwrite existing ones,
121
138
  # in case changes have been made
122
139
  def expand!
123
- all_attributes = self.class.fetch_attributes(id)
140
+ all_attributes = self.class.send(:fetch_attributes, id)
124
141
  @attributes = all_attributes.merge(@attributes)
125
142
  end
126
143
 
@@ -228,6 +245,10 @@ module Chord
228
245
  def self.base_path
229
246
  'roles'
230
247
  end
248
+
249
+ def users
250
+ attributes['users'].map{ |u| Chord::User.new(u['id'], u) }
251
+ end
231
252
  end
232
253
 
233
254
 
@@ -248,4 +269,10 @@ module Chord
248
269
  'products'
249
270
  end
250
271
  end
272
+
273
+ class ConfigurationError < StandardError
274
+ end
275
+
276
+ class APIError < StandardError
277
+ end
251
278
  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.5
4
+ version: 0.0.7
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-20 00:00:00.000000000 Z
11
+ date: 2022-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -52,7 +52,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
- version: 2.0.0
55
+ version: 2.5.0
56
56
  required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - ">="