class LC::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]
quiet[RW]
session[RW]
session_token[RW]

Public Class Methods

new(data = {}) { |c| ... } click to toggle source
# File lib/leancloud/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}
  @quiet          = data[:quiet] || false

  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', 'LC::LCProtocolRetry']
    c.use Faraday::ExtendedParseJson

    c.response :logger, @logger unless @quiet
    c.adapter Faraday.default_adapter

    yield(c) if block_given?
  end
end

Public Instance Methods

_request(uri: "", method: :get, body: nil, query: nil, content_type: nil, session_token: nil) click to toggle source
# File lib/leancloud/client.rb, line 81
def _request(uri: "", method: :get, body: nil, query: nil, content_type: nil, session_token: nil)
  raise ArgumentError, "wrong number of arguments (given 0, expected 1..6)" if !(uri && uri != "")
  headers = {}

  {
    "Content-Type"                  => content_type || 'application/json',
    "User-Agent"                    => 'leancloud-ruby-client, 0.0',
    Protocol::HEADER_APP_ID         => @application_id,
    Protocol::HEADER_API_KEY        => get_sign,
    Protocol::HEADER_SESSION_TOKEN  => session_token || @session_token,
  }.each do |key, value|
    headers[key] = value if value
  end

  @session.send(method, uri, query || body || {}, headers).body
end
delete(uri) click to toggle source
# File lib/leancloud/client.rb, line 110
def delete(uri)
  request(uri, :delete)
end
get(uri) click to toggle source
# File lib/leancloud/client.rb, line 98
def get(uri)
  request(uri)
end
get_sign(time = Time.now) click to toggle source
# File lib/leancloud/client.rb, line 51
def get_sign(time = Time.now)
  m = !!@master_key
  if m
    m = ",master"
  else
    m = ""
  end
  k = @master_key || @api_key
  t = (time.to_f * 1000).to_i.to_s
  md5 = Digest::MD5.hexdigest t+k
  sign = "#{md5},#{t}#{m}"
end
post(uri, body) click to toggle source
# File lib/leancloud/client.rb, line 102
def post(uri, body)
  request(uri, :post, body)
end
put(uri, body) click to toggle source
# File lib/leancloud/client.rb, line 106
def put(uri, body)
  request(uri, :put, body)
end
request(uri, method = :get, body = nil, query = nil, content_type = nil, session_token = nil) click to toggle source

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

# File lib/leancloud/client.rb, line 67
def request(uri, method = :get, body = nil, query = nil, content_type = nil, session_token = nil)
  headers = {}
  {
    "Content-Type"                  => content_type || 'application/json',
    "User-Agent"                    => 'leancloud-ruby-client, 0.0',
    Protocol::HEADER_APP_ID         => @application_id,
    Protocol::HEADER_API_KEY        => get_sign,
    Protocol::HEADER_SESSION_TOKEN  => session_token || @session_token,
  }.each do |key, value|
    headers[key] = value if value
  end

  @session.send(method, uri, query || body || {}, headers).body
end