usergrid_iron 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm_gemset_create_on_use_flag=1
2
+ rvm gemset use 'usergrid'
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in usergrid_iron.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ * Copyright (c) 2012 Scott Ganyo
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use the included files except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # Usergrid_iron
2
+
3
+ Usergrid_iron enables simple, low-level Ruby access to Apigee's App Services (aka Usergrid) REST API with minimal
4
+ dependencies.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'usergrid_iron'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install usergrid_iron
20
+
21
+
22
+ ## Usage
23
+
24
+ 1. Not familiar with Usergrid / Apigee's App Services? It's great stuff! Check it out, here:
25
+
26
+ Docs: http://apigee.com/docs/usergrid/
27
+ Open source: https://github.com/apigee/usergrid-stack
28
+
29
+ 2. Getting started with the Usergrid_iron SDK is simple! Let's run through some of the
30
+ basic concepts:
31
+
32
+ <pre>
33
+ require 'usergrid_iron'
34
+
35
+ # set the Usergrid (or Apigee App Services) URL
36
+ usergrid_api = Usergrid::Resource.new 'http://localhost:8080'
37
+
38
+ # get a management context & login the superuser
39
+ management = usergrid_api.management
40
+ management.login 'test', 'test'
41
+
42
+ # get the organization context & create a new application
43
+ app_name = 'dogs_application'
44
+ organization = management.organization 'test-organization'
45
+ application = organization.create_application app_name
46
+
47
+ # create an application user
48
+ application.create_user 'app_username', 'app_name', 'app_email@test.com', 'password'
49
+
50
+ # get a new application context and login as the new user
51
+ application = usergrid_api.application 'test-organization', app_name
52
+ application.login 'app_username', 'password'
53
+
54
+ # create a dog
55
+ application.create_entity 'dogs', { breed: 'Black Mouth Cur', name: 'Old Yeller' }
56
+
57
+ # create several more dogs
58
+ application.create_entities 'dogs', [{ breed: 'Catalan sheepdog', name: 'Einstein' },
59
+ { breed: 'Cocker Spaniel', name: 'Lady' },
60
+ { breed: 'Mixed', name: 'Benji' }]
61
+
62
+ # retrieves the dogs collection from Usergrid and prints their names
63
+ dogs = application['dogs'].get.collection
64
+ dogs.each do |dog| # works just like an array
65
+ pp dog.name # entities automatically have attributes
66
+ end
67
+
68
+ # query for a dog named Benji
69
+ dogs.query "select * where name = 'Benji'" # repopulates the collection
70
+ benji = dogs.first
71
+
72
+ # modify Benji's attributes & save
73
+ benji.command = 'come home' # use attribute access
74
+ benji['breed'] = 'American Cocker Spaniel' # or access it like a Hash
75
+ benji.save
76
+
77
+ # get the dog by uuid - the attributes were saved!
78
+ dog = application["dogs/#{benji.uuid}"].get.entity
79
+ if dog.command == 'come home'
80
+ puts "Benji's coming home!"
81
+ else
82
+ raise 'Benji is a lost puppy!'
83
+ end
84
+
85
+ </pre>
86
+
87
+ Looking for a specific feature? Check out the rspecs, there are examples of almost everything!
88
+
89
+
90
+ ## Contributing
91
+
92
+ We welcome your enhancements!
93
+
94
+ 1. Fork it
95
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
96
+ 3. Write some broken rspecs.
97
+ 4. Fix the rspecs with your new code.
98
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
99
+ 4. Push your changes to the upstream branch (`git push origin my-new-feature`)
100
+ 5. Create new Pull Request
101
+
102
+ We've got 100% rspec coverage and we're looking to keep it that way!
103
+ In order to run the tests, check out the Usergrid open source project
104
+ (https://github.com/apigee/usergrid-stack), build, and launch it locally.
105
+
106
+ (Note: If you change your local Usergrid setting from the default, be sure to update
107
+ usergrid_iron/spec/spec_settings.yaml to match.)
108
+
109
+
110
+ ## Notes
111
+
112
+ The following features are not currently implemented on the server:
113
+
114
+ * delete organization
115
+ * delete application
116
+ * delete user
117
+
118
+
119
+ ## Copyright
120
+
121
+ * Copyright (c) 2012 Scott Ganyo
122
+ *
123
+ * Licensed under the Apache License, Version 2.0 (the "License");
124
+ * you may not use the included files except in compliance with the License.
125
+ * You may obtain a copy of the License at
126
+ *
127
+ * http://www.apache.org/licenses/LICENSE-2.0
128
+ *
129
+ * Unless required by applicable law or agreed to in writing, software
130
+ * distributed under the License is distributed on an "AS IS" BASIS,
131
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132
+ * See the License for the specific language governing permissions and
133
+ * limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ RSpec::Core::RakeTask.new("spec:coverage")
data/bin/autospec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'autospec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'autospec')
data/bin/htmldiff ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'htmldiff' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('diff-lcs', 'htmldiff')
data/bin/ldiff ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'ldiff' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('diff-lcs', 'ldiff')
data/bin/restclient ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'restclient' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rest-client', 'restclient')
data/bin/rspec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'rspec')
data/lib/usergrid.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'logger'
2
+ require 'json'
3
+
4
+ module Usergrid
5
+
6
+ LOG = Logger.new(STDOUT)
7
+
8
+ USERGRID_PATH = File.join File.dirname(__FILE__), 'usergrid'
9
+
10
+ def self.usergrid_path *path
11
+ File.join USERGRID_PATH, *path
12
+ end
13
+
14
+ require usergrid_path('version')
15
+
16
+ require usergrid_path('extensions', 'hash')
17
+ require usergrid_path('extensions', 'response')
18
+
19
+ require usergrid_path('core', 'resource')
20
+ require usergrid_path('core', 'management')
21
+ require usergrid_path('core', 'organization')
22
+ require usergrid_path('core', 'application')
23
+ require usergrid_path('core', 'entity')
24
+ require usergrid_path('core', 'collection')
25
+
26
+ #autoload :Management, usergrid_path('core', 'management')
27
+ end
@@ -0,0 +1,70 @@
1
+ module Usergrid
2
+ class Application < Resource
3
+
4
+ def initialize(url, api_url, options)
5
+ super url, api_url, options
6
+ end
7
+
8
+ def create_user(username, name, email, password, invite=false)
9
+ data = { username: username,
10
+ name: name,
11
+ email: email,
12
+ password: password,
13
+ invite: invite }
14
+ create_entity 'users', data
15
+ end
16
+
17
+ # note: collection_name s/b plural!
18
+ def create_entity(collection_name, entity_data)
19
+ self[collection_name].post entity_data
20
+ end
21
+ alias_method :create_entities, :create_entity
22
+
23
+ def users(query=nil, options={})
24
+ self[__method__].query(query, options)
25
+ end
26
+
27
+ def groups(query=nil, options={})
28
+ self[__method__].query(query, options)
29
+ end
30
+
31
+ def activities(query=nil, options={})
32
+ self[__method__].query(query, options)
33
+ end
34
+
35
+ def devices(query=nil, options={})
36
+ self[__method__].query(query, options)
37
+ end
38
+
39
+ def assets(query=nil, options={})
40
+ self[__method__].query(query, options)
41
+ end
42
+
43
+ def folders(query=nil, options={})
44
+ self[__method__].query(query, options)
45
+ end
46
+
47
+ def events(query=nil, options={})
48
+ self[__method__].query(query, options)
49
+ end
50
+
51
+ def roles(query=nil, options={})
52
+ self[__method__].query(query, options)
53
+ end
54
+
55
+ def rolenames(query=nil, options={})
56
+ self[__method__].query(query, options).data.data.keys
57
+ end
58
+
59
+ def counter_names
60
+ self['counters'].get.data.data
61
+ end
62
+
63
+ # other_params: 'start_time' (ms), 'end_time' (ms), 'resolution' (minutes)
64
+ def counter(name, other_params={})
65
+ options = other_params.merge({counter: name})
66
+ self['counters'].get({params: options})
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,71 @@
1
+ module Usergrid
2
+ class Collection < Entity
3
+ include Enumerable
4
+
5
+ attr_reader :query_params
6
+
7
+ def initialize(url, api_url, options={}, response=nil)
8
+ super url, api_url, options, response
9
+ end
10
+
11
+ def collection
12
+ self
13
+ end
14
+
15
+ def entities
16
+ get unless response
17
+ response.entities
18
+ end
19
+
20
+ def [](k)
21
+ entities[k]
22
+ end
23
+
24
+ def []=(k,v)
25
+ raise "unsupported operation"
26
+ end
27
+
28
+ # does not save entities! just fields (eg. 'name')
29
+ def save
30
+ super save
31
+ end
32
+
33
+ def each(&block)
34
+ entities.each &block
35
+ end
36
+
37
+ def create_entity(data)
38
+ response = self.post data
39
+ response.entity
40
+ end
41
+
42
+ # options: 'reversed', 'start', 'cursor', 'limit', 'permission'
43
+ #def update(new_data, query=nil, options={}) # todo: enable when server is fixed
44
+ # options = options.symbolize_keys
45
+ # @query_params = query ? options.merge({ql: query}) : options
46
+ # self.put({params: @query_params }, new_data)
47
+ # self
48
+ #end
49
+
50
+ # options: 'reversed', 'start', 'cursor', 'limit', 'permission'
51
+ def query(query=nil, options={})
52
+ options = options.symbolize_keys
53
+ @query_params = query ? options.merge({ql: query}) : options
54
+ self.get({params: @query_params })
55
+ self
56
+ end
57
+
58
+ def size
59
+ entities.size
60
+ end
61
+
62
+ def cursor
63
+ response.data.cursor || nil
64
+ end
65
+
66
+ def next_page
67
+ query(nil, @query_params.merge({cursor: cursor}))
68
+ end
69
+
70
+ end
71
+ end