usergrid_iron 0.0.5 → 0.0.6

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.
data/README.md CHANGED
@@ -5,27 +5,30 @@ REST API with minimal dependencies.
5
5
 
6
6
  ## Installation
7
7
 
8
- Add this line to your application's Gemfile:
8
+ ### Project
9
+ Add the gem to your project's Gemfile:
9
10
 
10
11
  gem 'usergrid_iron'
11
12
 
12
- And then execute:
13
+ Then rebuild your bundle:
13
14
 
14
15
  $ bundle
15
16
 
16
- Or install it yourself as:
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
- ### Not familiar with Usergrid / Apigee's App Services?
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
- #### It's great stuff! Check it out, here:
28
+ App Services docs: <http://apigee.com/docs/usergrid/>
29
+ Open source stack: <https://github.com/apigee/usergrid-stack>
26
30
 
27
- Docs: <http://apigee.com/docs/usergrid/>
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 in the 'dogs' collection on the server
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
- # let's get the dog from the response and grab its persistent id
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
- # let's retrieve a dog from the server by UUID
57
- same_dog = application['dogs'][uuid].entity
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
- puts "My dog's name is: #{same_dog.name}"
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 = Usergrid::Application.new "#{usergrid_api}/#{org_name}/#{app_name}"
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
- dogs.each do |dog| # works just like an array
113
- puts "Hello, #{dog.name}!" # entities automatically have attributes
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
- benji = dogs.query("select * where name = 'Benji'").entity # shortcut: entity() returns the first
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 it like a Hash
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.cursor || nil
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
@@ -1,3 +1,3 @@
1
1
  module Usergrid
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
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
- it "should be able to create and retrieve events and retrieve counters" do
181
- # clear events
182
- {} while @application.events.entities.size > 0
183
-
184
- events_in = []
185
- events_in << {timestamp: 0, category: 'test', counters: { test: 1 }}
186
- events_in << {timestamp: 0, category: 'testme', counters: { testme: 1 }}
187
- events_in << {timestamp: 0, counters: { test: 1 }}
188
- events_in.each {|e| @application.create_entity :events, e }
189
-
190
- events_out = []
191
- events_out << @application.events.entity
192
- events_out << @application.events.entity
193
- events_out << @application.events.entity
194
-
195
- (0..2).each do |i|
196
- events_in[i][:category].should eq events_out[i]['category']
197
- end
198
-
199
- response = @application.events
200
- response.entities.size.should eq 0
201
-
202
- # get and test counters
203
- counter_names = @application.counter_names
204
- counter_names.should include 'test'
205
- counter_names.should include 'testme'
206
-
207
- response = @application.counter 'test'
208
- counter = response.data.counters.first
209
- counter.name.should eq 'test'
210
- # can't reliably test this - counters are batched on server
211
- #counter.values.last.first.value.should be > 0
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
- (1..10).each do |i|
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[9][:name]
55
- @collection[1].name.should eq @entity_data[8][:name]
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.5
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-11-16 00:00:00.000000000 Z
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: -2026056560338254280
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: -2026056560338254280
161
+ hash: -1755977299856749079
162
162
  requirements: []
163
163
  rubyforge_project:
164
164
  rubygems_version: 1.8.24