usergrid_iron 0.0.3 → 0.0.4

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.
@@ -54,10 +54,6 @@ module Usergrid
54
54
  self[__method__].query(query, options)
55
55
  end
56
56
 
57
- def rolenames(query=nil, options={})
58
- self[__method__].query(query, options).data.data.keys
59
- end
60
-
61
57
  def counter_names
62
58
  self['counters'].get.data.data
63
59
  end
@@ -39,13 +39,13 @@ module Usergrid
39
39
  end
40
40
  alias_method :create_entities, :create_entity
41
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
42
+ # options: 'reversed', 'start', 'cursor', 'limit', 'permission'
43
+ def update_query(updates, query=nil, options={})
44
+ options = options.symbolize_keys
45
+ @query_params = query ? options.merge({ql: query}) : options
46
+ self.put(updates, {params: @query_params })
47
+ self
48
+ end
49
49
 
50
50
  # options: 'reversed', 'start', 'cursor', 'limit', 'permission'
51
51
  def query(query=nil, options={})
@@ -59,6 +59,10 @@ module Usergrid
59
59
  entities.size
60
60
  end
61
61
 
62
+ def empty?
63
+ entities.empty?
64
+ end
65
+
62
66
  def cursor
63
67
  response.data.cursor || nil
64
68
  end
@@ -6,7 +6,7 @@ module Usergrid
6
6
  DEFAULT_API_URL = 'https://api.usergrid.com'
7
7
  TYPE_HEADERS = { :content_type => :json, :accept => :json }
8
8
 
9
- attr_reader :current_user, :auth_token, :api_url
9
+ attr_reader :current_user, :api_url
10
10
 
11
11
  def initialize(resource_url=DEFAULT_API_URL, api_url=nil, options={}, response=nil)
12
12
  options[:headers] = TYPE_HEADERS.merge options[:headers] || {}
@@ -34,7 +34,7 @@ module Usergrid
34
34
  end
35
35
 
36
36
  def logged_in?
37
- !!@auth_token
37
+ !!auth_token
38
38
  end
39
39
 
40
40
  def management
@@ -46,11 +46,18 @@ module Usergrid
46
46
  Usergrid::Application.new concat_urls(api_url, "#{organization}/#{application}"), options
47
47
  end
48
48
 
49
+ # options: 'reversed', 'start', 'cursor', 'limit', 'permission'
49
50
  def query(query=nil, options={})
50
51
  options = options.merge({ql: query}) if query
51
52
  get({params: options})
52
53
  end
53
54
 
55
+ # options: 'reversed', 'start', 'cursor', 'limit', 'permission'
56
+ def update_query(updates, query=nil, options={})
57
+ options = options.merge({ql: query}) if query
58
+ put(updates, {params: options})
59
+ end
60
+
54
61
  def entity
55
62
  response.entity
56
63
  end
@@ -87,16 +94,7 @@ module Usergrid
87
94
  self.response = super payload, additional_headers, &block
88
95
  end
89
96
 
90
- protected
91
-
92
- attr_reader :response
93
-
94
- def response=(response)
95
- @response = response
96
- end
97
-
98
97
  def auth_token=(auth_token)
99
- @auth_token = auth_token
100
98
  if auth_token
101
99
  @options[:headers].merge!({ Authorization: "Bearer #{auth_token}" })
102
100
  else
@@ -104,6 +102,22 @@ module Usergrid
104
102
  end
105
103
  end
106
104
 
105
+ def auth_token
106
+ begin
107
+ @options[:headers][:Authorization].gsub 'Bearer ', ''
108
+ rescue
109
+ nil
110
+ end
111
+ end
112
+
113
+ protected
114
+
115
+ attr_reader :response
116
+
117
+ def response=(response)
118
+ @response = response
119
+ end
120
+
107
121
  # add verbose debugging of response body
108
122
  def handle_response(response, request, result, &block)
109
123
  LOG.debug "response.body = #{response}"
@@ -28,7 +28,7 @@ module RestClient
28
28
  entities_data = data['entities'] || data['data'] || data['messages'] || data['list']
29
29
  raise "unable to determine entities from: #{data}" unless entities_data.is_a?(Array)
30
30
  entities_data.each do |e|
31
- e['uri'] = resource.concat_urls(data['uri'], e['uuid']) if e.is_a?(Hash) && e['uuid']
31
+ e['uri'] = concat_urls(data['uri'], e['uuid']) if e.is_a?(Hash) && e['uuid']
32
32
  end
33
33
  @entities_data = entities_data
34
34
  end
@@ -62,7 +62,20 @@ module RestClient
62
62
  end
63
63
 
64
64
  def entity
65
- Usergrid::Entity.new entity_data['uri'], resource.api_url, resource.options, self
65
+ Usergrid::Entity.new(entity_data['uri'], resource.api_url, resource.options, self) if entity_data
66
66
  end
67
+
68
+ protected
69
+
70
+ def concat_urls(url, suburl) # :nodoc:
71
+ url = url.to_s
72
+ suburl = suburl.to_s
73
+ if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
74
+ url + suburl
75
+ else
76
+ "#{url}/#{suburl}"
77
+ end
78
+ end
79
+
67
80
  end
68
81
  end
@@ -1,3 +1,3 @@
1
1
  module Usergrid
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -17,9 +17,14 @@ SimpleCov.start
17
17
 
18
18
  SPEC_SETTINGS = YAML::load_file(File.join File.dirname(__FILE__), 'spec_settings.yaml')
19
19
 
20
+ def login_management
21
+ management = Usergrid::Resource.new(SPEC_SETTINGS[:api_url]).management
22
+ management.login SPEC_SETTINGS[:organization][:username], SPEC_SETTINGS[:organization][:password]
23
+ management
24
+ end
25
+
20
26
  # ensure we are correctly setup (management login & organization)
21
- management = Usergrid::Resource.new(SPEC_SETTINGS[:api_url]).management
22
- management.login SPEC_SETTINGS[:management][:username], SPEC_SETTINGS[:management][:password]
27
+ management = login_management
23
28
 
24
29
  begin
25
30
  management.create_organization(SPEC_SETTINGS[:organization][:name],
@@ -36,14 +41,6 @@ rescue
36
41
  end
37
42
  end
38
43
 
39
- def app_endpoint
40
- "#{SPEC_SETTINGS[:organization][:name]}/#{SPEC_SETTINGS[:application][:name]}"
41
- end
42
-
43
- def org_endpoint
44
- "#{SPEC_SETTINGS[:organization][:name]}"
45
- end
46
-
47
44
  def create_random_application
48
45
  management = login_management
49
46
  organization = management.organization SPEC_SETTINGS[:organization][:name]
@@ -56,7 +53,7 @@ end
56
53
  def delete_application(application)
57
54
  management = login_management
58
55
  application.auth_token = management.auth_token
59
- application.delete
56
+ application.delete rescue nil # not implemented on server yet
60
57
  end
61
58
 
62
59
  def create_random_user(application, login=false)
@@ -69,9 +66,3 @@ def create_random_user(application, login=false)
69
66
  application.login user_hash[:username], user_hash[:password] if login
70
67
  entity
71
68
  end
72
-
73
- def login_management
74
- management = Usergrid::Resource.new(SPEC_SETTINGS[:api_url]).management
75
- management.login SPEC_SETTINGS[:organization][:username], SPEC_SETTINGS[:organization][:password]
76
- management
77
- end
@@ -201,20 +201,7 @@ describe Usergrid::Application do
201
201
  response = @application.counter 'test'
202
202
  counter = response.data.counters.first
203
203
  counter.name.should eq 'test'
204
- counter.values.last.first.value.should eq 2
205
- end
206
-
207
- it "should be able to create, retrieve, and delete roles via rolenames" do
208
- size = @application.rolenames.size
209
-
210
- role_name = "test-#{SecureRandom.hex}"
211
- @application.create_entity :rolenames, name: role_name
212
- role_names = @application.rolenames
213
- role_names.size.should eq size+1
214
- role_names.should include role_name
215
-
216
- @application['rolenames'][role_name].delete
217
- @application.rolenames.size.should eq size
204
+ counter.values.last.first.value.should be > 0
218
205
  end
219
206
 
220
207
  it "should be able to create, retrieve, and delete roles" do
@@ -71,12 +71,12 @@ describe Usergrid::Collection do
71
71
  @collection[0].name.should eq @entity_data[2][:name]
72
72
  end
73
73
 
74
- #it "should be able to update based on a query" do # todo: enable when server is fixed
75
- # @collection.update({new_field: 'new_value'}, "select * where name = \'#{@entity_data[4][:name]}\'")
76
- # @collection.query
77
- # entity = @collection.detect { |e| e.new_field == 'new_value' }
78
- # entity.should_not be_nil
79
- # entity.name.should eq @entity_data[4][:name]
80
- #end
74
+ it "should be able to update based on a query" do
75
+ @collection.update_query({new_field: 'new_value'}, "select * where name = \'#{@entity_data[4][:name]}\'")
76
+ @collection.query
77
+ entity = @collection.detect { |e| e['new_field'] == 'new_value' }
78
+ entity.should_not be_nil
79
+ entity.name.should eq @entity_data[4][:name]
80
+ end
81
81
 
82
82
  end
@@ -42,4 +42,8 @@ describe Usergrid::Entity do
42
42
  empty.data?.should be_false
43
43
  end
44
44
 
45
+ it "should return nil if empty result" do
46
+ @application['foobars'].get.entity.should be_nil
47
+ end
48
+
45
49
  end
@@ -18,6 +18,7 @@ describe Usergrid::Resource do
18
18
  end
19
19
 
20
20
  it "should fail with a 401 when lacking auth" do
21
+ app_endpoint = "#{SPEC_SETTINGS[:organization][:name]}/#{SPEC_SETTINGS[:application][:name]}"
21
22
  expect { @resource[app_endpoint].get }.to raise_error(RestClient::Unauthorized) { |exception|
22
23
  exception.response.code.should eq 401
23
24
  }
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.3
4
+ version: 0.0.4
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-09-26 00:00:00.000000000 Z
12
+ date: 2012-10-13 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: -270819572663973399
152
+ hash: 1515715500160031430
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: -270819572663973399
161
+ hash: 1515715500160031430
162
162
  requirements: []
163
163
  rubyforge_project:
164
164
  rubygems_version: 1.8.24