frenetic 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
- # Frenetic [![Build Status][travis_status]][travis]
1
+ # Frenetic [![Gem Version][version_badge]][version] [![Build Status][travis_status]][travis]
2
2
 
3
+ [version_badge]: https://badge.fury.io/rb/frenetic.png
4
+ [version]: http://badge.fury.io/rb/frenetic
3
5
  [travis_status]: https://secure.travis-ci.org/dlindahl/frenetic.png
4
6
  [travis]: http://travis-ci.org/dlindahl/frenetic
5
7
 
@@ -182,7 +184,7 @@ supports can be added to the Hash.
182
184
 
183
185
 
184
186
 
185
- # Middlware
187
+ # Middleware
186
188
 
187
189
  Frenetic supports anything that Faraday does. You may specify additional
188
190
  middleware with the `use` method:
@@ -286,4 +288,4 @@ make your code a bit more readable:
286
288
  [spire.io]: http://api.spire.io/
287
289
  [roar]: https://github.com/apotonick/roar
288
290
  [faraday]: https://github.com/technoweenie/faraday
289
- [rack_cache]: https://github.com/rtomayko/rack-cache
291
+ [rack_cache]: https://github.com/rtomayko/rack-cache
@@ -1,23 +1,22 @@
1
+ require 'active_support/inflector'
2
+
1
3
  class Frenetic
2
4
  class Resource
3
5
 
4
- attr_reader :links
5
-
6
6
  def initialize( attributes = {} )
7
- if attributes.is_a? Hash
8
- load attributes.keys, attributes
7
+ self.class.apply_schema
9
8
 
10
- @links = {}
11
- else
12
- load self.class.schema, attributes
9
+ attributes.each do |key, val|
10
+ instance_variable_set "@#{key}", val
11
+ end
13
12
 
14
- # Load embedded resources if present
15
- if attributes.resources
16
- load attributes.resources.members, attributes.resources
17
- end
13
+ @_links ||= {}
18
14
 
19
- @links = attributes.links
20
- end
15
+ build_associations attributes
16
+ end
17
+
18
+ def links
19
+ @_links
21
20
  end
22
21
 
23
22
  # Attempts to retrieve the Resource Schema from the API based on the name
@@ -33,37 +32,40 @@ class Frenetic
33
32
 
34
33
  def schema
35
34
  if self.respond_to? :api
36
- class_name = self.to_s.split('::').last.downcase
35
+ class_name = self.to_s.demodulize.underscore
37
36
 
38
- api.description.resources.schema.send(class_name).properties
37
+ if class_schema = api.description.resources.schema.send(class_name)
38
+ class_schema.properties
39
+ else
40
+ {}
41
+ end
39
42
  else
40
43
  raise MissingAPIReference,
41
- "This Resource needs a class accessor defined as " +
42
- "`.api` that references an instance of Frenetic."
44
+ "This Resource needs a class accessor defined as " \
45
+ "`.api` that references an instance of Frenetic."
43
46
  end
44
47
  end
45
48
 
46
- private
49
+ def apply_schema
50
+ schema.keys.each do |key|
51
+ next if key[0] == '_'
47
52
 
48
- def metaclass
49
- metaclass = class << self; self; end
53
+ class_eval { attr_reader key.to_sym } unless instance_methods.include? key
54
+ class_eval { attr_writer key.to_sym } unless instance_methods.include? "#{key}="
55
+ end
50
56
  end
51
- end
52
-
53
- private
54
57
 
55
- def metaclass
56
- metaclass = class << self; self; end
58
+ def metaclass
59
+ class << self; self; end
60
+ end
57
61
  end
58
62
 
59
- def load( keys, attributes )
60
- keys.each do |key|
61
- instance_variable_set "@#{key}", attributes[key.to_s]
63
+ def build_associations( attributes )
64
+ return unless attributes['_embedded']
62
65
 
63
- metaclass.class_eval do
64
- define_method key do
65
- instance_variable_get( "@#{key}")
66
- end
66
+ attributes['_embedded'].keys.each do |key|
67
+ self.class.class_eval do
68
+ attr_reader key.to_sym
67
69
  end
68
70
  end
69
71
  end
@@ -1,3 +1,3 @@
1
1
  class Frenetic
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
@@ -2,22 +2,43 @@ describe Frenetic::Resource do
2
2
 
