halibut 0.1.0 → 0.2.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.
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
@@ -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