api-resource 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/api-resource/resource.rb +88 -86
- data/lib/api-resource/version.rb +1 -1
- data/spec/resource_spec.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9705d238f913f0554c790cfe2958570fac35ba1
|
4
|
+
data.tar.gz: f6038bdafafd1544a5a49bd633ba87e5ff01b9c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec9cd10d4e3e5bc87931e9b9b69bf15f5ca266a706cf21128e5f2e9678ee119c8d82cee0d284d711a0ec457af0a3f2ab041897a584a61c9b3306e6e0c9d53a36
|
7
|
+
data.tar.gz: 4641956e83fcb957bb54319afb7229792eaa863aad170e6b53e23e5969f965fc782c9677cd7509b13a05d093537cc2a1ac26bcd3bf8a9101e9a69e3117c75804
|
@@ -5,109 +5,111 @@ require 'active_model/serializers/json'
|
|
5
5
|
require 'active_support/core_ext/hash/indifferent_access'
|
6
6
|
require 'active_support/core_ext/object/to_param'
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
self.
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
def initialize(hash=nil)
|
27
|
-
self.attributes = hash if hash
|
28
|
-
end
|
8
|
+
module ApiResource
|
9
|
+
class Resource
|
10
|
+
|
11
|
+
include ActiveModel::Model
|
12
|
+
include ActiveModel::Serializers::JSON
|
13
|
+
|
14
|
+
class_attribute :base_url
|
15
|
+
class_attribute :hmac
|
16
|
+
class_attribute :api_id
|
17
|
+
class_attribute :api_secret
|
18
|
+
class_attribute :hmac_options
|
19
|
+
|
20
|
+
def self.with_hmac(api_id, api_secret, options={})
|
21
|
+
self.hmac = true
|
22
|
+
self.api_id = api_id
|
23
|
+
self.api_secret = api_secret
|
24
|
+
self.hmac_options = options
|
25
|
+
end
|
29
26
|
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
def initialize(hash=nil)
|
28
|
+
self.attributes = hash if hash
|
29
|
+
end
|
33
30
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
end
|
31
|
+
def self.resource_name
|
32
|
+
self.to_s.tableize
|
33
|
+
end
|
38
34
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
35
|
+
def self.find(id)
|
36
|
+
result = client(:get, {}, resource_name, id)
|
37
|
+
self.new.from_json(result)
|
38
|
+
end
|
44
39
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
40
|
+
def self.all
|
41
|
+
result = client(:get, {}, resource_name)
|
42
|
+
array = JSON.parse(result)
|
43
|
+
array.map { |h| self.new(h) }
|
44
|
+
end
|
50
45
|
|
51
|
-
|
52
|
-
|
53
|
-
|
46
|
+
def self.where(options, verb=:get)
|
47
|
+
result = client(verb, *(verb==:get ? [{}, "#{resource_name}?#{options.to_param}"] : [options, resource_name]))
|
48
|
+
array = JSON.parse(result)
|
49
|
+
array.map { |h| self.new(h) }
|
50
|
+
end
|
54
51
|
|
55
|
-
|
52
|
+
def attributes
|
53
|
+
instance_values.with_indifferent_access
|
54
|
+
end
|
56
55
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
56
|
+
protected
|
57
|
+
|
58
|
+
def attributes=(hash)
|
59
|
+
hash.each do |key, value|
|
60
|
+
if respond_to? "#{key}="
|
61
|
+
send("#{key}=", value)
|
62
|
+
else
|
63
|
+
child_class = self.class.load_class(key)
|
64
|
+
if child_class && child_class < Resource
|
65
|
+
if value.is_a?(Hash)
|
66
|
+
child_value = child_class.new(value)
|
67
|
+
define_singleton_method(key) { child_value }
|
68
|
+
elsif value.is_a?(Array)
|
69
|
+
child_values = value.map { |h| child_class.new(h) }
|
70
|
+
define_singleton_method(key) { child_values }
|
71
|
+
end
|
70
72
|
end
|
71
73
|
end
|
72
74
|
end
|
73
75
|
end
|
74
|
-
end
|
75
76
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
77
|
+
def self.method_missing(m, *args, &_)
|
78
|
+
unless args.empty?
|
79
|
+
by_method_match = /^by_(.+)/.match(m)
|
80
|
+
if by_method_match
|
81
|
+
parent_resource_name = by_method_match[1]
|
82
|
+
with_parent_resource(args[0], parent_resource_name)
|
83
|
+
end
|
82
84
|
end
|
83
85
|
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def self.load_class(plural_resource_name)
|
87
|
-
plural_resource_name.to_s.singularize.classify.constantize rescue nil
|
88
|
-
end
|
89
86
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
hashes.map { |h| self.new(h) }
|
94
|
-
end
|
87
|
+
def self.load_class(plural_resource_name)
|
88
|
+
plural_resource_name.to_s.singularize.classify.constantize rescue nil
|
89
|
+
end
|
95
90
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
headers = { accept: :json }
|
101
|
-
req_params = { url: url, method: verb, headers: headers }
|
102
|
-
if verb == :get
|
103
|
-
headers[:params] = params
|
104
|
-
else
|
105
|
-
req_params[:payload] = params
|
91
|
+
def self.with_parent_resource(parent_resource_id, parent_resource_name)
|
92
|
+
result = client(:get, {}, parent_resource_name.pluralize, parent_resource_id, self.resource_name)
|
93
|
+
hashes = JSON.parse(result)
|
94
|
+
hashes.map { |h| self.new(h) }
|
106
95
|
end
|
107
|
-
|
108
|
-
|
109
|
-
|
96
|
+
|
97
|
+
def self.client(verb, params, *paths)
|
98
|
+
url = base_url
|
99
|
+
url += '/' unless url.end_with?('/')
|
100
|
+
url = url + paths.join('/')
|
101
|
+
headers = { accept: :json }
|
102
|
+
req_params = { url: url, method: verb, headers: headers }
|
103
|
+
if verb == :get
|
104
|
+
headers[:params] = params
|
105
|
+
else
|
106
|
+
req_params[:payload] = params
|
107
|
+
end
|
108
|
+
req = RestClient::Request.new(req_params)
|
109
|
+
if hmac
|
110
|
+
req.sign!(api_id, api_secret, hmac_options)
|
111
|
+
end
|
112
|
+
req.execute
|
110
113
|
end
|
111
|
-
req.execute
|
112
114
|
end
|
113
115
|
end
|
data/lib/api-resource/version.rb
CHANGED
data/spec/resource_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chaker Nakhli
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0.
|
19
|
+
version: '0.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0.
|
26
|
+
version: '0.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rest-client
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|