class Parse::Client

A class which encapsulates the HTTPS communication with the Parse API server.

Attributes

api_key[RW]
application_id[RW]
host[RW]
logger[RW]
master_key[RW]
max_retries[RW]
session[RW]
session_token[RW]

Public Class Methods

new(data = {}) { |c| ... } click to toggle source
# File lib/parse/client.rb, line 20
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}

  options = {:request => {:timeout => 30, :open_timeout => 30}}

  @session = Faraday.new("https://#{host}", options) 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

Public Instance Methods

delete(uri) click to toggle source
# File lib/parse/client.rb, line 83
def delete(uri)
  request(uri, :delete)
end
get(uri) click to toggle source
# File lib/parse/client.rb, line 71
def get(uri)
  request(uri)
end
post(uri, body) click to toggle source
# File lib/parse/client.rb, line 75
def post(uri, body)
  request(uri, :post, body)
end
put(uri, body) click to toggle source
# File lib/parse/client.rb, line 79
def put(uri, body)
  request(uri, :put, body)
end
request(uri, method = :get, body = nil, query = nil, content_type = nil) click to toggle source

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.

# File lib/parse/client.rb, line 54
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