api_bee 0.1.4 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.mkd CHANGED
@@ -9,33 +9,36 @@ These APIs must
9
9
 
10
10
  A single resource might look like:
11
11
 
12
+ ```javascript
13
+ {
14
+ 'name': 'Foo',
15
+ 'description': 'foo resoruce',
16
+ 'href': 'http://api.myservice.com/resources/foo'
17
+ }
18
+ ```
19
+ A resource collection looks like:
20
+
21
+ ```javascript
22
+ {
23
+ 'href': 'http://api.myservice.com/resources',
24
+ 'total_entries': 100,
25
+ 'page': 1,
26
+ 'per_page': 10,
27
+ 'entries': [
12
28
  {
13
29
  'name': 'Foo',
14
30
  'description': 'foo resoruce',
15
- 'href': 'http://api.myservice.com/resources/foo'
16
- }
17
-
18
- A resource collection looks like:
19
-
31
+ 'href': 'http://api.myservice.com/resources/foo'
32
+ },
20
33
  {
21
- 'href': 'http://api.myservice.com/resources',
22
- 'total_entries': 100,
23
- 'page': 1,
24
- 'per_page': 10,
25
- 'entries': [
26
- {
27
- 'name': 'Foo',
28
- 'description': 'foo resoruce',
29
- 'href': 'http://api.myservice.com/resources/foo'
30
- },
31
- {
32
- 'name': 'Bar',
33
- 'description': 'bar resoruce',
34
- 'href': 'http://api.myservice.com/resources/bar'
35
- },
36
- ...
37
- ]
38
- }
34
+ 'name': 'Bar',
35
+ 'description': 'bar resoruce',
36
+ 'href': 'http://api.myservice.com/resources/bar'
37
+ },
38
+ ...
39
+ ]
40
+ }
41
+ ```
39
42
 
40
43
  Collection resources must include the fields 'href', 'total_entries', 'page', 'per_page' and 'entries'. This allows clients to paginate and fetch more pages.
41
44
 
@@ -71,14 +74,16 @@ end
71
74
 
72
75
  If an object in a response has a 'href' attribute, it will be used to fetch more data if you ask for an attribute currently not in the object.
73
76
 
74
- # JSON Dataset
75
- {
76
- 'user': {
77
- 'name': 'Ismael',
78
- 'href': 'http://api.com/users/ismael'
79
- }
80
- }
81
-
77
+ ```javascript
78
+ /* JSON Dataset */
79
+ {
80
+ 'user': {
81
+ 'name': 'Ismael',
82
+ 'href': 'http://api.com/users/ismael'
83
+ }
84
+ }
85
+ ```
86
+
82
87
  ```ruby
83
88
  # Instantiate object
84
89
  user = api.get('/user')
@@ -90,26 +95,27 @@ user[:last_name]
90
95
 
91
96
  This works for objects in collections too.
92
97
 
93
- # JSON collection
94
- {
95
- 'users': {
96
- 'total_entries': 100,
97
- 'page': 1,
98
- 'per_page': 2,
99
- 'href': 'http://api.com/users',
100
- 'entries': [
101
- {
102
- 'name': 'Ismael',
103
- 'href': 'http://api.com/users/ismael'
104
- },
105
- {
106
- 'name': 'John',
107
- 'href': 'http://api.com/users/john'
108
- }
109
- ]
98
+ ```javascript
99
+ /* JSON collection */
100
+ {
101
+ 'users': {
102
+ 'total_entries': 100,
103
+ 'page': 1,
104
+ 'per_page': 2,
105
+ 'href': 'http://api.com/users',
106
+ 'entries': [
107
+ {
108
+ 'name': 'Ismael',
109
+ 'href': 'http://api.com/users/ismael'
110
+ },
111
+ {
112
+ 'name': 'John',
113
+ 'href': 'http://api.com/users/john'
110
114
  }
111
- }
112
-
115
+ ]
116
+ }
117
+ }
118
+ ```
113
119
 
114
120
  ```ruby
115
121
  # Instantiate collection
@@ -197,27 +203,42 @@ end
197
203
 
198
204
  ApiBee ships with an in-memory Hash adapter so it can be use with test/local data (for example a YAML file).
199
205
 
