json_api_client 1.0.0.beta2 → 1.0.0.beta3

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: 738f41ec73db3cf6cdf208abb4ad48ea7fc50e39
4
- data.tar.gz: a0b6dfce9e509e9d7ac206ba742ce3b52e41a04e
3
+ metadata.gz: 1af573eac7e606a57e398c563b46b41ed2f4aea0
4
+ data.tar.gz: 0c6b27e6af8fd78b449a253226c1a37e9c83c81b
5
5
  SHA512:
6
- metadata.gz: d20ef7885deb0589bbb6d6d7fa89d40ef7dbf192e1461ba006f4124c0986a581ce0da11d1bf5d9ce308610ef142effcc11ceb7aa4a91f78752db9f511357837e
7
- data.tar.gz: 2dd89bd5ea1b889f71a1b62826f087ebb30078a38f20218483fdf119928a3e0f724db2f10fb2dfcef58dba7ab3f48b108d7c9ade2133f469aed8dbf4c47dbaaf
6
+ metadata.gz: 5cefa69d519d148e9298e36a0bba1e11605a59b841e34d9eb5c213ed7e56eecfe6e6a86914584b84777e513d707c6b4cebd597b701406cdfc9d33a89454e19e3
7
+ data.tar.gz: 7ee4e4c5e432f24f232349ce0ee4565fdec370c00de50b5ba1f27291dee3bc9aa9095608a9ad48e6ea0b35b971128248dbfa94dd3b5bc9526f0a386092d9163d
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This gem is meant to help you build an API client for interacting with REST APIs as laid out by [http://jsonapi.org](http://jsonapi.org). It attempts to give you a query building framework that is easy to understand (it is similar to ActiveRecord scopes).
4
4
 
5
- *Note: master is currently tracking the 1.0.0 RC3 specification. If you're looking for the older code, see [0.x branch](https://github.com/chingor13/json_api_client/tree/0.x)*
5
+ *Note: master is currently tracking the 1.0.0 specification. If you're looking for the older code, see [0.x branch](https://github.com/chingor13/json_api_client/tree/0.x)*
6
6
 
7
7
  *Note: This is still a work in progress.*
8
8
 
@@ -31,7 +31,7 @@ end
31
31
 
32
32
  By convention, we figure guess the resource route from the class name. In the above example, `Article`'s path is "http://example.com/articles" and `Person`'s path would be "http://example.com/people".
33
33
 
34
- Some example usage:
34
+ Some basic example usage:
35
35
 
36
36
  ```
37
37
  MyApi::Article.all
@@ -60,6 +60,8 @@ All class level finders/creators should return a `JsonApiClient::ResultSet` whic
60
60
 
61
61
  ## Handling Validation Errors
62
62
 
63
+ [See specification](http://jsonapi.org/format/#errors)
64
+
63
65
  Out of the box, `json_api_client` handles server side validation only.
64
66
 
65
67
  ```
@@ -69,9 +71,19 @@ User.create(name: "Bob", email_address: "invalid email")
69
71
  user = User.new(name: "Bob", email_address: "invalid email")
70
72
  user.save
71
73
  => false
74
+
75
+ # returns an error collector which is array-like
72
76
  user.errors
73
77
  => ["Email address is invalid"]
74
78
 
79
+ # get all error titles
80
+ user.errors.full_messages
81
+ => ["Email address is invalid"]
82
+
83
+ # get errors for a specific parameter
84
+ user.errors[:email_address]
85
+ => ["Email address is invalid"]
86
+
75
87
  user = User.find(1)
76
88
  user.update_attributes(email_address: "invalid email")
77
89
  => false
@@ -81,6 +93,8 @@ user.email_address
81
93
  => "invalid email"
82
94
  ```
83
95
 
96
+ For now we are assuming that error sources are all parameters.
97
+
84
98
  If you want to add client side validation, I suggest creating a form model class that uses ActiveModel's validations.
85
99
 
86
100
  ## Meta information
@@ -204,10 +218,10 @@ article.created_at
204
218
  [See specification](http://jsonapi.org/format/#fetching-sorting)
205
219
 
206
220
  ```
207
- # makes request to /people?sort=+age
221
+ # makes request to /people?sort=age
208
222
  youngest = Person.sort(:age).all
209
223
 
210
- # also makes request to /people?sort=+age
224
+ # also makes request to /people?sort=age
211
225
  youngest = Person.sort(age: :asc).all
212
226
 
213
227
  # makes request to /people?sort=-age
@@ -228,7 +242,7 @@ articles = Article.page(2).per(30).to_a
228
242
  articles = Article.paginate(page: 2, per_page: 30).to_a
229
243
  ```
230
244
 
231
- *Note: The mapping of pagination parameters is done by the `query_builder` which is [customizable](#fixme).*
245
+ *Note: The mapping of pagination parameters is done by the `query_builder` which is [customizable](#custom-paginator).*
232
246
 
233
247
  ### Browsing
234
248
 
@@ -299,6 +313,23 @@ Also, we consider `nil` to be an acceptable value and will not cast the value.
299
313
 
300
314
  ## Customizing
301
315
 
316
+ ### Paths
317
+
318
+ You can customize this path by changing your resource's `table_name`:
319
+
320
+ ```
321
+ module MyApi
322
+ class SomeResource < Base
323
+ def self.table_name
324
+ "foobar"
325
+ end
326
+ end
327
+ end
328
+
329
+ # requests http://example.com/foobar
330
+ MyApi::SomeResource.all
331
+ ```
332
+
302
333
  ### Connections
303
334
 
304
335
  You can configure your API client to use a custom connection that implementes the `run` instance method. It should return data that your parser can handle. The default connection class wraps Faraday and lets you add middleware.
@@ -6,7 +6,7 @@ module JsonApiClient
6
6
  included do
7
7
  class << self
8
8
  extend Forwardable
9
- def_delegators :new_scope, :where, :order, :includes, :select, :all, :paginate, :page, :first
9
+ def_delegators :new_scope, :where, :order, :includes, :select, :all, :paginate, :page, :first, :find
10
10
  end
11
11
  class_attribute :connection_class, :connection_object, :connection_options, :query_builder
12
12
  self.connection_class = Connection
@@ -10,10 +10,6 @@ module JsonApiClient
10
10
  end
11
11
 
12
12
  module ClassMethods
13
- def find(conditions)
14
- requestor.find(conditions)
15
- end
16
-
17
13
  def create(conditions = {})
18
14
  new(conditions).tap do |resource|
19
15
  resource.save
@@ -6,6 +6,7 @@ module JsonApiClient
6
6
 
7
7
  def initialize(klass)
8
8
  @klass = klass
9
+ @primary_key = nil
9
10
  @pagination_params = {}
10
11
  @filters = {}
11
12
  @includes = []
@@ -64,19 +65,39 @@ module JsonApiClient
64
65
  .merge(includes_params)
65
66
  .merge(order_params)
66
67
  .merge(select_params)
68
+ .merge(primary_key_params)
67
69
  end
68
70
 
69
71
  def to_a
70
- @to_a ||= klass.find(params)
72
+ @to_a ||= find
71
73
  end
72
74
  alias all to_a
73
75
 
76
+ def find(args = {})
77
+ case args
78
+ when Hash
79
+ where(args)
80
+ else
81
+ @primary_key = args
82
+ end
83
+
84
+ klass.requestor.get(params)
85
+ end
86
+
74
87
  def method_missing(method_name, *args, &block)
75
88
  to_a.send(method_name, *args, &block)
76
89
  end
77
90
 
78
91
  private
79
92
 
93
+ def primary_key_params
94
+ return {} unless @primary_key
95
+
96
+ @primary_key.is_a?(Array) ?
97
+ {klass.primary_key.to_s.pluralize.to_sym => @primary_key.join(",")} :
98
+ {klass.primary_key => @primary_key}
99
+ end
100
+
80
101
  def pagination_params
81
102
  @pagination_params.empty? ? {} : {page: @pagination_params}
82
103
  end
@@ -121,11 +142,11 @@ module JsonApiClient
121
142
  case arg
122
143
  when Hash
123
144
  arg.map do |k, v|
124
- operator = (v == :desc ? "-" : "+")
145
+ operator = (v == :desc ? "-" : "")
125
146
  "#{operator}#{k}"
126
147
  end
127
148
  else
128
- "+#{arg}"
149
+ "#{arg}"
129
150
  end
130
151
  end.flatten
131
152
  end
@@ -20,16 +20,7 @@ module JsonApiClient
20
20
  })
21
21
  end
22
22
 
23
- def find(args)
24
- params = case args
25
- when Hash
26
- args
27
- when Array
28
- {klass.primary_key.to_s.pluralize.to_sym => args.join(",")}
29
- else
30
- {klass.primary_key => args}
31
- end
32
-
23
+ def get(params = {})
33
24
  path = resource_path(params)
34
25
  params.delete(klass.primary_key)
35
26
  request(:get, path, params)
@@ -1,3 +1,3 @@
1
1
  module JsonApiClient
2
- VERSION = "1.0.0.beta2"
2
+ VERSION = "1.0.0.beta3"
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: 1.0.0.beta2
4
+ version: 1.0.0.beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Ching
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-02 00:00:00.000000000 Z
11
+ date: 2015-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport