Class: AV::Client

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, &blk) ⇒ Client

Returns a new instance of Client.



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
50
51
# File 'lib/leancloud/client.rb', line 22

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

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

    yield(c) if block_given?
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#application_idObject

Returns the value of attribute application_id.



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

def application_id
  @application_id
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



19
20
21
# File 'lib/leancloud/client.rb', line 19

def logger
  @logger
end

#master_keyObject

Returns the value of attribute master_key.



15
16
17
# File 'lib/leancloud/client.rb', line 15

def master_key
  @master_key
end

#max_retriesObject

Returns the value of attribute max_retries.



18
19
20
# File 'lib/leancloud/client.rb', line 18

def max_retries
  @max_retries
end

#quietObject

Returns the value of attribute quiet.



20
21
22
# File 'lib/leancloud/client.rb', line 20

def quiet
  @quiet
end

#sessionObject

Returns the value of attribute session.



17
18
19
# File 'lib/leancloud/client.rb', line 17

def session
  @session
end

#session_tokenObject

Returns the value of attribute session_token.



16
17
18
# File 'lib/leancloud/client.rb', line 16

def session_token
  @session_token
end

Instance Method Details

#delete(uri) ⇒ Object



86
87
88
# File 'lib/leancloud/client.rb', line 86

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

#get(uri) ⇒ Object



74
75
76
# File 'lib/leancloud/client.rb', line 74

def get(uri)
  request(uri)
end

#post(uri, body) ⇒ Object



78
79
80
# File 'lib/leancloud/client.rb', line 78

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

#put(uri, body) ⇒ Object



82
83
84
# File 'lib/leancloud/client.rb', line 82

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 AVProtocolError if the response has an error status code, and will return the parsed JSON body on success, if there is one.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/leancloud/client.rb', line 57

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