200
-
201
206
  ```ruby
202
- api = ApiBee.setup(:hash, YAML.load_file('./my_data.yml'))
203
-
207
+ api = ApiBee.setup(:hash, YAML.load_file('./my_data.yml'))
208
+
204
209
  products = api.get('/products') # => ApiBee::Node::List
205
-
206
- products.first # => ApiBee::Node::Single
207
-
210
+
211
+ products.first # => ApiBee::Node::Single
208
212
  products.each() # iterate current page
209
-
210
213
  products.current_page # => 1
211
-
212
214
  products.paginate(:page => 2, :per_page => 4) # => ApiBee::Node::List # Next page
213
-
214
215
  products.has_next_page? # => false
215
-
216
216
  products.has_prev_page? # => true
217
-
218
217
  products.prev_page # => 1
219
218
  ```
220
219
 
221
220
  ## Examples
222
221
 
223
222
  See [examples/github_api.rb](https://github.com/ismasan/ApiBee/blob/master/examples/github_api.rb) for an adapter that paginates Github's API by decorating it's results with ApiBee's required pagination properties
223
+
224
+ # LICENSE
225
+
226
+ Copyright (C) 2011 Ismael Celis
227
+
228
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
229
+ this software and associated documentation files (the "Software"), to deal in
230
+ the Software without restriction, including without limitation the rights to
231
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
232
+ of the Software, and to permit persons to whom the Software is furnished to do
233
+ so, subject to the following conditions:
234
+
235
+ The above copyright notice and this permission notice shall be included in all
236
+ copies or substantial portions of the Software.
237
+
238
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
239
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
240
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
241
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
242
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
243
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
244
+ SOFTWARE.
@@ -6,8 +6,11 @@ module ApiBee
6
6
 
7
7
  attr_reader :data
8
8
 
9
- def initialize(*args)
10
- @data = args.last
9
+ def initialize(data, args = {})
10
+ @data = data
11
+ @config = {
12
+ :uid_field => :id
13
+ }.merge(args)
11
14
  end
12
15
 
13
16
  def get(href, opts = {})
@@ -49,7 +52,7 @@ module ApiBee
49
52
 
50
53
  def handle_array_data(array, key)
51
54
  if array[0].kind_of?(::Hash)
52
- array.find {|e| e[:id] == key}
55
+ array.find {|e| e[@config[:uid_field]].to_s == key}
53
56
  else
54
57
  array
55
58
  end
data/lib/api_bee/node.rb CHANGED
@@ -81,6 +81,10 @@ module ApiBee
81
81
  end
82
82
  end
83
83
 
84
+ def []=(key, value)
85
+ @attributes[key.to_sym] = value
86
+ end
87
+
84
88
  protected
85
89
 
86
90
  def update_attributes(attrs)
@@ -1,3 +1,3 @@
1
1
  module ApiBee
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -56,6 +56,19 @@ describe ApiBee::Adapters::Hash do
56
56
  end
57
57
  end
58
58
 
59
+ context 'custom item identifier' do
60
+ before do
61
+ @data[:collections].first[:uid] = 'xxx123'
62
+ @uid_adapter = ApiBee::Adapters::Hash.new(@data, :uid_field => :uid)
63
+ end
64
+
65
+ it 'should find by custom field name' do
66
+ collection = @uid_adapter.get('/collections/xxx123')
67
+ collection[:title].should == 'Catalog'
68
+ collection[:id].should == 'catalog'
69
+ end
70
+ end
71
+
59
72
  context 'accessing single nodes' do
60
73
 
61
74
  before do
data/spec/node_spec.rb CHANGED
@@ -117,6 +117,20 @@ describe ApiBee do
117
117
  end
118
118
  end
119
119
 
120
+ describe '#[]=' do
121
+
122
+ before do
123
+ @adapter = mock('Adapter')
124
+ @config = ApiBee.new_config
125
+ end
126
+
127
+ it 'should set attributes in nodes' do
128
+ node = ApiBee::Node.resolve(@adapter, @config, {:title => 'Blah', :total_entries => 4, :href => '/products'})
129
+ node[:foo] = 11
130
+ node[:foo].should == 11
131
+ end
132
+ end
133
+
120
134
  context 'lazy loading' do
121
135
  before do
122
136
  require 'api_bee/adapters/hash'
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: api_bee
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.4
5
+ version: 0.1.7
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ismael Celis
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-01-10 00:00:00 Z
13
+ date: 2012-01-18 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: Small Ruby client for discoverable, lazily-loaded, paginated JSON APIs