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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b704a0ba185c79406a60d9a7147bea2c1b36fb8
4
- data.tar.gz: 4b7fe113fb946fa877ab4bf388b0f9541f0faa0c
3
+ metadata.gz: bba96e8360159bc95c214c30eabb3fc8110afc44
4
+ data.tar.gz: 0e06f41c3f1b27a969d7c54a353fbc937dfb9e11
5
5
  SHA512:
6
- metadata.gz: e01d8abe49854076bda23d795766d22a25d6f98fd7438a461b25b47d5559a50869211e618578478639a1ff8c0d977d099e9c9b4785554ecceb7b80bfdbeeb6a7
7
- data.tar.gz: 2a4bb75b3fca97dc78049d696531354e2b4fe9e5ff8e2ed3d95af51c8d4a8e65deefaff6bf74ecf396999f62247fb161b2a729c913ec76864823c107fb264dd5
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 = URI.parse(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("resources" => local_resources.each_with_object({}) { |(k, v), memo|
37
- k = "http://helloworld.innoq.com/#{k}" unless k.to_s.include?("/")
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'] = "application/json-home"
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)["resources"] || {}
49
- JSONHome.logger && JSONHome.logger.info("Found ForeignLinks: #{@resources.keys.inspect}")
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 JSONHome.ignore_http_errors
55
+ if self.class.ignore_http_errors
56
56
  puts e
57
- JSONHome.logger && JSONHome.logger.error(e)
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
- JSONHome.uri(@resources, resource, params)
65
+ self.class.uri(@resources, resource, params)
66
66
  end
67
67
 
68
68
  def uri?(resource)
69
- JSONHome.uri?(@resources, resource)
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
- JSONHome.logger && JSONHome.logger.warn("Couldn't find valid JSON home data for '#{resource}'")
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 = resources[resource.to_s]
81
- if data["href"].is_a?(String)
82
- data["href"].to_s
83
- elsif data["href-template"].is_a?(String)
84
- uri = data["href-template"].to_s
85
- unknown_keys = (params.keys - data["href-vars"].keys)
86
- JSONHome.logger && JSONHome.logger.warn("Unknown keys #{unknown_keys.inspect} in ForeignLinks#uri call") if unknown_keys.any?
87
- data["href-vars"].each do |key, type_uri|
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
- uri
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.to_s]
106
+ data = resources[self.with_namespace(resource)]
97
107
  data.is_a?(Hash) && (
98
- data["href"].is_a?(String) ||
99
- (data["href-template"].is_a?(String) && data["href-vars"].is_a?(Hash))
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
- end
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
@@ -18,7 +18,7 @@ require 'json_home'
18
18
 
19
19
  module RubyJSONHome
20
20
 
21
- VERSION = '0.0.1'
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.1
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-07-10 00:00:00.000000000 Z
11
+ date: 2014-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler