halibut 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -1
- data/.gitmodules +3 -0
- data/.travis.yml +4 -4
- data/Gemfile +2 -2
- data/Gemfile.travis +11 -0
- data/README.md +83 -34
- data/Rakefile +2 -2
- data/TODO +2 -0
- data/halibut.gemspec +16 -11
- data/lib/halibut.rb +15 -3
- data/lib/halibut/adapter/json.rb +72 -0
- data/lib/halibut/adapter/xml.rb +88 -0
- data/lib/halibut/builder.rb +75 -0
- data/lib/halibut/hal/link.rb +81 -0
- data/lib/halibut/hal/resource.rb +74 -0
- data/lib/halibut/link_relation.rb +29 -0
- data/lib/halibut/relation_map.rb +24 -23
- data/lib/halibut/version.rb +1 -2
- data/spec/adapter/json_spec.rb +72 -0
- data/spec/adapter/xml_spec.rb +50 -0
- data/spec/builder_spec.rb +185 -0
- data/spec/fixtures/serialize.json +1 -1
- data/spec/link_relation_spec.rb +43 -0
- data/spec/link_spec.rb +13 -11
- data/spec/relation_map_spec.rb +7 -6
- data/spec/resource_spec.rb +23 -53
- data/spec/spec_helper.rb +4 -2
- metadata +78 -15
- data/lib/halibut/link.rb +0 -56
- data/lib/halibut/resource.rb +0 -94
data/lib/halibut/link.rb
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
module Halibut
|
2
|
-
|
3
|
-
class Link
|
4
|
-
attr_reader :href, :templated, :type, :name,
|
5
|
-
:profile, :title, :hreflang
|
6
|
-
|
7
|
-
def initialize(href, templated=nil, opts={})
|
8
|
-
@href = href
|
9
|
-
@templated = templated
|
10
|
-
|
11
|
-
set_options opts
|
12
|
-
end
|
13
|
-
|
14
|
-
def templated?
|
15
|
-
@templated || false
|
16
|
-
end
|
17
|
-
|
18
|
-
def to_hash
|
19
|
-
# hash = { 'href' => @href }
|
20
|
-
# hash['templated'] = @templated unless @templated.nil?
|
21
|
-
a = instance_variables.each_with_object({}) do |name, output|
|
22
|
-
next if (ivar = instance_variable_get(name)).nil?
|
23
|
-
|
24
|
-
output[name[1..-1]] = ivar
|
25
|
-
end
|
26
|
-
|
27
|
-
|
28
|
-
a
|
29
|
-
end
|
30
|
-
|
31
|
-
def to_s
|
32
|
-
@href.to_s
|
33
|
-
end
|
34
|
-
|
35
|
-
|
36
|
-
private
|
37
|
-
def set_options(opts)
|
38
|
-
@type = opts[:type]
|
39
|
-
@name = opts[:name]
|
40
|
-
@profile = opts[:profile]
|
41
|
-
@title = opts[:title]
|
42
|
-
@hreflang = opts[:hreflang]
|
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
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
data/lib/halibut/resource.rb
DELETED
@@ -1,94 +0,0 @@
|
|
1
|
-
require 'json'
|
2
|
-
require 'halibut/relation_map'
|
3
|
-
|
4
|
-
module Halibut
|
5
|
-
|
6
|
-
class Resource
|
7
|
-
attr_reader :properties, :links
|
8
|
-
|
9
|
-
def initialize(href=nil)
|
10
|
-
@links = RelationMap.new
|
11
|
-
@resources = RelationMap.new
|
12
|
-
@properties = {}
|
13
|
-
|
14
|
-
add_link('self', href) if href
|
15
|
-
end
|
16
|
-
|
17
|
-
def set_property property, value
|
18
|
-
@properties[property] = value
|
19
|
-
end
|
20
|
-
|
21
|
-
def get_property property
|
22
|
-
@properties[property]
|
23
|
-
end
|
24
|
-
|
25
|
-
def add_link(relation, href, templated=nil, opts={})
|
26
|
-
@links.add relation, Halibut::Link.new(href, templated)
|
27
|
-
end
|
28
|
-
|
29
|
-
def embed_resource(relation, resource)
|
30
|
-
@resources.add relation, resource
|
31
|
-
end
|
32
|
-
|
33
|
-
|
34
|
-
def embedded
|
35
|
-
@resources
|
36
|
-
end
|
37
|
-
|
38
|
-
|
39
|
-
def as_json
|
40
|
-
json = {}
|
41
|
-
json = json.merge @properties
|
42
|
-
json['_links'] = {}.merge @links unless @links.empty?
|
43
|
-
json['_resources'] = {}.merge @resources unless @resources.empty?
|
44
|
-
|
45
|
-
json
|
46
|
-
end
|
47
|
-
|
48
|
-
def to_json
|
49
|
-
JSON.dump as_json
|
50
|
-
end
|
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
|
93
|
-
end
|
94
|
-
end
|