ncore 1.2.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -0
- data/README.md +4 -2
- data/lib/ncore/associations.rb +35 -18
- data/lib/ncore/attributes.rb +22 -0
- data/lib/ncore/client.rb +46 -16
- data/lib/ncore/exceptions.rb +1 -0
- data/lib/ncore/methods/all.rb +5 -4
- data/lib/ncore/methods/build.rb +3 -2
- data/lib/ncore/methods/count.rb +3 -2
- data/lib/ncore/methods/create.rb +11 -10
- data/lib/ncore/methods/delete.rb +16 -6
- data/lib/ncore/methods/delete_single.rb +9 -3
- data/lib/ncore/methods/find.rb +8 -5
- data/lib/ncore/methods/find_single.rb +7 -5
- data/lib/ncore/methods/update.rb +18 -12
- data/lib/ncore/version.rb +1 -1
- data/ncore.gemspec +2 -3
- metadata +6 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48412aecbd99b296c719895d2bbd4337d509be4e
|
4
|
+
data.tar.gz: f0aae8fdd116df4efa17f2796622de842a2d972a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 016a83badb0134a32217d94da2293d9c80f7de47d31cc8ce16a749d695943b6cd35cce140a16c123dc9053d784d0b35c800b2b13afe4faeaeff486c741677d7f
|
7
|
+
data.tar.gz: a71630c646664844ddbf54733978fea894a1aa79dfc915574e8acf88538e0dd1551a9d0052e3ae89df3fa851baf95e18a8f69d94085406f5331e84392fe1e70d
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,22 @@
|
|
1
|
+
#### 2.0.0
|
2
|
+
|
3
|
+
- NOTE: This version includes breaking changes.
|
4
|
+
- Change params and credentials parsing
|
5
|
+
This changes the signatures for request() and all crud methods:
|
6
|
+
request(method, url, credentials, params, headers)
|
7
|
+
=> request(method, url, params: {}, headers: {}, credentials: {})
|
8
|
+
all(), find(), create(), update(), delete(), etc
|
9
|
+
=> pass credentials: {} instead of the final api_creds param
|
10
|
+
- find(nil) now raises RecordNotFound instead of returning nil
|
11
|
+
- Make <:assoc>_id=() writers private; shouldn't have been exposed to begin with.
|
12
|
+
- Treat 409 like 422 and add error messages instead of raising an exception.
|
13
|
+
- Add default error message for 409,422 if none received.
|
14
|
+
- Make MultiJson optional - use it if present, otherwise default to stdlib JSON.
|
15
|
+
If using MultiJson, requires v1.9+.
|
16
|
+
- Improve header handling for requests
|
17
|
+
- Add #update!(), #delete!()
|
18
|
+
- Add AccountInactive exception for 402 errors
|
19
|
+
|
1
20
|
#### 1.2.1
|
2
21
|
|
3
22
|
- Connection errors should raise NCore::ConnectionError
|
data/README.md
CHANGED
@@ -3,8 +3,10 @@
|
|
3
3
|
NCore is a Ruby gem designed to help build REST API clients. It is not an API
|
4
4
|
client by itself, but provides several useful building blocks to build one.
|
5
5
|
|
6
|
-
It relies on `
|
7
|
-
|
6
|
+
It relies on `excon` for HTTP handling and `activesupport`.
|
7
|
+
|
8
|
+
If present, uses `multi_json`. Otherwise, the stdlib 'json' is used.
|
9
|
+
'multi_json' with an accelerated json gem is recommended.
|
8
10
|
|
9
11
|
See `example/` for the beginning of an actual api client.
|
10
12
|
|
data/lib/ncore/associations.rb
CHANGED
@@ -9,59 +9,74 @@ module NCore
|
|
9
9
|
def #{assoc}(params={})
|
10
10
|
return [] unless id
|
11
11
|
reload = params.delete :reload
|
12
|
-
|
12
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
13
|
+
cacheable = params.except(:credentials, :request).empty?
|
14
|
+
params.merge! #{key}: id
|
15
|
+
if cacheable
|
13
16
|
# only cache unfiltered, default api call
|
14
|
-
@attribs[:#{assoc}] = (!reload && @attribs[:#{assoc}]) || #{klass}.all(
|
17
|
+
@attribs[:#{assoc}] = (!reload && @attribs[:#{assoc}]) || #{klass}.all(params)
|
15
18
|
else
|
16
|
-
#{klass}.all(params
|
19
|
+
#{klass}.all(params)
|
17
20
|
end
|
18
21
|
end
|
19
22
|
M1
|
20
23
|
class_eval <<-M2, __FILE__, __LINE__+1
|
21
24
|
def find_#{assoc.singularize}(aid, params={})
|
22
25
|
raise UnsavedObjectError unless id
|
23
|
-
|
26
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
27
|
+
params.merge! #{key}: id
|
28
|
+
#{klass}.find(aid, params)
|
24
29
|
end
|
25
30
|
M2
|
26
31
|
# will always return the object; check .errors? or .valid? to see how it went
|
27
32
|
class_eval <<-M3, __FILE__, __LINE__+1
|
28
33
|
def create_#{assoc.singularize}(params={})
|
29
34
|
raise UnsavedObjectError unless id
|
30
|
-
|
35
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
36
|
+
params.merge! #{key}: id
|
37
|
+
#{klass}.create(params)
|
31
38
|
end
|
32
39
|
M3
|
33
40
|
# will always return the object; check .errors? or .valid? to see how it went
|
34
41
|
class_eval <<-M4, __FILE__, __LINE__+1
|
35
42
|
def update_#{assoc.singularize}(aid, params={})
|
36
|
-
|
37
|
-
|
38
|
-
|
43
|
+
raise UnsavedObjectError unless id
|
44
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
45
|
+
params.merge! #{key}: id
|
46
|
+
#{klass}.update(aid, params)
|
39
47
|
end
|
40
48
|
M4
|
41
49
|
class_eval <<-M5, __FILE__, __LINE__+1
|
42
50
|
def create_#{assoc.singularize}!(params={})
|
43
51
|
raise UnsavedObjectError unless id
|
44
|
-
|
52
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
53
|
+
params.merge! #{key}: id
|
54
|
+
#{klass}.create!(params)
|
45
55
|
end
|
46
56
|
M5
|
47
57
|
class_eval <<-M6, __FILE__, __LINE__+1
|
48
58
|
def update_#{assoc.singularize}!(aid, params={})
|
49
|
-
|
50
|
-
|
59
|
+
raise UnsavedObjectError unless id
|
60
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
61
|
+
params.merge! #{key}: id
|
62
|
+
#{klass}.update!(aid, params)
|
51
63
|
end
|
52
64
|
M6
|
53
65
|
# will always return the object; check .errors? or .valid? to see how it went
|
54
66
|
class_eval <<-M7, __FILE__, __LINE__+1
|
55
67
|
def delete_#{assoc.singularize}(aid, params={})
|
56
|
-
|
57
|
-
|
58
|
-
|
68
|
+
raise UnsavedObjectError unless id
|
69
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
70
|
+
params.merge! #{key}: id
|
71
|
+
#{klass}.delete(aid, params)
|
59
72
|
end
|
60
73
|
M7
|
61
74
|
class_eval <<-M8, __FILE__, __LINE__+1
|
62
75
|
def delete_#{assoc.singularize}!(aid, params={})
|
63
76
|
raise UnsavedObjectError unless id
|
64
|
-
|
77
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
78
|
+
params.merge! #{key}: id
|
79
|
+
#{klass}.delete!(aid, params)
|
65
80
|
end
|
66
81
|
M8
|
67
82
|
end
|
@@ -73,11 +88,12 @@ module NCore
|
|
73
88
|
attr :#{assoc}_id
|
74
89
|
def #{assoc}(params={})
|
75
90
|
return nil unless #{assoc}_id
|
76
|
-
|
91
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
92
|
+
if params.except(:credentials, :request).empty?
|
77
93
|
# only cache unfiltered, default api call
|
78
|
-
@attribs[:#{assoc}] ||= #{klass}.find(#{assoc}_id,
|
94
|
+
@attribs[:#{assoc}] ||= #{klass}.find(#{assoc}_id, params)
|
79
95
|
else
|
80
|
-
#{klass}.find(#{assoc}_id, params
|
96
|
+
#{klass}.find(#{assoc}_id, params)
|
81
97
|
end
|
82
98
|
end
|
83
99
|
M1
|
@@ -86,6 +102,7 @@ module NCore
|
|
86
102
|
@attribs[:#{assoc}] = nil unless @attribs[:#{assoc}_id] == v
|
87
103
|
@attribs[:#{assoc}_id] = v
|
88
104
|
end
|
105
|
+
private :#{assoc}_id=
|
89
106
|
M2
|
90
107
|
end
|
91
108
|
|
data/lib/ncore/attributes.rb
CHANGED
@@ -51,6 +51,28 @@ module NCore
|
|
51
51
|
AD
|
52
52
|
end
|
53
53
|
end
|
54
|
+
|
55
|
+
def parse_request_params(params={}, opts={})
|
56
|
+
params = params.with_indifferent_access
|
57
|
+
req = params.delete(:request)
|
58
|
+
creds = params.delete(:credentials)
|
59
|
+
if opts[:json_root]
|
60
|
+
if params.key?(opts[:json_root])
|
61
|
+
o = params
|
62
|
+
else
|
63
|
+
o = {opts[:json_root] => params}.with_indifferent_access
|
64
|
+
end
|
65
|
+
else
|
66
|
+
o = params
|
67
|
+
end
|
68
|
+
o[:request] = req if req
|
69
|
+
o[:credentials] = creds if creds
|
70
|
+
o
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def parse_request_params(params, opts={})
|
75
|
+
self.class.parse_request_params(params, opts)
|
54
76
|
end
|
55
77
|
|
56
78
|
|
data/lib/ncore/client.rb
CHANGED
@@ -1,10 +1,19 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
1
3
|
module NCore
|
2
4
|
module Client
|
3
5
|
extend ActiveSupport::Concern
|
4
6
|
|
5
7
|
module ClassMethods
|
6
8
|
|
7
|
-
|
9
|
+
# opts - {params: {}, headers: {}, credentials: {}}
|
10
|
+
# unknown keys assumed to be :params if :params is missing
|
11
|
+
def request(method, url, opts={})
|
12
|
+
opts = opts.with_indifferent_access
|
13
|
+
request_credentials = opts.delete 'credentials'
|
14
|
+
headers = opts.delete('headers') || {}
|
15
|
+
params = opts['params'] || opts
|
16
|
+
|
8
17
|
request_credentials ||= retrieve_credentials
|
9
18
|
request_credentials = parse_credentials(request_credentials)
|
10
19
|
|
@@ -21,7 +30,11 @@ module NCore
|
|
21
30
|
path += qs
|
22
31
|
payload = nil
|
23
32
|
else
|
24
|
-
|
33
|
+
if defined? MultiJson
|
34
|
+
payload = MultiJson.encode params
|
35
|
+
else
|
36
|
+
payload = JSON.generate params
|
37
|
+
end
|
25
38
|
end
|
26
39
|
|
27
40
|
rest_opts = {
|
@@ -65,8 +78,8 @@ module NCore
|
|
65
78
|
h = {}
|
66
79
|
[default_headers, auth_headers(req_credentials), headers].each do |set|
|
67
80
|
set.each do |k,v|
|
68
|
-
k = k.to_s.
|
69
|
-
h[k] = v
|
81
|
+
k = k.to_s.tr('_','-').gsub(%r{(^|-)\w}){$&.upcase}
|
82
|
+
h[k] = v.respond_to?(:call) ? v.call : v
|
70
83
|
end
|
71
84
|
end
|
72
85
|
h
|
@@ -199,11 +212,13 @@ module NCore
|
|
199
212
|
case response.status
|
200
213
|
when 401 # API auth valid; API call itself is an auth-related call and failed
|
201
214
|
raise parent::AuthenticationFailed
|
215
|
+
when 402
|
216
|
+
raise parent::AccountInactive, "Account inactive; login to portal to check account status."
|
202
217
|
when 403 # API auth failed or insufficient permissions
|
203
218
|
raise parent::AccessDenied, "Access denied; check your API credentials and permissions."
|
204
219
|
when 404
|
205
220
|
raise parent::RecordNotFound
|
206
|
-
when 422
|
221
|
+
when 409, 422
|
207
222
|
# pass through
|
208
223
|
when 429
|
209
224
|
raise parent::RateLimited
|
@@ -216,16 +231,24 @@ module NCore
|
|
216
231
|
end
|
217
232
|
|
218
233
|
def parse_response(response)
|
219
|
-
if
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
234
|
+
if response.body.blank?
|
235
|
+
json = {}
|
236
|
+
else
|
237
|
+
if defined? MultiJson
|
238
|
+
begin
|
239
|
+
json = MultiJson.load(response.body, symbolize_keys: false) || {}
|
240
|
+
rescue MultiJson::ParseError
|
241
|
+
raise parent::Error, "Unable to parse API response; HTTP status: #{response.status}; body: #{response.body.inspect}"
|
242
|
+
end
|
243
|
+
else
|
244
|
+
begin
|
245
|
+
json = JSON.parse(response.body, symbolize_names: false) || {}
|
246
|
+
rescue JSON::ParserError
|
247
|
+
raise parent::Error, "Unable to parse API response; HTTP status: #{response.status}; body: #{response.body.inspect}"
|
248
|
+
end
|
249
|
+
end
|
228
250
|
end
|
251
|
+
json = json.with_indifferent_access
|
229
252
|
errors = json.delete(:errors) || []
|
230
253
|
if errors.any?
|
231
254
|
errors = errors.values.flatten
|
@@ -239,6 +262,9 @@ module NCore
|
|
239
262
|
metadata = nil
|
240
263
|
end
|
241
264
|
end
|
265
|
+
if [409, 422].include?(response.status) && errors.empty?
|
266
|
+
errors.push 'Validation error'
|
267
|
+
end
|
242
268
|
{data: json, errors: errors, metadata: metadata}
|
243
269
|
end
|
244
270
|
|
@@ -283,11 +309,15 @@ DBG
|
|
283
309
|
|
284
310
|
def debug_response(response)
|
285
311
|
return unless logger.debug?
|
286
|
-
|
312
|
+
if defined? MultiJson
|
313
|
+
json = MultiJson.load(response.body||'', symbolize_keys: false) rescue response.body
|
314
|
+
else
|
315
|
+
json = JSON.parse(response.body||'', symbolize_names: false) rescue response.body
|
316
|
+
end
|
287
317
|
logger << <<-DBG
|
288
318
|
RESPONSE:
|
289
319
|
#{response.headers['Status']} | #{response.headers['Content-Type']} | #{response.body.size} bytes
|
290
|
-
#{response.headers.except('Status', 'Connection', 'Content-Type'
|
320
|
+
#{response.headers.except('Status', 'Connection', 'Content-Type').map{|h,d| "#{h}: #{d}"}.join("\n ")}
|
291
321
|
#{json.pretty_inspect.split("\n").join("\n ")}
|
292
322
|
#{'-=- '*18}
|
293
323
|
DBG
|
data/lib/ncore/exceptions.rb
CHANGED
data/lib/ncore/methods/all.rb
CHANGED
@@ -3,8 +3,9 @@ module NCore
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
def all(params={}
|
7
|
-
|
6
|
+
def all(params={})
|
7
|
+
params = parse_request_params(params)
|
8
|
+
parsed, creds = request(:get, url, params)
|
8
9
|
if parsed[:errors].any?
|
9
10
|
raise parent::QueryError, parsed[:errors]
|
10
11
|
end
|
@@ -20,9 +21,9 @@ module NCore
|
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
23
|
-
def first(params={}
|
24
|
+
def first(params={})
|
24
25
|
params = params.with_indifferent_access.merge(max_results: 1)
|
25
|
-
all(params
|
26
|
+
all(params).first
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
data/lib/ncore/methods/build.rb
CHANGED
@@ -3,8 +3,9 @@ module NCore
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
def build(params={}
|
7
|
-
|
6
|
+
def build(params={})
|
7
|
+
params = parse_request_params(params)
|
8
|
+
parsed, creds = request(:get, url+'/new', params)
|
8
9
|
if parsed[:errors].any?
|
9
10
|
raise parent::QueryError, parsed[:errors]
|
10
11
|
end
|
data/lib/ncore/methods/count.rb
CHANGED
@@ -3,8 +3,9 @@ module NCore
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
def count(params={}
|
7
|
-
|
6
|
+
def count(params={})
|
7
|
+
params = parse_request_params(params)
|
8
|
+
parsed, _ = request(:get, "#{url}/count", params)
|
8
9
|
parsed[:data][:count]
|
9
10
|
end
|
10
11
|
end
|
data/lib/ncore/methods/create.rb
CHANGED
@@ -3,28 +3,29 @@ module NCore
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
def create!(attribs={}
|
7
|
-
obj = create(attribs
|
8
|
-
if obj.errors
|
6
|
+
def create!(attribs={})
|
7
|
+
obj = create(attribs)
|
8
|
+
if obj.errors?
|
9
9
|
raise parent::RecordInvalid, obj
|
10
10
|
end
|
11
11
|
obj
|
12
12
|
end
|
13
13
|
|
14
14
|
# always returns a new object; check .errors? or .valid? to see how it went
|
15
|
-
def create(attribs={}
|
16
|
-
params =
|
17
|
-
|
18
|
-
|
15
|
+
def create(attribs={})
|
16
|
+
params = parse_request_params(attribs)
|
17
|
+
obj = new({}, params[:credentials])
|
18
|
+
obj.send :create, params
|
19
|
+
obj
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
22
23
|
private
|
23
24
|
|
24
25
|
def create(attribs={})
|
25
|
-
params =
|
26
|
-
parsed, @api_creds = request(:post, self.class.url,
|
27
|
-
load(data:
|
26
|
+
params = parse_request_params(attribs, json_root: json_root).reverse_merge credentials: api_creds
|
27
|
+
parsed, @api_creds = request(:post, self.class.url, params)
|
28
|
+
load(data: params[json_root]) if parsed[:errors].any?
|
28
29
|
load(parsed)
|
29
30
|
errors.empty? ? self : false
|
30
31
|
end
|
data/lib/ncore/methods/delete.rb
CHANGED
@@ -3,24 +3,34 @@ module NCore
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
def delete!(id, params={}
|
7
|
-
obj =
|
8
|
-
obj.
|
6
|
+
def delete!(id, params={})
|
7
|
+
obj = delete(id, attribs)
|
8
|
+
if obj.errors?
|
9
|
+
raise parent::RecordInvalid, obj
|
10
|
+
end
|
11
|
+
obj
|
9
12
|
end
|
10
13
|
|
11
14
|
# always returns a new object; check .errors? or .valid? to see how it went
|
12
|
-
def delete(id, params={}
|
13
|
-
|
15
|
+
def delete(id, params={})
|
16
|
+
raise(parent::RecordNotFound, "Cannot delete id=nil") if id.blank?
|
17
|
+
params = parse_request_params(params)
|
18
|
+
obj = new({id: id}, params[:credentials])
|
14
19
|
obj.delete(params)
|
15
20
|
obj
|
16
21
|
end
|
17
22
|
end
|
18
23
|
|
19
24
|
def delete(params={})
|
20
|
-
|
25
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
26
|
+
parsed, @api_creds = request(:delete, url, params)
|
21
27
|
load(parsed)
|
22
28
|
errors.empty? ? self : false
|
23
29
|
end
|
24
30
|
|
31
|
+
def delete!(params={})
|
32
|
+
delete(params) || raise(self.class.parent::RecordInvalid, self)
|
33
|
+
end
|
34
|
+
|
25
35
|
end
|
26
36
|
end
|
@@ -3,17 +3,23 @@ module NCore
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
def delete(params={}
|
7
|
-
|
6
|
+
def delete(params={})
|
7
|
+
params = parse_request_params(params)
|
8
|
+
obj = new({}, params[:credentials])
|
8
9
|
obj.delete(params) || raise(parent::RecordInvalid, obj)
|
9
10
|
end
|
10
11
|
end
|
11
12
|
|
12
13
|
def delete(params={})
|
13
|
-
|
14
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
15
|
+
parsed, @api_creds = request(:delete, url, params)
|
14
16
|
load(parsed)
|
15
17
|
errors.empty? ? self : false
|
16
18
|
end
|
17
19
|
|
20
|
+
def delete!(params={})
|
21
|
+
delete(params) || raise(self.class.parent::RecordInvalid, self)
|
22
|
+
end
|
23
|
+
|
18
24
|
end
|
19
25
|
end
|
data/lib/ncore/methods/find.rb
CHANGED
@@ -3,13 +3,15 @@ module NCore
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
def find(id, params={}
|
7
|
-
|
6
|
+
def find(id, params={})
|
7
|
+
raise(parent::RecordNotFound, "Cannot find id=nil") if id.blank?
|
8
|
+
params = parse_request_params(params)
|
9
|
+
o = new({id: id}, params[:credentials])
|
8
10
|
o.reload(params)
|
9
11
|
end
|
10
12
|
|
11
|
-
def retrieve(id, params={}
|
12
|
-
find id, params
|
13
|
+
def retrieve(id, params={})
|
14
|
+
find id, params
|
13
15
|
rescue parent::RecordNotFound
|
14
16
|
false
|
15
17
|
end
|
@@ -17,7 +19,8 @@ module NCore
|
|
17
19
|
|
18
20
|
def reload(find_params={})
|
19
21
|
return if id.blank?
|
20
|
-
|
22
|
+
params = parse_request_params(find_params).reverse_merge credentials: api_creds
|
23
|
+
parsed, @api_creds = request(:get, url, params)
|
21
24
|
@attribs = {}.with_indifferent_access
|
22
25
|
load(parsed)
|
23
26
|
end
|
@@ -3,16 +3,17 @@ module NCore
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
def find(params={}
|
7
|
-
|
6
|
+
def find(params={})
|
7
|
+
params = parse_request_params(params)
|
8
|
+
parsed, creds = request(:get, url, params)
|
8
9
|
if parsed[:errors].any?
|
9
10
|
raise parent::QueryError, parsed[:errors]
|
10
11
|
end
|
11
12
|
new(parsed, creds)
|
12
13
|
end
|
13
14
|
|
14
|
-
def retrieve(params={}
|
15
|
-
find params
|
15
|
+
def retrieve(params={})
|
16
|
+
find params
|
16
17
|
rescue parent::RecordNotFound
|
17
18
|
false
|
18
19
|
end
|
@@ -25,7 +26,8 @@ module NCore
|
|
25
26
|
private
|
26
27
|
|
27
28
|
def reload(find_params={})
|
28
|
-
|
29
|
+
params = parse_request_params(find_params).reverse_merge credentials: api_creds
|
30
|
+
parsed, @api_creds = request(:get, url, params)
|
29
31
|
@attribs = {}.with_indifferent_access
|
30
32
|
load(parsed)
|
31
33
|
end
|
data/lib/ncore/methods/update.rb
CHANGED
@@ -3,29 +3,35 @@ module NCore
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
def update!(id, attribs
|
7
|
-
obj =
|
8
|
-
obj.
|
6
|
+
def update!(id, attribs)
|
7
|
+
obj = update(id, attribs)
|
8
|
+
if obj.errors?
|
9
|
+
raise parent::RecordInvalid, obj
|
10
|
+
end
|
11
|
+
obj
|
9
12
|
end
|
10
13
|
|
11
14
|
# always returns a new object; check .errors? or .valid? to see how it went
|
12
|
-
def update(id, attribs
|
13
|
-
|
14
|
-
|
15
|
+
def update(id, attribs)
|
16
|
+
raise(parent::RecordNotFound, "Cannot update id=nil") if id.blank?
|
17
|
+
params = parse_request_params(attribs)
|
18
|
+
obj = new({id: id}, params[:credentials])
|
19
|
+
obj.update params
|
15
20
|
obj
|
16
21
|
end
|
17
22
|
end
|
18
23
|
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
24
|
def update(attribs={})
|
23
|
-
params =
|
24
|
-
parsed, @api_creds = request(:put, url,
|
25
|
-
load(data:
|
25
|
+
params = parse_request_params(attribs, json_root: json_root).reverse_merge credentials: api_creds
|
26
|
+
parsed, @api_creds = request(:put, url, params)
|
27
|
+
load(data: params[json_root]) if parsed[:errors].any?
|
26
28
|
load(parsed)
|
27
29
|
errors.empty? ? self : false
|
28
30
|
end
|
29
31
|
|
32
|
+
def update!(params={})
|
33
|
+
update(params) || raise(self.class.parent::RecordInvalid, self)
|
34
|
+
end
|
35
|
+
|
30
36
|
end
|
31
37
|
end
|
data/lib/ncore/version.rb
CHANGED
data/ncore.gemspec
CHANGED
@@ -6,7 +6,7 @@ require 'ncore/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "ncore"
|
8
8
|
spec.version = NCore::VERSION
|
9
|
-
spec.authors = ["
|
9
|
+
spec.authors = ["Notioneer Team"]
|
10
10
|
spec.email = ["hello@notioneer.com"]
|
11
11
|
spec.description = %q{NCore - Ruby gem useful for building REST API clients}
|
12
12
|
spec.summary = %q{NCore - Gem for building REST API clients}
|
@@ -18,9 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency 'activesupport', '>= 3.2', '< 5.
|
21
|
+
spec.add_dependency 'activesupport', '>= 3.2', '< 5.1'
|
22
22
|
spec.add_dependency 'excon', '~> 0.32'
|
23
|
-
spec.add_dependency 'multi_json', '~> 1.7'
|
24
23
|
|
25
24
|
spec.add_development_dependency "bundler", "~> 1.3"
|
26
25
|
spec.add_development_dependency "rake"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ncore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Notioneer Team
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '3.2'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '5.
|
22
|
+
version: '5.1'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '3.2'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '5.
|
32
|
+
version: '5.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: excon
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,20 +44,6 @@ dependencies:
|
|
44
44
|
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0.32'
|
47
|
-
- !ruby/object:Gem::Dependency
|
48
|
-
name: multi_json
|
49
|
-
requirement: !ruby/object:Gem::Requirement
|
50
|
-
requirements:
|
51
|
-
- - "~>"
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '1.7'
|
54
|
-
type: :runtime
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
requirements:
|
58
|
-
- - "~>"
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
version: '1.7'
|
61
47
|
- !ruby/object:Gem::Dependency
|
62
48
|
name: bundler
|
63
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -152,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
138
|
version: '0'
|
153
139
|
requirements: []
|
154
140
|
rubyforge_project:
|
155
|
-
rubygems_version: 2.4.
|
141
|
+
rubygems_version: 2.4.8
|
156
142
|
signing_key:
|
157
143
|
specification_version: 4
|
158
144
|
summary: NCore - Gem for building REST API clients
|