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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5dae0339d240421eb712387cb2a22f95da96640
4
- data.tar.gz: ec15e6fe4b2179873490059e11ca1811823ddee1
3
+ metadata.gz: b9705d238f913f0554c790cfe2958570fac35ba1
4
+ data.tar.gz: f6038bdafafd1544a5a49bd633ba87e5ff01b9c0
5
5
  SHA512:
6
- metadata.gz: 1be26aca4ea7a90274e8ebe02dec7c3c18bcc51df974dfee5c3e9f471464cf490e5e68405e6cfd55155e18055d23c01cd7b458e8341aed78235476b005822b96
7
- data.tar.gz: e5daac829ddb90523e31364302d53eec67046474c2ae8915921245828b0c6a50079e61fcaf2a2a9d1c38f3bc0c950792139f4b593667f1f39ad26e1b25832069
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
- class Resource
9
-
10
- include ActiveModel::Model
11
- include ActiveModel::Serializers::JSON
12
-
13
- class_attribute :base_url
14
- class_attribute :hmac
15
- class_attribute :api_id
16
- class_attribute :api_secret
17
- class_attribute :auth_prefix
18
-
19
- def self.with_hmac(api_id, api_secret, auth_prefix='APIAuth')
20
- self.hmac = true
21
- self.api_id = api_id
22
- self.api_secret = api_secret
23
- self.auth_prefix = auth_prefix
24
- end
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
- def self.resource_name
31
- self.to_s.tableize
32
- end
27
+ def initialize(hash=nil)
28
+ self.attributes = hash if hash
29
+ end
33
30
 
34
- def self.find(id)
35
- result = client(:get, {}, resource_name, id)
36
- self.new.from_json(result)
37
- end
31
+ def self.resource_name
32
+ self.to_s.tableize
33
+ end
38
34
 
39
- def self.all
40
- result = client(:get, {}, resource_name)
41
- array = JSON.parse(result)
42
- array.map { |h| self.new(h) }
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
- def self.where(options, verb=:get)
46
- result = client(verb, *(verb==:get ? [{}, "#{resource_name}?#{options.to_param}"] : [options, resource_name]))
47
- array = JSON.parse(result)
48
- array.map { |h| self.new(h) }
49
- end
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
- def attributes
52
- instance_values.with_indifferent_access
53
- end
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
- protected
52
+ def attributes
53
+ instance_values.with_indifferent_access
54
+ end
56
55
 
57
- def attributes=(hash)
58
- hash.each do |key, value|
59
- if respond_to? "#{key}="
60
- send("#{key}=", value)
61
- else
62
- child_class = self.class.load_class(key)
63
- if child_class && child_class < Resource
64
- if value.is_a?(Hash)
65
- child_value = child_class.new(value)
66
- define_singleton_method(key) { child_value }
67
- elsif value.is_a?(Array)
68
- child_values = value.map { |h| child_class.new(h) }
69
- define_singleton_method(key) { child_values }
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
- def self.method_missing(m, *args, &_)
77
- unless args.empty?
78
- by_method_match = /^by_(.+)/.match(m)
79
- if by_method_match
80
- parent_resource_name = by_method_match[1]
81
- with_parent_resource(args[0], parent_resource_name)
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
- def self.with_parent_resource(parent_resource_id, parent_resource_name)
91
- result = client(:get, {}, parent_resource_name.pluralize, parent_resource_id, self.resource_name)
92
- hashes = JSON.parse(result)
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
- def self.client(verb, params, *paths)
97
- url = base_url
98
- url += '/' unless url.end_with?('/')
99
- url = url + paths.join('/')
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
- req = RestClient::Request.new(req_params)
108
- if hmac
109
- req.sign!(api_id, api_secret, auth_prefix)
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
@@ -1,3 +1,3 @@
1
1
  module ApiResource
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -1,6 +1,6 @@
1
- RSpec.describe Resource do
1
+ RSpec.describe ApiResource::Resource do
2
2
 
3
- class BlogResource < Resource
3
+ class BlogResource < ApiResource::Resource
4
4
  self.base_url = 'http://api.example.com'
5
5
  end
6
6
 
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.1.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.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.0'
26
+ version: '0.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rest-client
29
29
  requirement: !ruby/object:Gem::Requirement