Skip to content

Commit

Permalink
enable simpler and more comprehensive access to retrieve/query collec…
Browse files Browse the repository at this point in the history
…tions, update examples, update version
  • Loading branch information
theganyo committed Nov 9, 2012
1 parent 1db04b4 commit fd39fe7
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 55 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ application = Usergrid::Application.new "#{usergrid_api}/#{organization}/#{appli
application.login username, password application.login username, password
# create and store a dog in the 'dogs' collection on the server # create and store a dog in the 'dogs' collection on the server
response = application.create_entity 'dogs', { breed: 'Black Mouth Cur', name: 'Old Yeller' } response = application.create_dog breed: 'Black Mouth Cur', name: 'Old Yeller'
# let's get the dog from the response and grab its persistent id # let's get the dog from the response and grab its persistent id
dog = response.entity dog = response.entity
Expand Down Expand Up @@ -89,7 +89,7 @@ you shouldn't need to do anything!)
new_application = organization.create_application app_name new_application = organization.create_application app_name
# create an user for our application # create an user for our application
new_application.create_user 'username', 'password' new_application.create_user username: 'username', password: 'password'
## now we can play with the puppies! ## ## now we can play with the puppies! ##
Expand All @@ -99,10 +99,11 @@ you shouldn't need to do anything!)
application.login 'username', 'password' application.login 'username', 'password'
# we can start with our dog again # we can start with our dog again
application.create_entity 'dogs', { breed: 'Black Mouth Cur', name: 'Old Yeller' } application.create_dog breed: 'Black Mouth Cur', name: 'Old Yeller'
# but this time let's create several more dogs at once # but this time let's create several more dogs at once
application.create_entities 'dogs', [{ breed: 'Catalan sheepdog', name: 'Einstein' }, application.create_dogs [
{ breed: 'Catalan sheepdog', name: 'Einstein' },
{ breed: 'Cocker Spaniel', name: 'Lady' }, { breed: 'Cocker Spaniel', name: 'Lady' },
{ breed: 'Mixed', name: 'Benji' }] { breed: 'Mixed', name: 'Benji' }]
Expand All @@ -122,7 +123,7 @@ you shouldn't need to do anything!)
# query for the dogs that are home (should just be Benji) # query for the dogs that are home (should just be Benji)
dogs = application['dogs'].query("select * where location = 'home'").collection dogs = application['dogs'].query("select * where location = 'home'").collection
if dogs.size == 1 && dogs.first == 'home' if dogs.size == 1 && dogs.first.location == 'home'
puts "Benji's home!" puts "Benji's home!"
end end
Expand Down Expand Up @@ -154,6 +155,12 @@ usergrid_iron/spec/spec_settings.yaml to match.)


## Release notes ## Release notes


### 0.0.5
* New features
1. added create_* method for application
* Incompatible changes
1. deprecated (Application::create_user username, password, ...) method

### 0.0.4 ### 0.0.4
* New features * New features
1. empty? check for collection 1. empty? check for collection
Expand Down
56 changes: 15 additions & 41 deletions lib/usergrid/core/application.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ def initialize(url, options={})
super url, api_url, options super url, api_url, options
end end


def create_user(username, password, email=nil, name=nil, invite=false)
user_hash = { username: username,
password: password,
email: email,
name: name,
invite: invite }
create_entity 'users', user_hash
end

# note: collection_name s/b plural, but the server will change it if not # note: collection_name s/b plural, but the server will change it if not
def create_entity(collection_name, entity_data) def create_entity(collection_name, entity_data)
self[collection_name].post entity_data self[collection_name].post entity_data
Expand All @@ -27,44 +18,15 @@ def method_missing(method, *args, &block)
method_s = method.to_s method_s = method.to_s
if method_s.start_with? 'create_' if method_s.start_with? 'create_'
entity = method_s.split('_')[1] entity = method_s.split('_')[1]
return _create_user *args if entity == 'user' && args[0].is_a?(String) # backwards compatibility
create_entity entity, *args create_entity entity, *args
elsif method_s.end_with? 's' # shortcut for retrieving collections
self[method].query(*args)
else else
super method, args, block super method, args, block
end end
end end


