Class: Usergrid::Application

Inherits:
Resource
  • Object
show all
Defined in:
lib/usergrid/core/application.rb

Constant Summary

Constants inherited from Resource

Resource::DEFAULT_API_URL, Resource::RESERVED, Resource::TYPE_HEADERS

Instance Attribute Summary

Attributes inherited from Resource

#api_url, #current_user, #response

Instance Method Summary collapse

Methods inherited from Resource

#[], #api_resource, #application, #auth_token, #auth_token=, #collection, #delete_query, #entities, #entity, #get, #logged_in?, #login, #logout, #management, #post, #put, #query, #update_query

Constructor Details

#initialize(url, options = {}) ⇒ Application

Returns a new instance of Application.



4
5
6
7
8
# File 'lib/usergrid/core/application.rb', line 4

def initialize(url, options={})
  org_name = url.split('/')[-2]
  api_url = url[0..url.index(org_name)-2]
  super url, api_url, options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

allow create_something(hash_or_array) method



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/usergrid/core/application.rb', line 17

def method_missing(method, *args, &block)
  method_s = method.to_s
  if method_s.start_with? 'create_'
    entity = method_s.split('_')[1]
    return _create_user *args if entity == 'user' && args[0].is_a?(String) # backwards compatibility
    create_entity entity, *args
  elsif method_s.end_with? 's' # shortcut for retrieving collections
    self[method].query(*args)
  else
    super method, args, block
  end
end

Instance Method Details

#counter(name, other_params = {}) ⇒ Object

other_params: ‘start_time’ (ms), ‘end_time’ (ms), ‘resolution’ (minutes)



35
36
37
38
# File 'lib/usergrid/core/application.rb', line 35

def counter(name, other_params={})
  options = other_params.merge({counter: name})
  self['counters'].get({params: options})
end

#counter_namesObject



30
31
32
# File 'lib/usergrid/core/application.rb', line 30

def counter_names
  self['counters'].get.data.data
end

#create_entity(collection_name, entity_data) ⇒ Object Also known as: create_entities

note: collection_name s/b plural, but the server will change it if not



11
12
13
# File 'lib/usergrid/core/application.rb', line 11

def create_entity(collection_name, entity_data)
  self[collection_name].post entity_data
end

#facebook_login(access_token) ⇒ Object

login with Facebook token. matching user will be created in usergrid as needed. usergrid auth token automatically set in auth header for future requests



42
43
44
45
46
47
48
49
# File 'lib/usergrid/core/application.rb', line 42

def (access_token)
  params = { fb_access_token: access_token }
  response = self['auth/facebook'].get({ params: params })
  self.auth_token = response.data['access_token']
  user_uuid = response.data['user']['uuid']
  @current_user = self["/users/#{user_uuid}"].get.entity
  response
end

#login_credentials(client_id, client_secret) ⇒ Object



51
52
53
54
# File 'lib/usergrid/core/application.rb', line 51

def (client_id, client_secret)
  response = self['token'].post grant_type: 'client_credentials', client_id: client_id, client_secret: client_secret
  self.auth_token = response.data['access_token']
end