patchboard 0.4.2 → 0.4.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b8396b43a867c8dd3f0d7ad9554cc452af65098
4
- data.tar.gz: fdb8ed9700cc348cdc4f551c9ca32563a44bffea
3
+ metadata.gz: 46fe7f3a6e42bd2248df577e00d2bf4ebff0de19
4
+ data.tar.gz: e5cbc55c390be6754d739326283058c2d120f71c
5
5
  SHA512:
6
- metadata.gz: 91da69da6c145512a3a3b9af1687f54ce49b3f86d0e0bfb28be52b68599487a365b0cee23a44cb66be0f5d30b223a59d0940a3660c924e9e699cac30701a9b35
7
- data.tar.gz: d1bb9654e039f2636737cd33c7ae25fd657ec1c0cb5229c2d7ed3653cf97abfbe256e7e07692df06072f5d252d51e854e45d856b8eae06a01f02f9a1d0b416af
6
+ metadata.gz: 8d5bd558a0d07e437dab031f50f291eb82ae334492f8c4bf2284b859b66077f7938224bfa661ea82c926b94880fc3ca060c9fad9ee5bf1e58f2131dd72c821af
7
+ data.tar.gz: 5096c602c6aee31da7d65dffb3efe1fec7d1539efdd47854dd16c833118c3e23ff3f57b1dc442bb4d46f1df7cf8071cbc3b81ce01ef2236f4b92cc986e7666b7
@@ -43,8 +43,7 @@ class Patchboard
43
43
  raw = self.http.request @method, url, options.merge(:response => :object)
44
44
  response = Response.new(raw)
45
45
  if response.status != @status
46
- # TODO: custom exception classes, express response body in such.
47
- raise "Unexpected response status: #{response.status}"
46
+ raise ResponseError.new(response.status, response.body), "Unexpected response status: #{response.status} - #{response.body}"
48
47
  end
49
48
  out = @api.decorate(resource.context, @response_schema, response.data)
50
49
  out.response = response
@@ -100,7 +99,15 @@ class Patchboard
100
99
  end
101
100
 
102
101
 
102
+ class ResponseError < StandardError
103
+ attr_reader :status
104
+ attr_reader :body
103
105
 
106
+ def initialize(status, body)
107
+ @status = status
108
+ @body = body
109
+ end
110
+ end
104
111
 
105
112
  end
106
113
 
@@ -47,38 +47,37 @@ class Patchboard
47
47
  else
48
48
  # Otherwise traverse the schema in search of subschemas that have
49
49
  # resource classes available.
50
- case schema[:type]
51
- when "array"
50
+
51
+ if schema[:items]
52
52
  # TODO: handle the case where schema.items is an array, which
53
53
  # signifies a tuple. schema.additionalItems then becomes important.
54
54
  array = data.map! do |item|
55
55
  self.decorate(context, schema[:items], item)
56
56
  end
57
57
  data = ArrayResource.new(array)
58
- when "object"
59
- if schema[:properties]
60
- schema[:properties].each do |key, prop_schema|
61
- if value = data[key]
62
- data[key] = self.decorate(context, prop_schema, value)
63
- end
58
+ end
59
+
60
+ if schema[:properties]
61
+ schema[:properties].each do |key, prop_schema|
62
+ if value = data[key]
63
+ data[key] = self.decorate(context, prop_schema, value)
64
64
  end
65
65
  end
66
- # TODO: handle schema.patternProperties
67
- # TODO: consider alternative to iterating over all keys.
68
- if schema[:additionalProperties]
69
- data.each do |key, value|
70
- next if schema[:properties] && schema[:properties][key]
71
- data[key] = self.decorate(context, schema[:additionalProperties], value)
72
- end
66
+ end
67
+
68
+ if schema[:additionalProperties]
69
+ data.each do |key, value|
70
+ next if schema[:properties] && schema[:properties][key]
71
+ data[key] = self.decorate(context, schema[:additionalProperties], value)
73
72
  end
73
+ end
74
+
75
+ if data.is_a? Hash
74
76
  data = Hashie::Mash.new data
75
- else
76
- if data.is_a? Hash
77
- data = Hashie::Mash.new data
78
- end
79
77
  end
78
+ data
80
79
  end
81
- data
80
+
82
81
  end
83
82
 
84
83
  end
@@ -17,20 +17,45 @@ class Patchboard
17
17
  schema
18
18
  end
19
19
 
20
+ # FIXME: break this out into multiple methods.
20
21
  if schema && schema[:properties]
21
22
  schema[:properties].each do |name, definition|
22
- define_method name do
23
- @attributes[name]
23
+
24
+ if property_mapping = self.api.find_mapping(definition)
25
+ if property_mapping.query
26
+ define_method name do |params={}|
27
+ params[:url] = @attributes[name][:url]
28
+ url = property_mapping.generate_url(params)
29
+ property_mapping.klass.new self.context, :url => url
30
+ end
31
+ else
32
+ define_method name do
33
+ property_mapping.klass.new self.context, @attributes[name]
34
+ end
35
+ end
36
+ else
37
+ define_method name do
38
+ @attributes[name]
39
+ end
24
40
  end
41
+
25
42
  end
26
43
  end
27
44
 
45
+
46
+ # When an additionalProperties schema is defined, the resource can
47
+ # contain top-level attributes that should obey that schema.
28
48
  if schema && schema[:additionalProperties] != false
29
- define_method :method_missing do |name, *args, &block|
30
- if args.size == 0
49
+
50
+ if (add_mapping = self.api.find_mapping(schema)) && (add_mapping.query)
51
+ define_method :method_missing do |name, params|
52
+ params[:url] = @attributes[name][:url]
53
+ url = add_mapping.generate_url(params)
54
+ add_mapping.klass.new self.context, :url => url
55
+ end
56
+ else
57
+ define_method :method_missing do |name|
31
58
  @attributes[name.to_sym]
32
- else
33
- super(name, *args, &block)
34
59
  end
35
60
  end
36
61
  end
@@ -53,26 +78,10 @@ class Patchboard
53
78
  # TODO: add some sort of validation for the input attributes.
54
79
  # Hey, we have a JSON Schema, why not use it?
55
80
  if self.schema && (properties = self.schema[:properties])
56
- context = instance.context
57
81
  properties.each do |key, sub_schema|
58
- next unless (value = attributes[key])
59
-
60
- if mapping = self.api.find_mapping(sub_schema)
61
- if mapping.query
62
- # TODO: find a way to define this at runtime, not once
63
- # for every instance.
64
- instance.define_singleton_method key do |params={}|
65
- params[:url] = value[:url]
66
- url = mapping.generate_url(params)
67
- mapping.klass.new context, :url => url
68
- end
69
- else
70
- attributes[key] = mapping.klass.new context, value
71
- end
72
- else
73
- attributes[key] = self.api.decorate(context, sub_schema, value)
82
+ if (value = attributes[key]) && !self.api.find_mapping(sub_schema)
83
+ attributes[key] = self.api.decorate(instance.context, sub_schema, value)
74
84
  end
75
-
76
85
  end
77
86
  end
78
87
  attributes
@@ -91,7 +100,7 @@ class Patchboard
91
100
  id = "%x" % (self.object_id << 1)
92
101
  %Q{
93
102
  #<#{self.class}:0x#{id}
94
- @url="#{@url}" @context=#{@context}>
103
+ @url="#{@url}">
95
104
  }.strip
96
105
  end
97
106
 
@@ -1,4 +1,3 @@
1
- require "pp"
2
1
  class Patchboard
3
2
 
4
3
  class SchemaManager
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patchboard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew King
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-17 00:00:00.000000000 Z
11
+ date: 2014-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json