def users(query=nil, options={})
self[__method__].query(query, options)
end

def groups(query=nil, options={})
self[__method__].query(query, options)
end

def activities(query=nil, options={})
self[__method__].query(query, options)
end

def devices(query=nil, options={})
self[__method__].query(query, options)
end

def assets(query=nil, options={})
self[__method__].query(query, options)
end

def folders(query=nil, options={})
self[__method__].query(query, options)
end

def events(query=nil, options={})
self[__method__].query(query, options)
end

def roles(query=nil, options={})
self[__method__].query(query, options)
end

def counter_names def counter_names
self['counters'].get.data.data self['counters'].get.data.data
end end
Expand All @@ -75,5 +37,17 @@ def counter(name, other_params={})
self['counters'].get({params: options}) self['counters'].get({params: options})
end end


private

def _create_user(username, password, email=nil, name=nil, invite=false)
LOG.warn "create_user(username, password, ...) is deprecated"
user_hash = { username: username,
password: password,
email: email,
name: name,
invite: invite }
create_entity 'users', user_hash
end

end end
end end
7 changes: 7 additions & 0 deletions lib/usergrid/core/resource.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -59,10 +59,17 @@ def update_query(updates, query=nil, options={})
end end


def entity def entity
get unless response
response.entity response.entity
end end


def entities
get unless response
response.entities
end

def collection def collection
get unless response
Collection.new url, api_url, options, response Collection.new url, api_url, options, response
end end


Expand Down
2 changes: 1 addition & 1 deletion lib/usergrid/version.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,3 @@
module Usergrid module Usergrid
VERSION = '0.0.4' VERSION = '0.0.5'
end end
39 changes: 31 additions & 8 deletions spec/usergrid/core/application_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
delete_application @application delete_application @application
end end


it "should be able to create a user using deprecated syntax" do
random = SecureRandom.hex
response = @application.create_user "username_#{random}", 'password'
response.entity.uuid.should_not be_nil
end

it "should be able to create, login, and delete a user" do it "should be able to create, login, and delete a user" do
random = SecureRandom.hex random = SecureRandom.hex
application = Usergrid::Application.new @application.url # create application resource that's not logged in application = Usergrid::Application.new @application.url # create application resource that's not logged in
response = application.create_user "username_#{random}", 'password' response = application.create_user username: "username_#{random}", password: 'password'
entity = response.entity entity = response.entity
application.login "username_#{random}", 'password' application.login "username_#{random}", 'password'
begin begin
Expand Down Expand Up @@ -252,23 +258,40 @@
end end


it "should be able to create a new collection and access it" do it "should be able to create a new collection and access it" do
entities = (1..4).collect do |i| entities = (1..4).collect { |i| { name: "test_#{i}" } }
{ name: "test_#{i}" }
end
@application.create_entities 'tests', entities @application.create_entities 'tests', entities
response = @application['tests'].get response = @application['tests'].get
collection = response.collection collection = response.collection
collection.size.should eq 4 collection.size.should eq 4
end end


it "should be able to create a new collection via create_ method and access it" do it "should be able to create a new collection via create_ method and access it" do
entities = (1..4).collect do |i| entities = (1..4).collect { |i| { name: "test_#{i}" } }
{ name: "test_#{i}" }
end
@application.create_moretests entities @application.create_moretests entities
response = @application['moretests'].get response = @application['tests'].get
collection = response.collection collection = response.collection
collection.size.should eq 4 collection.size.should eq 4
end end


it "should be able to access a collection without calling get" do
entities = (1..4).collect { |i| { name: "test_#{i}" } }
@application.create_entities 'tests', entities
collection = @application['tests'].collection
collection.size.should eq 4
end

it "should be able to access entities without calling get" do
entities = (1..4).collect { |i| { name: "test_#{i}" } }
@application.create_entities 'tests', entities
entities = @application['tests'].entities
entities.size.should eq 4
end

it "should be able to query using dot notation" do
entities = (1..4).collect { |i| { name: "test_#{i}" } }
@application.create_btests entities
entities = @application.btests("name = 'test_1'").entities
entities.size.should eq 1
end

end end

0 comments on commit fd39fe7

Please sign in to comment.