3
3
  let(:client) { Frenetic.new(url:'http://example.org') }
4
4
 
5
- let(:resource) { described_class.new }
5
+ let(:resource) { MyResource.new }
6
+
7
+ let(:other_resource) { MyOtherResource.new }
6
8
 
7
9
  let(:description_stub) do
8
- Frenetic::HalJson::ResponseWrapper.new('resources' => { 'schema' => { 'resource' =>
9
- { 'properties' => { 'foo' => 2 } }
10
+ Frenetic::HalJson::ResponseWrapper.new('resources' => {
11
+ 'schema' => {
12
+ 'my_resource' => { 'properties' => { 'foo' => 'string' } },
13
+ 'my_other_resource' => { 'properties' => {} }
10
14
  } } )
11
15
  end
12
16
 
17
+ before do
18
+ client.stub(:description).and_return description_stub
19
+
20
+ described_class.stub(:api).and_return client
21
+
22
+ stub_const 'MyResource', Class.new(described_class)
23
+ stub_const 'MyOtherResource', Class.new(described_class)
24
+ end
25
+
13
26
  subject { resource }
14
27
 
15
28
  context "created from a Hash" do
16
- let(:resource) { described_class.new( foo: 'bar' ) }
29
+ let(:resource) { MyResource.new( foo: 'bar' ) }
17
30
 
18
31
  it { should respond_to(:foo) }
32
+ it { should respond_to(:foo=) }
19
33
  its(:links) { should be_a Hash }
20
34
  its(:links) { should be_empty }
35
+
36
+ context 'other subclasses' do
37
+ subject { other_resource }
38
+
39
+ it { should_not respond_to(:foo) }
40
+ it { should_not respond_to(:foo=) }
41
+ end
21
42
  end
22
43
 
23
44
  context "created from a HAL-JSON response" do
@@ -30,17 +51,16 @@ describe Frenetic::Resource do
30
51
  'bar' => 2
31
52
  }
32
53
  end
54
+
33
55
  let(:wrapped_response) do
34
56
  Frenetic::HalJson::ResponseWrapper.new(api_response)
35
57
  end
36
- let(:resource_a) { described_class.new( wrapped_response ) }
37
- let(:resource_b) { described_class.new }
38
58
 
39
- before do
40
- client.stub(:description).and_return description_stub
59
+ let(:resource_a) { MyResource.new( wrapped_response ) }
41
60
 
42
- described_class.stub(:api).and_return client
61
+ let(:resource_b) { MyResource.new }
43
62
 
63
+ before do
44
64
  resource_a && resource_b
45
65
  end
46
66
 
@@ -55,7 +75,7 @@ describe Frenetic::Resource do
55
75
  context "intiailized in sequence without data" do
56
76
  subject { resource_b }
57
77
 
58
- it { should_not respond_to(:foo) }
78
+ it { should respond_to(:foo) }
59
79
  it { should_not respond_to(:bar) }
60
80
  its(:links) { should be_empty }
61
81
  end
@@ -80,19 +100,29 @@ describe Frenetic::Resource do
80
100
  end
81
101
 
82
102
  describe ".api_client" do
103
+ before do
104
+ described_class.unstub! :api
105
+ end
106
+
107
+ subject { MyResource }
108
+
83
109
  context "with a block" do
84
- before { resource.class.api_client { client } }
110
+ before { subject.api_client { client } }
85
111
 
86
112
  it "should reference the defined API client" do
87
- subject.class.api.should == client
113
+ subject.api.should eq client
114
+
115
+ MyOtherResource.should_not respond_to :api
88
116
  end
89
117
  end
90
118
 
91
119
  context "with an argument" do
92
- before { resource.class.api_client client }
120
+ before { MyResource.api_client client }
93
121
 
94
122
  it "should reference the defined API client" do
95
- subject.class.api.should == client
123
+ subject.api.should eq client
124
+
125
+ MyOtherResource.should_not respond_to :api
96
126
  end
97
127
  end
98
128
  end
@@ -108,7 +138,7 @@ describe Frenetic::Resource do
108
138
  end
109
139
 
110
140
  it "should return the schema for the specific resource" do
111
- subject.should == description_stub.resources.schema.resource.properties
141
+ subject.should == description_stub.resources.schema.my_resource.properties
112
142
  end
113
143
  end
114
144
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frenetic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-16 00:00:00.000000000 Z
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday