Class: Parse::Client
- Inherits:
-
Object
- Object
- Parse::Client
- 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
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#application_id ⇒ Object
Returns the value of attribute application_id.
-
#host ⇒ Object
Returns the value of attribute host.
-
#master_key ⇒ Object
Returns the value of attribute master_key.
-
#session ⇒ Object
Returns the value of attribute session.
-
#session_token ⇒ Object
Returns the value of attribute session_token.
Instance Method Summary collapse
- #delete(uri) ⇒ Object
- #get(uri) ⇒ Object
-
#initialize(data = {}) ⇒ Client
constructor
A new instance of Client.
- #post(uri, body) ⇒ Object
- #put(uri, body) ⇒ Object
-
#request(uri, method = :get, body = nil, query = nil, max_retries = 2) ⇒ Object
Perform an HTTP request for the given uri and method with common basic response handling.
Constructor Details
#initialize(data = {}) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/parse/client.rb', line 18 def initialize(data = {}) @host = data[:host] || Protocol::HOST @application_id = data[:application_id] @api_key = data[:api_key] @master_key = data[:master_key] @session_token = data[:session_token] @session = Patron::Session.new @session.timeout = 30 @session.connect_timeout = 30 @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" end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
13 14 15 |
# File 'lib/parse/client.rb', line 13 def api_key @api_key end |
#application_id ⇒ Object
Returns the value of attribute application_id.
12 13 14 |
# File 'lib/parse/client.rb', line 12 def application_id @application_id end |
#host ⇒ Object
Returns the value of attribute host.
11 12 13 |
# File 'lib/parse/client.rb', line 11 def host @host end |
#master_key ⇒ Object
Returns the value of attribute master_key.
14 15 16 |
# File 'lib/parse/client.rb', line 14 def master_key @master_key end |
#session ⇒ Object
Returns the value of attribute session.
16 17 18 |
# File 'lib/parse/client.rb', line 16 def session @session end |
#session_token ⇒ Object
Returns the value of attribute session_token.
15 16 17 |
# File 'lib/parse/client.rb', line 15 def session_token @session_token end |
Instance Method Details
#delete(uri) ⇒ Object
85 86 87 |
# File 'lib/parse/client.rb', line 85 def delete(uri) request(uri, :delete) end |
#get(uri) ⇒ Object
73 74 75 |
# File 'lib/parse/client.rb', line 73 def get(uri) request(uri) end |
#post(uri, body) ⇒ Object
77 78 79 |
# File 'lib/parse/client.rb', line 77 def post(uri, body) request(uri, :post, body) end |
#put(uri, body) ⇒ Object
81 82 83 |
# File 'lib/parse/client.rb', line 81 def put(uri, body) request(uri, :put, body) end |
#request(uri, method = :get, body = nil, query = nil, max_retries = 2) ⇒ 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.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/parse/client.rb', line 38 def request(uri, method = :get, body = nil, query = nil, max_retries = 2) @session.headers[Protocol::HEADER_MASTER_KEY] = @master_key @session.headers[Protocol::HEADER_API_KEY] = @api_key @session.headers[Protocol::HEADER_APP_ID] = @application_id @session.headers[Protocol::HEADER_SESSION_TOKEN] = @session_token = {} if body [:data] = body end if query [:query] = query end num_tries = 0 begin response = @session.request(method, uri, {}, ) rescue Patron::TimeoutError num_tries += 1 if num_tries <= max_retries retry else raise Patron::TimeoutError end end if response.status >= 400 raise ParseError, "#{JSON.parse(response.body)['code']}: #{JSON.parse(response.body)['error']}" else if response return JSON.parse response.body end end end |