Class: LC::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', 'LC::LCProtocolRetry']
    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

#_request(uri: "", method: :get, body: nil, query: nil, content_type: nil, session_token: nil) ⇒ Object

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/leancloud/client.rb', line 82

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) ⇒ Object



111
112
113
# File 'lib/leancloud/client.rb', line 111

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

#get(uri) ⇒ Object



99
100
101
# File 'lib/leancloud/client.rb', line 99

def get(uri)
  request(uri)
end

#get_sign(time = Time.now) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/leancloud/client.rb', line 52

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) ⇒ Object



103
104
105
# File 'lib/leancloud/client.rb', line 103

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

#put(uri, body) ⇒ Object



107
108
109
# File 'lib/leancloud/client.rb', line 107

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

#request(uri, method = :get, body = nil, query = nil, content_type = nil, session_token = nil) ⇒ Object

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.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/leancloud/client.rb', line 68

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