json_api_client 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +44 -1
- data/lib/json_api_client/helpers/attributable.rb +3 -1
- data/lib/json_api_client/helpers/schemable.rb +68 -0
- data/lib/json_api_client/helpers.rb +1 -0
- data/lib/json_api_client/resource.rb +1 -0
- data/lib/json_api_client/schema.rb +64 -0
- data/lib/json_api_client/version.rb +1 -1
- data/lib/json_api_client.rb +1 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eafc20bde2b50f635c58ba67bd72c3234d3cf098
|
4
|
+
data.tar.gz: db4d71324f20cd39c03677afb1d089001ceac8cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0496018b36ad43f465f18f96b80d7361223cc70b0e3a0d956030ed19b756b9e72be62b7f85b7c98f7faea442d35f37ccf24664134d0b8338c50e016d1330cddf
|
7
|
+
data.tar.gz: ba8475927ba6c4f2af4b96aa84c5a4674e40b2c3a03255e471205d435a74b3dc0929f39d71d4573aee1821650a7cfd6faaea86c7c04dd473bf7f4a6740b3c948
|
data/README.md
CHANGED
@@ -159,4 +159,47 @@ You can also call the instance method `verify` on a `MyApi::User` instance.
|
|
159
159
|
|
160
160
|
We also respect the [links specification](http://jsonapi.org/format/#document-structure-resource-relationships). The client can fetch linked resources based on the defined endpoint from the link specification as well as load data from any `linked` data provided in the response. Additionally, it will still fetch missing data if not all linked resources are provided in the `linked` data response.
|
161
161
|
|
162
|
-
See the [tests](https://github.com/chingor13/json_api_client/blob/master/test/unit/links_test.rb).
|
162
|
+
See the [tests](https://github.com/chingor13/json_api_client/blob/master/test/unit/links_test.rb).
|
163
|
+
|
164
|
+
## Schema
|
165
|
+
|
166
|
+
You can define schema within your client model. You can define basic types and set default values if you wish. If you declare a basic type, we will try to cast any input to be that type.
|
167
|
+
|
168
|
+
The added benefit of declaring your schema is that you can access fields before data is set (otherwise, you'll get a `NoMethodError`).
|
169
|
+
|
170
|
+
### Example
|
171
|
+
|
172
|
+
```
|
173
|
+
class User < JsonApiClient::Resource
|
174
|
+
property :name, type: :string
|
175
|
+
property :is_admin, type: :boolean, default: false
|
176
|
+
property :points_accrued, type: :int, default: 0
|
177
|
+
property :averge_points_per_day, type: :float
|
178
|
+
end
|
179
|
+
|
180
|
+
# default values
|
181
|
+
u = User.new
|
182
|
+
u.name
|
183
|
+
=> nil
|
184
|
+
u.is_admin
|
185
|
+
=> false
|
186
|
+
u.points_accrued
|
187
|
+
=> 0
|
188
|
+
|
189
|
+
# casting
|
190
|
+
u.average_points_per_day = "0.3"
|
191
|
+
u.average_points_per_day
|
192
|
+
=> 0.3
|
193
|
+
|
194
|
+
```
|
195
|
+
|
196
|
+
### Types
|
197
|
+
|
198
|
+
The basic types that we allow are:
|
199
|
+
|
200
|
+
* `:int` or `:integer`
|
201
|
+
* `:float`
|
202
|
+
* `:string`
|
203
|
+
* `:boolean` - *Note: we will cast the string version of "true" and "false" to their respective values*
|
204
|
+
|
205
|
+
Also, we consider `nil` to be an acceptable value and will not cast the value.
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module JsonApiClient
|
2
|
+
module Helpers
|
3
|
+
module Schemable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
initializer do |obj, params|
|
8
|
+
obj.send(:set_default_values)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
# Returns the schema for this resource class
|
14
|
+
#
|
15
|
+
# @return [Schema] the schema for this resource class
|
16
|
+
def schema
|
17
|
+
@schema ||= Schema.new
|
18
|
+
end
|
19
|
+
|
20
|
+
# Declares a new property by name
|
21
|
+
#
|
22
|
+
# @param name [Symbol] the name of the property
|
23
|
+
# @param options [Hash] property options
|
24
|
+
# @option options [Symbol] :type The property type
|
25
|
+
# @option options [Symbol] :default The default value for the property
|
26
|
+
def property(name, options = {})
|
27
|
+
schema.add(name, options)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Declare multiple properties with the same optional options
|
31
|
+
#
|
32
|
+
# @param [Array<Symbol>] names
|
33
|
+
# @param options [Hash] property options
|
34
|
+
# @option options [Symbol] :type The property type
|
35
|
+
# @option options [Symbol] :default The default value for the property
|
36
|
+
def properties(*names)
|
37
|
+
options = names.last.is_a?(Hash) ? names.pop : {}
|
38
|
+
names.each do |name|
|
39
|
+
property name, options
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
|
46
|
+
def set_attribute(name, value)
|
47
|
+
property = property_for(name)
|
48
|
+
value = property.cast(value) if property
|
49
|
+
super(name, value)
|
50
|
+
end
|
51
|
+
|
52
|
+
def has_attribute?(attr_name)
|
53
|
+
!!property_for(attr_name) || super
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_default_values
|
57
|
+
self.class.schema.each_property do |property|
|
58
|
+
attributes[property.name] = property.default unless attributes.has_key?(property.name)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def property_for(name)
|
63
|
+
self.class.schema.find(name)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -7,6 +7,7 @@ module JsonApiClient
|
|
7
7
|
autoload :Linkable, 'json_api_client/helpers/linkable'
|
8
8
|
autoload :Parsable, 'json_api_client/helpers/parsable'
|
9
9
|
autoload :Queryable, 'json_api_client/helpers/queryable'
|
10
|
+
autoload :Schemable, 'json_api_client/helpers/schemable'
|
10
11
|
autoload :Serializable, 'json_api_client/helpers/serializable'
|
11
12
|
end
|
12
13
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module JsonApiClient
|
2
|
+
class Schema
|
3
|
+
Property = Struct.new(:name, :type, :default) do
|
4
|
+
def cast(value)
|
5
|
+
return nil if value.nil?
|
6
|
+
return value if type.nil?
|
7
|
+
|
8
|
+
case type.to_sym
|
9
|
+
when :int, :integer
|
10
|
+
value.to_i
|
11
|
+
when :string
|
12
|
+
value.to_s
|
13
|
+
when :float
|
14
|
+
value.to_f
|
15
|
+
when :boolean
|
16
|
+
if value.is_a?(String)
|
17
|
+
value == "false" ? false : true
|
18
|
+
else
|
19
|
+
!!value
|
20
|
+
end
|
21
|
+
else
|
22
|
+
value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize
|
28
|
+
@properties = {}
|
29
|
+
end
|
30
|
+
|
31
|
+
# Add a property to the schema
|
32
|
+
#
|
33
|
+
# @param name [Symbol] the name of the property
|
34
|
+
# @param options [Hash] property options
|
35
|
+
# @option options [Symbol] :type The property type
|
36
|
+
# @option options [Symbol] :default The default value for the property
|
37
|
+
# @return [void]
|
38
|
+
def add(name, options)
|
39
|
+
@properties[name.to_sym] = Property.new(name.to_sym, options[:type], options[:default])
|
40
|
+
end
|
41
|
+
|
42
|
+
# How many properties are defined
|
43
|
+
#
|
44
|
+
# @return [Fixnum] the number of defined properties
|
45
|
+
def size
|
46
|
+
@properties.size
|
47
|
+
end
|
48
|
+
alias_method :length, :size
|
49
|
+
|
50
|
+
def each_property(&block)
|
51
|
+
@properties.values.each(&block)
|
52
|
+
end
|
53
|
+
alias_method :each, :each_property
|
54
|
+
|
55
|
+
# Look up a property by name
|
56
|
+
#
|
57
|
+
# @param property_name [String] the name of the property
|
58
|
+
# @return [Property, nil] the property definition for property_name or nil
|
59
|
+
def find(property_name)
|
60
|
+
@properties[property_name.to_sym]
|
61
|
+
end
|
62
|
+
alias_method :[], :find
|
63
|
+
end
|
64
|
+
end
|
data/lib/json_api_client.rb
CHANGED
@@ -14,6 +14,7 @@ module JsonApiClient
|
|
14
14
|
autoload :Query, 'json_api_client/query'
|
15
15
|
autoload :Resource, 'json_api_client/resource'
|
16
16
|
autoload :ResultSet, 'json_api_client/result_set'
|
17
|
+
autoload :Schema, 'json_api_client/schema'
|
17
18
|
autoload :Scope, 'json_api_client/scope'
|
18
19
|
autoload :Utils, 'json_api_client/utils'
|
19
20
|
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.
|
4
|
+
version: 0.5.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-08-
|
11
|
+
date: 2014-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -91,6 +91,7 @@ files:
|
|
91
91
|
- lib/json_api_client/helpers/linkable.rb
|
92
92
|
- lib/json_api_client/helpers/parsable.rb
|
93
93
|
- lib/json_api_client/helpers/queryable.rb
|
94
|
+
- lib/json_api_client/helpers/schemable.rb
|
94
95
|
- lib/json_api_client/helpers/serializable.rb
|
95
96
|
- lib/json_api_client/link.rb
|
96
97
|
- lib/json_api_client/link_definition.rb
|
@@ -110,6 +111,7 @@ files:
|
|
110
111
|
- lib/json_api_client/query/update.rb
|
111
112
|
- lib/json_api_client/resource.rb
|
112
113
|
- lib/json_api_client/result_set.rb
|
114
|
+
- lib/json_api_client/schema.rb
|
113
115
|
- lib/json_api_client/scope.rb
|
114
116
|
- lib/json_api_client/utils.rb
|
115
117
|
- lib/json_api_client/version.rb
|