usergrid_iron 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +58 -27
- data/lib/usergrid/core/application.rb +12 -0
- data/lib/usergrid/core/collection.rb +14 -2
- data/lib/usergrid/version.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- data/spec/usergrid/core/application_spec.rb +33 -33
- data/spec/usergrid/core/collection_spec.rb +21 -6
- metadata +4 -4
data/README.md
CHANGED
@@ -5,27 +5,30 @@ REST API with minimal dependencies.
|
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
8
|
-
|
8
|
+
### Project
|
9
|
+
Add the gem to your project's Gemfile:
|
9
10
|
|
10
11
|
gem 'usergrid_iron'
|
11
12
|
|
12
|
-
|
13
|
+
Then rebuild your bundle:
|
13
14
|
|
14
15
|
$ bundle
|
15
16
|
|
16
|
-
|
17
|
+
### Stand-alone script
|
18
|
+
Just manually install the gem:
|
17
19
|
|
18
20
|
$ gem install usergrid_iron
|
19
21
|
|
20
22
|
|
21
23
|
## Usage
|
22
24
|
|
23
|
-
###
|
25
|
+
### Prerequisite
|
26
|
+
You'll want to be at least a little bit familiar with Usergrid / Apigee's App Services before you jump in here - but it easy and it's great! Start here:
|
24
27
|
|
25
|
-
|
28
|
+
App Services docs: <http://apigee.com/docs/usergrid/>
|
29
|
+
Open source stack: <https://github.com/apigee/usergrid-stack>
|
26
30
|
|
27
|
-
|
28
|
-
Open source: <https://github.com/apigee/usergrid-stack>
|
31
|
+
Awesome. Let's go!
|
29
32
|
|
30
33
|
### Getting started with the Usergrid_iron SDK is simple!
|
31
34
|
|
@@ -43,24 +46,40 @@ application = ''
|
|
43
46
|
username = ''
|
44
47
|
password = ''
|
45
48
|
|
49
|
+
# create the base application resource
|
50
|
+
# (this is a RestClient.resource)
|
46
51
|
application = Usergrid::Application.new "#{usergrid_api}/#{organization}/#{application}"
|
52
|
+
|
53
|
+
# login (note: not required for sandbox)
|
47
54
|
application.login username, password
|
48
55
|
|
49
|
-
# create and store a dog
|
56
|
+
# create and store a new dog on the server
|
57
|
+
# (the "response" is an enhanced RestClient.response)
|
50
58
|
response = application.create_dog breed: 'Black Mouth Cur', name: 'Old Yeller'
|
51
59
|
|
52
|
-
#
|
60
|
+
# the response has returned the entity data
|
61
|
+
# response.entity wraps it in an easy-to-use object
|
53
62
|
dog = response.entity
|
63
|
+
|
64
|
+
# it's persistent now, so it has a unique id
|
54
65
|
uuid = dog.uuid
|
55
66
|
|
56
|
-
#
|
57
|
-
|
67
|
+
# we can retrieve the dog by UUID using Hash syntax and calling get()
|
68
|
+
# (all dogs are stored in the "dogs" collection)
|
69
|
+
response = application["dogs"][uuid].get
|
70
|
+
same_dog = response.entity
|
71
|
+
|
72
|
+
# we could also retrieve the dog by name
|
73
|
+
# we could also use path ('/') syntax instead of nested Hash
|
74
|
+
# and we can even skip get() - entity() will do it for us
|
75
|
+
same_dog = application["dogs/#{dog.name}"].entity
|
58
76
|
|
59
77
|
# is it our dog? well, he knows his name!
|
60
|
-
|
78
|
+
# (and we can retrieve its values by dot or Hash)
|
79
|
+
puts "My dog's name is: #{same_dog.name} and his breed is #{same_dog['breed']}"
|
61
80
|
```
|
62
81
|
|
63
|
-
Well that was really easy.
|
82
|
+
Well that was really easy. More comments than code! :)
|
64
83
|
|
65
84
|
#### Let's try something slightly more complex.
|
66
85
|
Let's say you've registered for an organization, but you don't have an application yet
|
@@ -78,7 +97,7 @@ you shouldn't need to do anything!)
|
|
78
97
|
password = 'test'
|
79
98
|
app_name = 'dog_sitter'
|
80
99
|
|
81
|
-
## first, let's get that setup out of the way ##
|
100
|
+
## first, let's get that setup out of the way... ##
|
82
101
|
|
83
102
|
# get a management context & login the superuser
|
84
103
|
management = Usergrid::Management.new usergrid_api
|
@@ -91,37 +110,44 @@ you shouldn't need to do anything!)
|
|
91
110
|
# create an user for our application
|
92
111
|
new_application.create_user username: 'username', password: 'password'
|
93
112
|
|
94
|
-
|
95
|
-
## now we can play with the puppies! ##
|
96
|
-
|
97
113
|
# login to our new application as our new user
|
98
|
-
application =
|
114
|
+
application = organization.application app_name
|
99
115
|
application.login 'username', 'password'
|
100
116
|
|
117
|
+
|
118
|
+
## now we can play with the puppies! ##
|
119
|
+
|
101
120
|
# we can start with our dog again
|
102
121
|
application.create_dog breed: 'Black Mouth Cur', name: 'Old Yeller'
|
103
122
|
|
104
|
-
# but this time let's create several more dogs at once
|
123
|
+
# but this time let's create several more dogs all at once
|
105
124
|
application.create_dogs [
|
106
125
|
{ breed: 'Catalan sheepdog', name: 'Einstein' },
|
107
126
|
{ breed: 'Cocker Spaniel', name: 'Lady' },
|
108
127
|
{ breed: 'Mixed', name: 'Benji' }]
|
109
128
|
|
110
129
|
# retrieve all the dogs (well, the first 'page' anyway) and tell them hi!
|
130
|
+
# note: we're calling collection() instead of entity() because we have several
|
111
131
|
dogs = application['dogs'].collection
|
112
|
-
|
113
|
-
|
132
|
+
|
133
|
+
# you can iterate a collection just like an array
|
134
|
+
dogs.each do |dog|
|
135
|
+
puts "Hello, #{dog.name}!"
|
114
136
|
end
|
115
137
|
|
116
|
-
# "Benji, come!"
|
117
|
-
|
138
|
+
# Let's get Benji ("Benji, come!"), but this time we'll retrieve by query
|
139
|
+
response = dogs.query "select * where name = 'Benji'"
|
140
|
+
|
141
|
+
# we could call "response.collection.first"
|
142
|
+
# but there's a shortcut: entity() will also return the first
|
143
|
+
benji = response.entity
|
118
144
|
|
119
|
-
# modify Benji's attributes & save
|
145
|
+
# modify Benji's attributes & save to the server
|
120
146
|
benji.location = 'home' # use attribute access
|
121
|
-
benji['breed'] = 'American Cocker Spaniel' # or access
|
147
|
+
benji['breed'] = 'American Cocker Spaniel' # or access attributes like a Hash
|
122
148
|
benji.save
|
123
149
|
|
124
|
-
# query for the dogs that are home (should just be Benji)
|
150
|
+
# now query for the dogs that are home (should just be Benji)
|
125
151
|
dogs = application['dogs'].query("select * where location = 'home'").collection
|
126
152
|
if dogs.size == 1 && dogs.first.location == 'home'
|
127
153
|
puts "Benji's home!"
|
@@ -129,7 +155,7 @@ you shouldn't need to do anything!)
|
|
129
155
|
|
130
156
|
```
|
131
157
|
|
132
|
-
Whew. That's enough for now. But looking for a specific feature? Check out the rspecs,
|
158
|
+
Whew. That's enough for now. But looking for a specific feature? Check out the [rspecs](http://github.com/scottganyo/usergrid_iron/tree/master/spec/usergrid/core),
|
133
159
|
there are examples of nearly everything!
|
134
160
|
|
135
161
|
|
@@ -155,6 +181,11 @@ usergrid_iron/spec/spec_settings.yaml to match.)
|
|
155
181
|
|
156
182
|
## Release notes
|
157
183
|
|
184
|
+
### 0.0.6
|
185
|
+
* New features
|
186
|
+
1. iterators can now optionally cross page boundaries, eg. `collection.follow_cursor.each`
|
187
|
+
2. added facebook_login(fb_access_token) method to application
|
188
|
+
|
158
189
|
### 0.0.5
|
159
190
|
* New features
|
160
191
|
1. added create_* method for application
|
@@ -37,6 +37,18 @@ module Usergrid
|
|
37
37
|
self['counters'].get({params: options})
|
38
38
|
end
|
39
39
|
|
40
|
+
# login with Facebook token. matching user will be created in usergrid as needed.
|
41
|
+
# usergrid auth token automatically set in auth header for future requests
|
42
|
+
def facebook_login(access_token)
|
43
|
+
params = { fb_access_token: access_token }
|
44
|
+
response = self['auth/facebook'].get({ params: params })
|
45
|
+
self.auth_token = response.data['access_token']
|
46
|
+
user_uuid = response.data['user']['uuid']
|
47
|
+
@current_user = self["/users/#{user_uuid}"].get.entity
|
48
|
+
response
|
49
|
+
end
|
50
|
+
|
51
|
+
|
40
52
|
private
|
41
53
|
|
42
54
|
def _create_user(username, password, email=nil, name=nil, invite=false)
|
@@ -2,10 +2,12 @@ module Usergrid
|
|
2
2
|
class Collection < Entity
|
3
3
|
include Enumerable
|
4
4
|
|
5
|
+
attr_accessor :iterator_follows_cursor
|
5
6
|
attr_reader :query_params
|
6
7
|
|
7
8
|
def initialize(url, api_url, options={}, response=nil)
|
8
9
|
super url, api_url, options, response
|
10
|
+
@iterator_follows_cursor = false
|
9
11
|
end
|
10
12
|
|
11
13
|
def collection
|
@@ -32,6 +34,17 @@ module Usergrid
|
|
32
34
|
|
33
35
|
def each(&block)
|
34
36
|
entities.each &block
|
37
|
+
while cursor
|
38
|
+
next_page
|
39
|
+
entities.each &block
|
40
|
+
end if iterator_follows_cursor
|
41
|
+
end
|
42
|
+
|
43
|
+
# use in conjunction with each() like: collection.follow_cursor.each {|e| }
|
44
|
+
def follow_cursor
|
45
|
+
my_clone = self.clone
|
46
|
+
my_clone.iterator_follows_cursor = true
|
47
|
+
my_clone
|
35
48
|
end
|
36
49
|
|
37
50
|
def create_entity(data)
|
@@ -64,12 +77,11 @@ module Usergrid
|
|
64
77
|
end
|
65
78
|
|
66
79
|
def cursor
|
67
|
-
response.data
|
80
|
+
response.data['cursor']
|
68
81
|
end
|
69
82
|
|
70
83
|
def next_page
|
71
84
|
query(nil, @query_params.merge({cursor: cursor}))
|
72
85
|
end
|
73
|
-
|
74
86
|
end
|
75
87
|
end
|
data/lib/usergrid/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -53,7 +53,7 @@ end
|
|
53
53
|
def delete_application(application)
|
54
54
|
management = login_management
|
55
55
|
application.auth_token = management.auth_token
|
56
|
-
application.delete rescue nil # not implemented on server yet
|
56
|
+
# application.delete rescue nil # not implemented on server yet
|
57
57
|
end
|
58
58
|
|
59
59
|
def create_random_user(application, login=false)
|
@@ -177,39 +177,39 @@ describe Usergrid::Application do
|
|
177
177
|
response.entities.size.should eq size+1
|
178
178
|
end
|
179
179
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
end
|
180
|
+
# can't reliably test this - counters are batched on server
|
181
|
+
#it "should be able to create and retrieve events and retrieve counters" do
|
182
|
+
# # clear events
|
183
|
+
# {} while @application.events.entities.size > 0
|
184
|
+
#
|
185
|
+
# events_in = []
|
186
|
+
# events_in << {timestamp: 0, category: 'test', counters: { test: 1 }}
|
187
|
+
# events_in << {timestamp: 0, category: 'testme', counters: { testme: 1 }}
|
188
|
+
# events_in << {timestamp: 0, counters: { test: 1 }}
|
189
|
+
# events_in.each {|e| @application.create_entity :events, e }
|
190
|
+
#
|
191
|
+
# events_out = []
|
192
|
+
# events_out << @application.events.entity
|
193
|
+
# events_out << @application.events.entity
|
194
|
+
# events_out << @application.events.entity
|
195
|
+
#
|
196
|
+
# (0..2).each do |i|
|
197
|
+
# events_in[i][:category].should eq events_out[i]['category']
|
198
|
+
# end
|
199
|
+
#
|
200
|
+
# response = @application.events
|
201
|
+
# response.entities.size.should eq 0
|
202
|
+
#
|
203
|
+
# # get and test counters
|
204
|
+
# counter_names = @application.counter_names
|
205
|
+
# counter_names.should include 'test'
|
206
|
+
# counter_names.should include 'testme'
|
207
|
+
#
|
208
|
+
# response = @application.counter 'test'
|
209
|
+
# counter = response.data.counters.first
|
210
|
+
# counter.name.should eq 'test'
|
211
|
+
# counter.values.last.first.value.should be > 0
|
212
|
+
#end
|
213
213
|
|
214
214
|
it "should be able to create, retrieve, and delete roles" do
|
215
215
|
size = @application.roles.collection.size
|
@@ -5,10 +5,8 @@ describe Usergrid::Collection do
|
|
5
5
|
@user = create_random_user @application, true
|
6
6
|
|
7
7
|
@collection = @application['tests'].collection
|
8
|
-
@entity_data =
|
9
|
-
|
10
|
-
test = { name: "name_#{i}", value: "value_#{i+1}" }
|
11
|
-
@entity_data << test
|
8
|
+
@entity_data = (1..25).collect do |i|
|
9
|
+
{ name: "name_#{i}", value: "value_#{i+1}" }
|
12
10
|
end
|
13
11
|
@collection.create_entities @entity_data
|
14
12
|
end
|
@@ -51,8 +49,8 @@ describe Usergrid::Collection do
|
|
51
49
|
it "should be able to respect query reversal and limits" do
|
52
50
|
@collection.query nil, reversed: true, start: 5, cursor: nil, limit: 2, permission: nil
|
53
51
|
@collection.size.should eq 2
|
54
|
-
@collection[0].name.should eq @entity_data[
|
55
|
-
@collection[1].name.should eq @entity_data[
|
52
|
+
@collection[0].name.should eq @entity_data[@entity_data.size-1][:name]
|
53
|
+
@collection[1].name.should eq @entity_data[@entity_data.size-2][:name]
|
56
54
|
end
|
57
55
|
|
58
56
|
it "should be able to select the start by uuid" do
|
@@ -79,4 +77,21 @@ describe Usergrid::Collection do
|
|
79
77
|
entity.name.should eq @entity_data[4][:name]
|
80
78
|
end
|
81
79
|
|
80
|
+
it "should be able to iterate within the page" do
|
81
|
+
@collection.query
|
82
|
+
@collection.cursor.should_not be_nil
|
83
|
+
count = 0
|
84
|
+
page_size = @collection.count
|
85
|
+
@collection.each {|e| count += 1 }
|
86
|
+
count.should eq page_size
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should be able to iterate over pages" do
|
90
|
+
@collection.query
|
91
|
+
@collection.cursor.should_not be_nil
|
92
|
+
count = 0
|
93
|
+
@collection.follow_cursor.each {|e| count += 1 }
|
94
|
+
count.should eq @entity_data.size
|
95
|
+
end
|
96
|
+
|
82
97
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: usergrid_iron
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
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: 2012-
|
12
|
+
date: 2012-12-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rest-client
|
@@ -149,7 +149,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
149
149
|
version: '0'
|
150
150
|
segments:
|
151
151
|
- 0
|
152
|
-
hash: -
|
152
|
+
hash: -1755977299856749079
|
153
153
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
154
154
|
none: false
|
155
155
|
requirements:
|
@@ -158,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
158
|
version: '0'
|
159
159
|
segments:
|
160
160
|
- 0
|
161
|
-
hash: -
|
161
|
+
hash: -1755977299856749079
|
162
162
|
requirements: []
|
163
163
|
rubyforge_project:
|
164
164
|
rubygems_version: 1.8.24
|