Class: Parse::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/client.rb

Overview

A class which encapsulates the HTTPS communication with the Parse API server. Currently uses the Patron library for low-level HTTP communication.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/parse/client.rb', line 16

def initialize(data = {})
  @host           = data[:host] || Protocol::HOST
  @application_id = data[:application_id]
  @api_key        = data[:api_key]
  @session        = Patron::Session.new

  @session.base_url                 = "https://#{host}"
  @session.headers["Content-Type"]  = "application/json"
  @session.headers["Accept"]        = "application/json"
  @session.headers["User-Agent"]    = "Parse for Ruby, 0.0"
  @session.headers[Protocol::HEADER_API_KEY]  = @api_key
  @session.headers[Protocol::HEADER_APP_ID]   = @application_id
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#application_idObject

Returns the value of attribute application_id.



12
13
14
# File 'lib/parse/client.rb', line 12

def application_id
  @application_id
end

#hostObject

Returns the value of attribute host.



11
12
13
# File 'lib/parse/client.rb', line 11

def host
  @host
end

#sessionObject

Returns the value of attribute session.



14
15
16
# File 'lib/parse/client.rb', line 14

def session
  @session
end

Instance Method Details

#delete(uri) ⇒ Object



65
66
67
# File 'lib/parse/client.rb', line 65

def delete(uri)
  request(uri, :delete)
end

#get(uri) ⇒ Object



53
54
55
# File 'lib/parse/client.rb', line 53

def get(uri)
  request(uri)
end

#post(uri, body) ⇒ Object



57
58
59
# File 'lib/parse/client.rb', line 57

def post(uri, body)
  request(uri, :post, body)
end

#put(uri, body) ⇒ Object



61
62
63
# File 'lib/parse/client.rb', line 61

def put(uri, body)
  request(uri, :put, body)
end

#request(uri, method = :get, body = nil, query = nil) ⇒ Object

Perform an HTTP request for the given uri and method with common basic response handling. Will raise a ParseProtocolError if the response has an error status code, and will return the parsed JSON body on success, if there is one.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/parse/client.rb', line 34

def request(uri, method = :get, body = nil, query = nil)
  options = {}
  if body
    options[:data] = body
  end
  if query
    options[:query] = query
  end

  response = @session.request(method, uri, {}, options)
  if response.status >= 400
    raise ParseProtocolError, response
  else
    if response.body
      return JSON.parse response.body
    end
  end
end