ruby-json-home 0.0.1 → 0.0.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_home.rb +64 -33
- data/lib/ruby-json-home.rb +2 -2
- 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: bba96e8360159bc95c214c30eabb3fc8110afc44
|
4
|
+
data.tar.gz: 0e06f41c3f1b27a969d7c54a353fbc937dfb9e11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67111dca97aac5329ddac374d83ed7338c3a8cd8f25f060352aa75e2d3ecf9a3ac38a2b0a3488bd6215837110801e4874807d854e1bfc1261602db618af051b8
|
7
|
+
data.tar.gz: 0ed557f7fdfaf5685089d1bd0035b7c48d1ec4e9f281f9b925abe58573208f5574a7e4582ee8585c220579bdecc7770a67b9a04b3b0ace8042597d1232b94e0f
|
data/lib/json_home.rb
CHANGED
@@ -19,85 +19,116 @@ class JSONHome
|
|
19
19
|
|
20
20
|
attr_reader :base_uri
|
21
21
|
|
22
|
+
DEFAULT_NAMESPACE = 'http://helloworld.innoq.com'
|
23
|
+
|
22
24
|
class << self
|
23
|
-
attr_accessor :logger, :ignore_http_errors
|
25
|
+
attr_accessor :logger, :ignore_http_errors, :default_namespace, :use_additional_params
|
24
26
|
end
|
27
|
+
self.default_namespace = DEFAULT_NAMESPACE
|
25
28
|
|
26
29
|
def initialize(base_uri, local_resources = nil)
|
27
|
-
@base_uri
|
30
|
+
@base_uri = URI.parse(base_uri)
|
28
31
|
@resources = {}
|
29
32
|
reload(local_resources)
|
30
33
|
end
|
31
34
|
|
32
35
|
def reload(local_resources = nil)
|
33
|
-
Net::HTTP.start(base_uri.host, base_uri.port) do |http|
|
36
|
+
Net::HTTP.start(@base_uri.host, @base_uri.port) do |http|
|
34
37
|
req = if local_resources.is_a?(Hash)
|
35
|
-
r = Net::HTTP::Put.new(base_uri.request_uri)
|
36
|
-
r.body = JSON.generate(
|
37
|
-
|
38
|
-
memo[k] = v
|
39
|
-
})
|
40
|
-
r["Content-Type"] = "application/json"
|
38
|
+
r = Net::HTTP::Put.new(@base_uri.request_uri)
|
39
|
+
r.body = JSON.generate('resources' => with_namespaces(local_resources))
|
40
|
+
r['Content-Type'] = 'application/json'
|
41
41
|
r
|
42
42
|
else
|
43
|
-
Net::HTTP::Get.new(base_uri.request_uri)
|
43
|
+
Net::HTTP::Get.new(@base_uri.request_uri)
|
44
44
|
end
|
45
|
-
req['Accept'] =
|
45
|
+
req['Accept'] = 'application/json-home'
|
46
46
|
res = http.request(req)
|
47
47
|
if res.is_a?(Net::HTTPSuccess)
|
48
|
-
@resources = JSON.parse(res.body)[
|
49
|
-
|
48
|
+
@resources = JSON.parse(res.body)['resources'] || {}
|
49
|
+
self.class.logger.info("Found ForeignLinks: #{@resources.keys.inspect}") if self.class.logger
|
50
50
|
else
|
51
51
|
raise RuntimeError.new("HTTP error: '#{res.code} #{res.message}'")
|
52
52
|
end
|
53
53
|
end
|
54
54
|
rescue Net::HTTPExceptions, Errno::ECONNREFUSED, RuntimeError => e
|
55
|
-
if
|
55
|
+
if self.class.ignore_http_errors
|
56
56
|
puts e
|
57
|
-
|
58
|
-
@resources =
|
57
|
+
self.class.logger.error(e) if self.class.logger
|
58
|
+
@resources = with_namespaces(local_resources)
|
59
59
|
else
|
60
60
|
raise e
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
64
|
def uri(resource, params = {})
|
65
|
-
|
65
|
+
self.class.uri(@resources, resource, params)
|
66
66
|
end
|
67
67
|
|
68
68
|
def uri?(resource)
|
69
|
-
|
69
|
+
self.class.uri?(@resources, resource)
|
70
70
|
end
|
71
71
|
|
72
72
|
def self.uri(resources, resource, params = {})
|
73
|
+
resource = with_namespace(resource)
|
73
74
|
unless uri?(resources, resource)
|
74
|
-
|
75
|
+
self.warn "Couldn't find valid JSON home data for '#{resource}'"
|
75
76
|
return nil
|
76
77
|
end
|
77
78
|
|
78
79
|
params = params.inject({}) { |h, (k, v)| h.merge(k.to_s => v) }
|
80
|
+
data = resources[resource.to_s]
|
79
81
|
|
80
|
-
data
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
JSONHome.logger && JSONHome.logger.warn("Missing key '#{key}' in ForeignLinks#uri call") unless params[key]
|
82
|
+
if data['href'].is_a?(String)
|
83
|
+
data['href']
|
84
|
+
elsif data['href-template'].is_a?(String)
|
85
|
+
uri = data['href-template']
|
86
|
+
unknown_keys = (params.keys - data['href-vars'].keys)
|
87
|
+
self.warn "Unknown keys #{unknown_keys.inspect} in ForeignLinks#uri call" if unknown_keys.any?
|
88
|
+
data['href-vars'].each do |key, type_uri|
|
89
|
+
self.warn("Missing key '#{key}' in ForeignLinks#uri call") unless params[key]
|
89
90
|
uri = uri.gsub("{#{key}}", params[key].to_s)
|
90
91
|
end
|
91
|
-
|
92
|
+
if self.use_additional_params and unknown_keys.any?
|
93
|
+
additional_params = URI.encode_www_form params.select{|k, v| unknown_keys.include? k }
|
94
|
+
if uri.include? '?'
|
95
|
+
uri + '&' + additional_params
|
96
|
+
else
|
97
|
+
uri + '?' + additional_params
|
98
|
+
end
|
99
|
+
else
|
100
|
+
uri
|
101
|
+
end
|
92
102
|
end
|
93
103
|
end
|
94
104
|
|
95
105
|
def self.uri?(resources, resource)
|
96
|
-
data = resources[resource
|
106
|
+
data = resources[self.with_namespace(resource)]
|
97
107
|
data.is_a?(Hash) && (
|
98
|
-
data[
|
99
|
-
(data[
|
108
|
+
data['href'].is_a?(String) ||
|
109
|
+
(data['href-template'].is_a?(String) && data['href-vars'].is_a?(Hash))
|
100
110
|
)
|
101
111
|
end
|
102
112
|
|
103
|
-
|
113
|
+
protected
|
114
|
+
|
115
|
+
def self.with_namespace(resource)
|
116
|
+
if resource.to_s.include?('/')
|
117
|
+
resource.to_s
|
118
|
+
else
|
119
|
+
[self.default_namespace, resource].join('/')
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# appends the default namespace to each resource given in :local_resources
|
124
|
+
def with_namespaces(local_resources)
|
125
|
+
local_resources.each_with_object({}) do |(k, v), memo|
|
126
|
+
memo[self.class.with_namespace(k)] = v
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.warn(msg)
|
131
|
+
self.logger.warn msg if self.logger
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
data/lib/ruby-json-home.rb
CHANGED
@@ -18,7 +18,7 @@ require 'json_home'
|
|
18
18
|
|
19
19
|
module RubyJSONHome
|
20
20
|
|
21
|
-
VERSION = '0.0.
|
21
|
+
VERSION = '0.0.2'
|
22
22
|
|
23
23
|
end
|
24
24
|
|
@@ -28,4 +28,4 @@ if Kernel.const_defined?(:Rails)
|
|
28
28
|
JSONHome.logger = Rails.logger
|
29
29
|
else
|
30
30
|
JSONHome.logger = Logger.new(STDOUT)
|
31
|
-
end
|
31
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-json-home
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Till Schulte-Coerne
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|