json_api_client 0.1.3 → 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: b850c5b674039c387b45ecaa49c4800a667bf503
4
- data.tar.gz: b0a12cdba47effcf2e5708a64852445dc77b494c
3
+ metadata.gz: 10641ff8a17866b4787a265a7f2067fd3ed68e0c
4
+ data.tar.gz: 87292de4965d7692869bded6f648a317083d4770
5
5
  SHA512:
6
- metadata.gz: f4f52b341ab65f8993f90c347d0d114c91f1ab269d018190f3e937c38f08c19c725d1be84ef0751364c9b3655536556b05b5a928e19c251deb69c9a0cfa86e89
7
- data.tar.gz: d5df64cc6493f06792596c879bad18f031398bc306a6c8fe179d97ff10d8ffc2ce1fc032875b39e4fd030e70265cd4791a0edbc2f1964182a5df783bdaae9a15
6
+ metadata.gz: 6730d8e95fe347c9a5687cd9891f3e74cd7fe1e243103a20c7d3ab3afa217312e2baaedae2bbe7a5958f96e2929e1795a07cc4dd8b10cbad10ab1bbe169c3921
7
+ data.tar.gz: 0fa330613ac5400f7a077ec870c91cb507c78dc017b628a432ff7cd5c62bca0b49b9ddc67f163f742f1f7c6fba83e1e225d5d866ae2066fdb8fe33bee1a02b87
data/README.md CHANGED
@@ -113,6 +113,25 @@ module MyApi
113
113
  belongs_to :user
114
114
  end
115
115
  end
116
+ ```
117
+
118
+ ## Custom Methods
116
119
 
120
+ You can create custom methods on both collections (class method) and members (instance methods).
117
121
 
118
122
  ```
123
+ module MyApi
124
+ class User < JsonApiClient::Resource
125
+
126
+ # GET /users/search.json
127
+ custom_endpoint :search, on: :collection, request_method: :get
128
+
129
+ # PUT /users/:id/verify.json
130
+ custom_endpoint :verify, on: :member, request_method: :put
131
+ end
132
+ end
133
+ ```
134
+
135
+ In the above scenario, you can call the class method `MyApi::User.search`. The results will be parsed like any other query. If the response returns users, you will get back a `ResultSet` of `MyApi::User` instances.
136
+
137
+ You can also call the instance method `verify` on a `MyApi::User` instance.
@@ -0,0 +1,39 @@
1
+ module JsonApiClient
2
+ module Helpers
3
+ module CustomEndpoints
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def custom_endpoint(name, options = {})
8
+ if :collection == options.delete(:on)
9
+ collection_endpoint(name, options)
10
+ else
11
+ member_endpoint(name, options)
12
+ end
13
+ end
14
+
15
+ def collection_endpoint(name, options = {})
16
+ self.class.send(:define_method, name) do |*params|
17
+ input = {
18
+ name: name,
19
+ params: request_params = params.first || {}
20
+ }.merge(options)
21
+ run_request(Query::Custom.new(self, input))
22
+ end
23
+ end
24
+
25
+ def member_endpoint(name, options = {})
26
+ define_method name do |*params|
27
+ request_params = params.first || {}
28
+ request_params[self.class.primary_key] = attributes.fetch(primary_key)
29
+ input = {
30
+ name: name,
31
+ params: request_params
32
+ }.merge(options)
33
+ run_request(Query::Custom.new(self.class, input))
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -2,6 +2,7 @@ module JsonApiClient
2
2
  module Helpers
3
3
  autoload :Associable, 'json_api_client/helpers/associable'
4
4
  autoload :Attributable, 'json_api_client/helpers/attributable'
5
+ autoload :CustomEndpoints, 'json_api_client/helpers/custom_endpoints'
5
6
  autoload :Initializable, 'json_api_client/helpers/initializable'
6
7
  autoload :Linkable, 'json_api_client/helpers/linkable'
7
8
  autoload :Parsable, 'json_api_client/helpers/parsable'
@@ -0,0 +1,22 @@
1
+ module JsonApiClient
2
+ module Query
3
+ class Custom < Base
4
+
5
+ def request_method
6
+ @request_method
7
+ end
8
+
9
+ def path
10
+ [@path, @options[:name]].join("/")
11
+ end
12
+
13
+ def build_params(params)
14
+ opts = params.dup
15
+ @request_method = opts.delete(:request_method) || :get
16
+ @params = opts.delete(:params) || {}
17
+ @options = opts
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -2,6 +2,7 @@ module JsonApiClient
2
2
  module Query
3
3
  autoload :Base, 'json_api_client/query/base'
4
4
  autoload :Create, 'json_api_client/query/create'
5
+ autoload :Custom, 'json_api_client/query/custom'
5
6
  autoload :Destroy, 'json_api_client/query/destroy'
6
7
  autoload :Find, 'json_api_client/query/find'
7
8
  autoload :Update, 'json_api_client/query/update'
@@ -51,6 +51,7 @@ module JsonApiClient
51
51
  include Helpers::Queryable
52
52
  include Helpers::Serializable
53
53
  include Helpers::Linkable
54
+ include Helpers::CustomEndpoints
54
55
 
55
56
  def save
56
57
  query = persisted? ?
@@ -1,3 +1,3 @@
1
1
  module JsonApiClient
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Ching
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-15 00:00:00.000000000 Z
11
+ date: 2014-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -86,6 +86,7 @@ files:
86
86
  - lib/json_api_client/helpers.rb
87
87
  - lib/json_api_client/helpers/associable.rb
88
88
  - lib/json_api_client/helpers/attributable.rb
89
+ - lib/json_api_client/helpers/custom_endpoints.rb
89
90
  - lib/json_api_client/helpers/initializable.rb
90
91
  - lib/json_api_client/helpers/linkable.rb
91
92
  - lib/json_api_client/helpers/parsable.rb
@@ -98,6 +99,7 @@ files:
98
99
  - lib/json_api_client/query.rb
99
100
  - lib/json_api_client/query/base.rb
100
101
  - lib/json_api_client/query/create.rb
102
+ - lib/json_api_client/query/custom.rb
101
103
  - lib/json_api_client/query/destroy.rb
102
104
  - lib/json_api_client/query/find.rb
103
105
  - lib/json_api_client/query/update.rb
@@ -131,3 +133,4 @@ signing_key:
131
133
  specification_version: 4
132
134
  summary: Build client libraries compliant with specification defined by jsonapi.org
133
135
  test_files: []
136
+ has_rdoc: