Class: Usergrid::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/usergrid.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/usergrid.rb', line 7

def initialize(options = {})
  @app = options[:app]
  @key = options[:key]
  @secret = options[:secret]
end

Instance Method Details

#access_tokenObject



21
22
23
# File 'lib/usergrid.rb', line 21

def access_token
  @access_token ||= self.class.get("#{base_uri}/token?grant_type=client_credentials&client_id=#{@key}&client_secret=#{@secret}").parsed_response["access_token"]
end

#base_uriObject



25
26
27
# File 'lib/usergrid.rb', line 25

def base_uri
  "http://api.usergrid.com/#{@app}"
end

#create_user(params) ⇒ Object



17
18
19
# File 'lib/usergrid.rb', line 17

def create_user params
  post("/users", params)
end

#get(url) ⇒ Object



29
30
31
# File 'lib/usergrid.rb', line 29

def get url
  self.class.get("#{base_uri}#{url}", :headers => headers_hash)
end

#headers_hashObject



38
39
40
# File 'lib/usergrid.rb', line 38

def headers_hash
  {"Authorization" => "Bearer #{access_token}"}
end

#post(url, params) ⇒ Object



33
34
35
36
# File 'lib/usergrid.rb', line 33

def post url, params
  options = {:body => params.to_json, :headers => {"Content-Type" => "application/json"}}
  self.class.post("#{base_uri}#{url}", options)
end

#usersObject



13
14
15
# File 'lib/usergrid.rb', line 13

def users
  get("/users")
end