halibut 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +11 -0
- data/lib/halibut/link.rb +10 -0
- data/lib/halibut/relation_map.rb +4 -0
- data/lib/halibut/resource.rb +54 -14
- data/lib/halibut/version.rb +1 -1
- data/spec/fixtures/serialize.json +23 -0
- data/spec/link_spec.rb +1 -1
- data/spec/resource_spec.rb +21 -0
- data/spec/spec_helper.rb +1 -0
- metadata +5 -2
data/.travis.yml
ADDED
data/lib/halibut/link.rb
CHANGED
@@ -41,6 +41,16 @@ module Halibut
|
|
41
41
|
@title = opts[:title]
|
42
42
|
@hreflang = opts[:hreflang]
|
43
43
|
end
|
44
|
+
|
45
|
+
def ==(other)
|
46
|
+
@href == other.href &&
|
47
|
+
@templated == other.templated &&
|
48
|
+
@type == other.type &&
|
49
|
+
@name == other.name &&
|
50
|
+
@profile == other.profile &&
|
51
|
+
@title == other.title &&
|
52
|
+
@hreflang == other.hreflang
|
53
|
+
end
|
44
54
|
end
|
45
55
|
|
46
56
|
end
|
data/lib/halibut/relation_map.rb
CHANGED
data/lib/halibut/resource.rb
CHANGED
@@ -5,50 +5,90 @@ module Halibut
|
|
5
5
|
|
6
6
|
class Resource
|
7
7
|
attr_reader :properties, :links
|
8
|
-
|
8
|
+
|
9
9
|
def initialize(href=nil)
|
10
10
|
@links = RelationMap.new
|
11
11
|
@resources = RelationMap.new
|
12
12
|
@properties = {}
|
13
|
-
|
13
|
+
|
14
14
|
add_link('self', href) if href
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def set_property property, value
|
18
18
|
@properties[property] = value
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def get_property property
|
22
22
|
@properties[property]
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def add_link(relation, href, templated=nil, opts={})
|
26
26
|
@links.add relation, Halibut::Link.new(href, templated)
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def embed_resource(relation, resource)
|
30
30
|
@resources.add relation, resource
|
31
31
|
end
|
32
|
-
|
33
|
-
|
32
|
+
|
33
|
+
|
34
34
|
def embedded
|
35
35
|
@resources
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
|
39
39
|
def as_json
|
40
40
|
json = {}
|
41
41
|
json = json.merge @properties
|
42
|
-
json['_links'] = {}.merge @links unless @links.empty?
|
42
|
+
json['_links'] = {}.merge @links unless @links.empty?
|
43
43
|
json['_resources'] = {}.merge @resources unless @resources.empty?
|
44
|
-
|
44
|
+
|
45
45
|
json
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
def to_json
|
49
49
|
JSON.dump as_json
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
|
+
def self.from_json resource
|
53
|
+
json = JSON.load(resource)
|
54
|
+
halibut = Halibut::Resource.new
|
55
|
+
|
56
|
+
links = json.delete '_links'
|
57
|
+
resources = json.delete '_embedded'
|
58
|
+
|
59
|
+
json.each_pair do |k,v|
|
60
|
+
halibut.set_property k, v
|
61
|
+
end
|
62
|
+
|
63
|
+
links.each do |relation,v|
|
64
|
+
link = [] << v
|
65
|
+
link = link.flatten
|
66
|
+
|
67
|
+
link.each do |attrs|
|
68
|
+
href = attrs.delete 'href'
|
69
|
+
templated = attrs.delete 'templated'
|
70
|
+
options = attrs
|
71
|
+
|
72
|
+
halibut.add_link relation, href, templated, options
|
73
|
+
end
|
74
|
+
end if links
|
75
|
+
|
76
|
+
resources.each do |relation,value|
|
77
|
+
res = [] << value
|
78
|
+
res = res.flatten
|
79
|
+
|
80
|
+
res.each do |resource|
|
81
|
+
halibut.embed_resource relation, Halibut::Resource.from_json(JSON.dump resource)
|
82
|
+
end
|
83
|
+
end if resources
|
84
|
+
|
85
|
+
halibut
|
86
|
+
end
|
87
|
+
|
88
|
+
def ==(other)
|
89
|
+
@properties == other.properties &&
|
90
|
+
@links == other.links &&
|
91
|
+
@resources == other.embedded
|
92
|
+
end
|
52
93
|
end
|
53
|
-
|
54
94
|
end
|
data/lib/halibut/version.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
{ "_links": {
|
2
|
+
"self": { "href": "/orders" },
|
3
|
+
"find": { "href": "/orders{?id}", "templated": true },
|
4
|
+
"next": [
|
5
|
+
{ "href": "/orders/1" },
|
6
|
+
{ "href": "/orders/9" }
|
7
|
+
]
|
8
|
+
},
|
9
|
+
"_embedded": {
|
10
|
+
"orders": [
|
11
|
+
{
|
12
|
+
"_links": {
|
13
|
+
"self": { "href": "/orders/123" }
|
14
|
+
},
|
15
|
+
"total": 30.00,
|
16
|
+
"currency": "USD",
|
17
|
+
"status": "shipped"
|
18
|
+
}
|
19
|
+
]
|
20
|
+
},
|
21
|
+
"currentlyProcessing": 14,
|
22
|
+
"shippedToday": 20
|
23
|
+
}
|
data/spec/link_spec.rb
CHANGED
data/spec/resource_spec.rb
CHANGED
@@ -65,4 +65,25 @@ describe Halibut::Resource do
|
|
65
65
|
JSON.load(subject).must_equal JSON.load(json)
|
66
66
|
end
|
67
67
|
end
|
68
|
+
|
69
|
+
describe "Deserialize" do
|
70
|
+
subject { Halibut::Resource.from_json(load_json "serialize") }
|
71
|
+
|
72
|
+
it "deserializes from JSON" do
|
73
|
+
order = Halibut::Resource.new "/orders/123"
|
74
|
+
order.set_property "total", 30.00
|
75
|
+
order.set_property "currency", "USD"
|
76
|
+
order.set_property "status", "shipped"
|
77
|
+
|
78
|
+
resource = Halibut::Resource.new "/orders"
|
79
|
+
resource.add_link "find", "/orders{?id}", true
|
80
|
+
resource.add_link "next", "/orders/1"
|
81
|
+
resource.add_link "next", "/orders/9"
|
82
|
+
resource.set_property "currentlyProcessing", 14
|
83
|
+
resource.set_property "shippedToday", 20
|
84
|
+
resource.embed_resource "orders", order
|
85
|
+
|
86
|
+
subject.must_equal resource
|
87
|
+
end
|
88
|
+
end
|
68
89
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: halibut
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: multi_json
|
@@ -212,6 +212,7 @@ extra_rdoc_files: []
|
|
212
212
|
files:
|
213
213
|
- .gitignore
|
214
214
|
- .rvmrc
|
215
|
+
- .travis.yml
|
215
216
|
- Gemfile
|
216
217
|
- Guardfile
|
217
218
|
- LICENSE
|
@@ -226,6 +227,7 @@ files:
|
|
226
227
|
- spec/fixtures/example.json
|
227
228
|
- spec/fixtures/example.xml
|
228
229
|
- spec/fixtures/minimal.json
|
230
|
+
- spec/fixtures/serialize.json
|
229
231
|
- spec/fixtures/simple.json
|
230
232
|
- spec/link_spec.rb
|
231
233
|
- spec/relation_map_spec.rb
|
@@ -259,6 +261,7 @@ test_files:
|
|
259
261
|
- spec/fixtures/example.json
|
260
262
|
- spec/fixtures/example.xml
|
261
263
|
- spec/fixtures/minimal.json
|
264
|
+
- spec/fixtures/serialize.json
|
262
265
|
- spec/fixtures/simple.json
|
263
266
|
- spec/link_spec.rb
|
264
267
|
- spec/relation_map_spec.rb
|