json_api_client 0.9.0 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json_api_client/helpers/attributable.rb +1 -1
- data/lib/json_api_client/schema.rb +30 -13
- data/lib/json_api_client/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bae5dacd58847759a00a4bb8ffd01e34d7752957
|
4
|
+
data.tar.gz: 2815de325108b31b33ca49a4d2ff826ad2b7b0c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e60688cf07d575fc8984967c4368f36ee0810519ed7501ffd433d496d27366f5c8d64bfc4c0f8bdf59513ccf42cd3a5f56c801e060f9a4efdb564649fc1a13f4
|
7
|
+
data.tar.gz: 953fad49c530d284756742cdeba6128b196e58a5321060d21fd77269d49c8a22bdd7accb3ec6e814c8b56d0388fd35c0eb6f55e91c907bd697fc67086f243bef
|
@@ -1,23 +1,40 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
1
3
|
module JsonApiClient
|
2
4
|
class Schema
|
5
|
+
DEFAULT_PROPERTY_TYPES = {
|
6
|
+
int: lambda {|value| value.to_i },
|
7
|
+
integer: lambda {|value| value.to_i },
|
8
|
+
string: lambda {|value| value.to_s },
|
9
|
+
float: lambda {|value| value.to_f },
|
10
|
+
boolean: lambda {|value| value.is_a?(String) ? (value != "false") : !!value },
|
11
|
+
timestamp: lambda {|value| value.is_a?(DateTime) ? value : Time.at(value.to_f).to_datetime },
|
12
|
+
timestamp_ms: lambda {|value| value.is_a?(DateTime) ? value : Time.at(value.to_f/1000).to_datetime },
|
13
|
+
datetime: lambda {|value| value.is_a?(DateTime) ? value : DateTime.parse(value.to_s) },
|
14
|
+
date: lambda {|value| value.is_a?(Date) ? value : Date.parse(value.to_s) }
|
15
|
+
}
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def property_types
|
19
|
+
@property_types ||= DEFAULT_PROPERTY_TYPES
|
20
|
+
end
|
21
|
+
|
22
|
+
def register_property_type(name, caster)
|
23
|
+
property_types[name.to_sym] = caster
|
24
|
+
end
|
25
|
+
|
26
|
+
def find_property_type(name)
|
27
|
+
property_types[name.to_sym]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
3
31
|
Property = Struct.new(:name, :type, :default) do
|
4
32
|
def cast(value)
|
5
33
|
return nil if value.nil?
|
6
34
|
return value if type.nil?
|
7
35
|
|
8
|
-
|
9
|
-
|
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
|
36
|
+
if caster = Schema.find_property_type(type)
|
37
|
+
caster.call(value)
|
21
38
|
else
|
22
39
|
value
|
23
40
|
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.9.
|
4
|
+
version: 0.9.2
|
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-
|
11
|
+
date: 2015-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|