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.
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.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#master_key ⇒ Object
Returns the value of attribute master_key.
-
#max_retries ⇒ Object
Returns the value of attribute max_retries.
-
#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 = {}, &blk) ⇒ Client
constructor
A new instance of Client.
- #post(uri, body) ⇒ Object
- #put(uri, body) ⇒ Object
-
#request(uri, method = :get, body = nil, query = nil, content_type = nil) ⇒ Object
Perform an HTTP request for the given uri and method with common basic response handling.
Constructor Details
#initialize(data = {}, &blk) ⇒ Client
Returns a new instance of Client.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/parse/client.rb', line 21 def initialize(data = {}, &blk) @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] @max_retries = data[:max_retries] || 3 @logger = data[:logger] || Logger.new(STDERR).tap{|l| l.level = Logger::INFO} = {:request => {:timeout => 30, :open_timeout => 30}} @session = Faraday.new("https://#{host}", ) do |c| c.request :json c.use Faraday::GetMethodOverride c.use Faraday::BetterRetry, max: @max_retries, logger: @logger, interval: 0.5, exceptions: ['Faraday::Error::TimeoutError', 'Faraday::Error::ParsingError', 'Parse::ParseProtocolRetry'] c.use Faraday::ExtendedParseJson c.response :logger, @logger c.adapter Faraday.default_adapter yield(c) if block_given? end end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/parse/client.rb', line 14 def api_key @api_key end |
#application_id ⇒ Object
Returns the value of attribute application_id.
13 14 15 |
# File 'lib/parse/client.rb', line 13 def application_id @application_id end |
#host ⇒ Object
Returns the value of attribute host.
12 13 14 |
# File 'lib/parse/client.rb', line 12 def host @host end |
#logger ⇒ Object
Returns the value of attribute logger.
19 20 21 |
# File 'lib/parse/client.rb', line 19 def logger @logger end |
#master_key ⇒ Object
Returns the value of attribute master_key.
15 16 17 |
# File 'lib/parse/client.rb', line 15 def master_key @master_key end |
#max_retries ⇒ Object
Returns the value of attribute max_retries.
18 19 20 |
# File 'lib/parse/client.rb', line 18 def max_retries @max_retries end |
#session ⇒ Object
Returns the value of attribute session.
17 18 19 |
# File 'lib/parse/client.rb', line 17 def session @session end |
#session_token ⇒ Object
Returns the value of attribute session_token.
16 17 18 |
# File 'lib/parse/client.rb', line 16 def session_token @session_token end |
Instance Method Details
#delete(uri) ⇒ Object
84 85 86 |
# File 'lib/parse/client.rb', line 84 def delete(uri) request(uri, :delete) end |
#get(uri) ⇒ Object
72 73 74 |
# File 'lib/parse/client.rb', line 72 def get(uri) request(uri) end |
#post(uri, body) ⇒ Object
76 77 78 |
# File 'lib/parse/client.rb', line 76 def post(uri, body) request(uri, :post, body) end |
#put(uri, body) ⇒ Object
80 81 82 |
# File 'lib/parse/client.rb', line 80 def put(uri, body) request(uri, :put, body) end |
#request(uri, method = :get, body = nil, query = nil, content_type = 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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/parse/client.rb', line 55 def request(uri, method = :get, body = nil, query = nil, content_type = nil) headers = {} { "Content-Type" => content_type || 'application/json', "User-Agent" => 'Parse for Ruby, 0.0', Protocol::HEADER_MASTER_KEY => @master_key, Protocol::HEADER_APP_ID => @application_id, Protocol::HEADER_API_KEY => @api_key, Protocol::HEADER_SESSION_TOKEN => @session_token, }.each do |key, value| headers[key] = value if value end @session.send(method, uri, query || body || {}, headers).body end |