rest_model 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/to_source/serializables.rb +8 -5
- data/lib/rest_model/configuration.rb +8 -0
- data/lib/rest_model/errors.rb +16 -0
- data/lib/rest_model/key/property/sender.rb +1 -1
- data/lib/rest_model/serialization/boolean.rb +2 -2
- data/lib/rest_model/serialization/date.rb +2 -2
- data/lib/rest_model/serialization/datetime.rb +19 -0
- data/lib/rest_model/serialization/enumerable.rb +4 -2
- data/lib/rest_model/serialization/float.rb +2 -2
- data/lib/rest_model/serialization/integer.rb +2 -2
- data/lib/rest_model/source/sender.rb +9 -1
- data/lib/rest_model/source/translation.rb +8 -2
- data/lib/rest_model/version.rb +1 -1
- data/lib/rest_model.rb +2 -0
- data/spec/unit/serialization/datetime_spec.rb +13 -0
- data/spec/unit/source/retriever_spec.rb +1 -1
- metadata +19 -14
@@ -4,20 +4,23 @@ RestModel::Configuration.configure do |c|
|
|
4
4
|
c.true_value = "X"
|
5
5
|
c.false_value = ""
|
6
6
|
c.date_format = "%Y%m%d"
|
7
|
+
c.datetime_format = "%a %b %d %H:%M:%S %Y"
|
7
8
|
end
|
8
9
|
|
9
10
|
class Customer < RestModel
|
10
11
|
property :login
|
11
|
-
property :age,
|
12
|
-
property :birth,
|
13
|
-
property :
|
14
|
-
property :
|
15
|
-
property :
|
12
|
+
property :age, type: Integer
|
13
|
+
property :birth, type: Date
|
14
|
+
property :created_at, type: DateTime
|
15
|
+
property :active, type: Boolean
|
16
|
+
property :services, type: Enumerable
|
17
|
+
property :balance, type: Float
|
16
18
|
end
|
17
19
|
|
18
20
|
@root = Customer.new(login: 'jackiechan2010',
|
19
21
|
age: 22,
|
20
22
|
birth: "1990-07-04",
|
23
|
+
created_at: DateTime.now,
|
21
24
|
active: true,
|
22
25
|
services: ["Hosting", "Email"],
|
23
26
|
balance: 200.00)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class RestModel
|
2
|
+
class SerializationError < StandardError; end
|
3
|
+
class TranslationError < StandardError; end
|
4
|
+
|
5
|
+
class SourceError < StandardError
|
6
|
+
attr_reader :errors
|
7
|
+
|
8
|
+
def initialize(errors)
|
9
|
+
@errors = errors
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.exception(errors = {})
|
13
|
+
new(errors)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -5,7 +5,7 @@ class RestModel
|
|
5
5
|
source_value = begin
|
6
6
|
translation.translates_to_source? ? translation.translate_to_source(value, resource)
|
7
7
|
: serializer.desserialize(value)
|
8
|
-
rescue
|
8
|
+
rescue TranslationError, SerializationError
|
9
9
|
raise exception if options[:fail]
|
10
10
|
end
|
11
11
|
|
@@ -27,12 +27,12 @@ class RestModel
|
|
27
27
|
|
28
28
|
def self.serialize(value)
|
29
29
|
MAPPINGS[value].tap do |bool|
|
30
|
-
fail "value
|
30
|
+
fail SerializationError, "value '#{value}' is not boolean" if bool.nil?
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
def self.desserialize(value)
|
35
|
-
fail unless boolean?(value)
|
35
|
+
fail SerializationError, "value '#{value}' is not boolean" unless boolean?(value)
|
36
36
|
|
37
37
|
value ? RestModel::Configuration.true_value
|
38
38
|
: RestModel::Configuration.false_value
|
@@ -4,7 +4,7 @@ class RestModel
|
|
4
4
|
def self.serialize(value)
|
5
5
|
::Date.parse value
|
6
6
|
rescue ArgumentError
|
7
|
-
raise "value
|
7
|
+
raise SerializationError, "value '#{value}' is an invalid date"
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.desserialize(value)
|
@@ -12,7 +12,7 @@ class RestModel
|
|
12
12
|
format = RestModel::Configuration.date_format
|
13
13
|
format ? date.strftime(format) : date.to_s
|
14
14
|
rescue ArgumentError
|
15
|
-
raise "value
|
15
|
+
raise SerializationError, "value '#{value}' is an invalid date"
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class RestModel
|
2
|
+
module Serialization
|
3
|
+
class DateTime
|
4
|
+
def self.serialize(value)
|
5
|
+
::DateTime.parse value
|
6
|
+
rescue ArgumentError
|
7
|
+
raise SerializationError, "value '#{value}' is an invalid datetime"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.desserialize(value)
|
11
|
+
datetime = value.kind_of?(::DateTime) ? value : ::DateTime.parse(value)
|
12
|
+
format = RestModel::Configuration.datetime_format
|
13
|
+
format ? datetime.strftime(format) : datetime.to_s
|
14
|
+
rescue ArgumentError
|
15
|
+
raise SerializationError, "value '#{value}' is an invalid datetime"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -2,12 +2,14 @@ class RestModel
|
|
2
2
|
module Serialization
|
3
3
|
class Enumerable
|
4
4
|
def self.serialize(value)
|
5
|
-
fail "value
|
5
|
+
fail SerializationError, "value '#{value}' is not an enumerable" unless value.kind_of?(::Enumerable)
|
6
|
+
|
6
7
|
value
|
7
8
|
end
|
8
9
|
|
9
10
|
def self.desserialize(value)
|
10
|
-
fail "value
|
11
|
+
fail SerializationError, "value '#{value}' is not an enumerable" unless value.kind_of?(::Enumerable)
|
12
|
+
|
11
13
|
value
|
12
14
|
end
|
13
15
|
end
|
@@ -4,13 +4,13 @@ class RestModel
|
|
4
4
|
def self.serialize(value)
|
5
5
|
Float(value)
|
6
6
|
rescue ArgumentError
|
7
|
-
raise "value
|
7
|
+
raise SerializationError, "value '#{value}' is not a float"
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.desserialize(value)
|
11
11
|
Float(value)
|
12
12
|
rescue ArgumentError
|
13
|
-
raise "value
|
13
|
+
raise SerializationError, "value '#{value}' is not a float"
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -4,13 +4,13 @@ class RestModel
|
|
4
4
|
def self.serialize(value)
|
5
5
|
Integer(value)
|
6
6
|
rescue ArgumentError
|
7
|
-
raise "value
|
7
|
+
raise SerializationError, "value '#{value}' is not an integer"
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.desserialize(value)
|
11
11
|
Integer(value)
|
12
12
|
rescue ArgumentError
|
13
|
-
raise "value
|
13
|
+
raise SerializationError, "value '#{value}' is not an integer"
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -7,14 +7,22 @@ class RestModel
|
|
7
7
|
|
8
8
|
def to_source(options = {})
|
9
9
|
source = {}
|
10
|
+
errors = {}
|
10
11
|
root_options = {without_nil: options[:without_nil], fail: options[:fail]}
|
11
12
|
|
12
13
|
keys_to_source(options).each do |key|
|
13
14
|
value = __send__(key.name)
|
14
15
|
key_options = options.fetch(key.name, {}).merge(root_options)
|
15
|
-
|
16
|
+
|
17
|
+
begin
|
18
|
+
source.merge! key.to_source!(value, self, key_options)
|
19
|
+
rescue TranslationError, SerializationError => error
|
20
|
+
errors[key.name] = error.message
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
24
|
+
fail SourceError, errors unless errors.empty? if options[:fail]
|
25
|
+
|
18
26
|
source.with_indifferent_access
|
19
27
|
end
|
20
28
|
|
@@ -17,7 +17,10 @@ class RestModel
|
|
17
17
|
if from_source
|
18
18
|
resource.instance_exec(value, &from_source)
|
19
19
|
elsif values
|
20
|
-
|
20
|
+
unless values.has_value?(value)
|
21
|
+
fail TranslationError, "value '#{value}' doesn't belong to values: #{values.values}"
|
22
|
+
end
|
23
|
+
|
21
24
|
values.key(value)
|
22
25
|
else
|
23
26
|
value
|
@@ -32,7 +35,10 @@ class RestModel
|
|
32
35
|
if to_source
|
33
36
|
resource.instance_exec(value, &to_source)
|
34
37
|
elsif values
|
35
|
-
|
38
|
+
unless values.has_key?(value)
|
39
|
+
fail TranslationError, "value '#{value}' doesn't belong to values: #{values.keys}"
|
40
|
+
end
|
41
|
+
|
36
42
|
values[value]
|
37
43
|
else
|
38
44
|
value
|
data/lib/rest_model/version.rb
CHANGED
data/lib/rest_model.rb
CHANGED
@@ -15,6 +15,7 @@ require "rest_model/source/sender"
|
|
15
15
|
require "rest_model/response"
|
16
16
|
require "rest_model/serialization/boolean"
|
17
17
|
require "rest_model/serialization/date"
|
18
|
+
require "rest_model/serialization/datetime"
|
18
19
|
require "rest_model/serialization/enumerable"
|
19
20
|
require "rest_model/serialization/float"
|
20
21
|
require "rest_model/serialization/integer"
|
@@ -30,6 +31,7 @@ require "rest_model/key/embeddable/builder"
|
|
30
31
|
require "rest_model/key/href"
|
31
32
|
require "rest_model/key/builder"
|
32
33
|
require "rest_model/configuration"
|
34
|
+
require "rest_model/errors"
|
33
35
|
|
34
36
|
class RestModel
|
35
37
|
extend Key::Builder
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RestModel::Serialization::DateTime do
|
4
|
+
it "converts to datetime" do
|
5
|
+
subject.class.serialize('2011-07-01T10:20:30').should == DateTime.new(2011, 07, 01, 10, 20, 30)
|
6
|
+
end
|
7
|
+
|
8
|
+
context "when an invalid value is serialized" do
|
9
|
+
it "raises an error" do
|
10
|
+
expect {subject.class.serialize("not a datetime")}.to raise_error
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -85,7 +85,7 @@ describe RestModel::Source::Retriever do
|
|
85
85
|
|
86
86
|
context "when calling from_source!" do
|
87
87
|
it "fails when value wasn't mapped" do
|
88
|
-
expect {Example.from_source!({status: '03'})}.to raise_error
|
88
|
+
expect {Example.from_source!({status: '03'})}.to raise_error
|
89
89
|
end
|
90
90
|
end
|
91
91
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,11 +11,11 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2011-
|
14
|
+
date: 2011-11-03 00:00:00.000000000Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
18
|
-
requirement: &
|
18
|
+
requirement: &70151888329840 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
@@ -23,10 +23,10 @@ dependencies:
|
|
23
23
|
version: '3.0'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *70151888329840
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: i18n
|
29
|
-
requirement: &
|
29
|
+
requirement: &70151882162800 !ruby/object:Gem::Requirement
|
30
30
|
none: false
|
31
31
|
requirements:
|
32
32
|
- - ! '>='
|
@@ -34,10 +34,10 @@ dependencies:
|
|
34
34
|
version: '0.5'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
|
-
version_requirements: *
|
37
|
+
version_requirements: *70151882162800
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
39
|
name: rspec
|
40
|
-
requirement: &
|
40
|
+
requirement: &70151882162340 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
43
|
- - ~>
|
@@ -45,10 +45,10 @@ dependencies:
|
|
45
45
|
version: '2.6'
|
46
46
|
type: :development
|
47
47
|
prerelease: false
|
48
|
-
version_requirements: *
|
48
|
+
version_requirements: *70151882162340
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
50
|
name: guard
|
51
|
-
requirement: &
|
51
|
+
requirement: &70151882161880 !ruby/object:Gem::Requirement
|
52
52
|
none: false
|
53
53
|
requirements:
|
54
54
|
- - ~>
|
@@ -56,10 +56,10 @@ dependencies:
|
|
56
56
|
version: '0.5'
|
57
57
|
type: :development
|
58
58
|
prerelease: false
|
59
|
-
version_requirements: *
|
59
|
+
version_requirements: *70151882161880
|
60
60
|
- !ruby/object:Gem::Dependency
|
61
61
|
name: guard-rspec
|
62
|
-
requirement: &
|
62
|
+
requirement: &70151882161420 !ruby/object:Gem::Requirement
|
63
63
|
none: false
|
64
64
|
requirements:
|
65
65
|
- - ~>
|
@@ -67,10 +67,10 @@ dependencies:
|
|
67
67
|
version: '0.4'
|
68
68
|
type: :development
|
69
69
|
prerelease: false
|
70
|
-
version_requirements: *
|
70
|
+
version_requirements: *70151882161420
|
71
71
|
- !ruby/object:Gem::Dependency
|
72
72
|
name: growl
|
73
|
-
requirement: &
|
73
|
+
requirement: &70151882160960 !ruby/object:Gem::Requirement
|
74
74
|
none: false
|
75
75
|
requirements:
|
76
76
|
- - ~>
|
@@ -78,7 +78,7 @@ dependencies:
|
|
78
78
|
version: '1.0'
|
79
79
|
type: :development
|
80
80
|
prerelease: false
|
81
|
-
version_requirements: *
|
81
|
+
version_requirements: *70151882160960
|
82
82
|
description: ''
|
83
83
|
email:
|
84
84
|
- victorcrodrigues@gmail.com
|
@@ -143,6 +143,7 @@ files:
|
|
143
143
|
- examples/update_attributes/simple.rb
|
144
144
|
- lib/rest_model.rb
|
145
145
|
- lib/rest_model/configuration.rb
|
146
|
+
- lib/rest_model/errors.rb
|
146
147
|
- lib/rest_model/key.rb
|
147
148
|
- lib/rest_model/key/association.rb
|
148
149
|
- lib/rest_model/key/builder.rb
|
@@ -164,6 +165,7 @@ files:
|
|
164
165
|
- lib/rest_model/response.rb
|
165
166
|
- lib/rest_model/serialization/boolean.rb
|
166
167
|
- lib/rest_model/serialization/date.rb
|
168
|
+
- lib/rest_model/serialization/datetime.rb
|
167
169
|
- lib/rest_model/serialization/enumerable.rb
|
168
170
|
- lib/rest_model/serialization/float.rb
|
169
171
|
- lib/rest_model/serialization/integer.rb
|
@@ -204,6 +206,7 @@ files:
|
|
204
206
|
- spec/unit/rest_model_spec.rb
|
205
207
|
- spec/unit/serialization/boolean_spec.rb
|
206
208
|
- spec/unit/serialization/date_spec.rb
|
209
|
+
- spec/unit/serialization/datetime_spec.rb
|
207
210
|
- spec/unit/serialization/float_spec.rb
|
208
211
|
- spec/unit/serialization/integer_spec.rb
|
209
212
|
- spec/unit/serialization/string_spec.rb
|
@@ -263,8 +266,10 @@ test_files:
|
|
263
266
|
- spec/unit/rest_model_spec.rb
|
264
267
|
- spec/unit/serialization/boolean_spec.rb
|
265
268
|
- spec/unit/serialization/date_spec.rb
|
269
|
+
- spec/unit/serialization/datetime_spec.rb
|
266
270
|
- spec/unit/serialization/float_spec.rb
|
267
271
|
- spec/unit/serialization/integer_spec.rb
|
268
272
|
- spec/unit/serialization/string_spec.rb
|
269
273
|
- spec/unit/source/path_spec.rb
|
270
274
|
- spec/unit/source/retriever_spec.rb
|
275
|
+
has_rdoc:
|