Class: Parse::Client

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

Overview

The client that communicates with the Parse server via REST

Constant Summary collapse

RETRIED_EXCEPTIONS =
[
  'Faraday::Error::TimeoutError',
  'Faraday::Error::ParsingError',
  'Faraday::Error::ConnectionFailed',
  'Parse::ParseProtocolRetry'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/parse/client.rb', line 35

def initialize(data = {}, &_blk)
  @host           = data[:host]
  @path           = data[:path] || Protocol::PATH

  @application_id = data[:application_id]
  @master_key     = data[:master_key]

  @api_key        = data[:api_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
  @timeout        = data[:timeout] || 30

  # Additional parameters for Faraday Request::Retry
  @interval       = data[:interval] || 0.5
  @backoff_factor = data[:backoff_factor] || 2

  @retried_exceptions = RETRIED_EXCEPTIONS
  @retried_exceptions += data[:retried_exceptions] if data[
    :retried_exceptions]

  @get_method_override = data[:get_method_override]

  options = { request: { timeout: @timeout, open_timeout: @timeout } }

  @session = Faraday.new(host, options) do |c|
    c.request :json

    c.use Faraday::GetMethodOverride if @get_method_override

    c.use Faraday::BetterRetry,
          max: @max_retries,
          logger: @logger,
          interval: @interval,
          backoff_factor: @backoff_factor,
          exceptions: @retried_exceptions
    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.



22
23
24
# File 'lib/parse/client.rb', line 22

def api_key
  @api_key
end

#application_idObject

Returns the value of attribute application_id.



21
22
23
# File 'lib/parse/client.rb', line 21

def application_id
  @application_id
end

#backoff_factorObject

Returns the value of attribute backoff_factor.



31
32
33
# File 'lib/parse/client.rb', line 31

def backoff_factor
  @backoff_factor
end

#get_method_overrideObject (readonly)

Returns the value of attribute get_method_override.



33
34
35
# File 'lib/parse/client.rb', line 33

def get_method_override
  @get_method_override
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#intervalObject

Returns the value of attribute interval.



30
31
32
# File 'lib/parse/client.rb', line 30

def interval
  @interval
end

#loggerObject

Returns the value of attribute logger.



27
28
29
# File 'lib/parse/client.rb', line 27

def logger
  @logger
end

#master_keyObject

Returns the value of attribute master_key.



23
24
25
# File 'lib/parse/client.rb', line 23

def master_key
  @master_key
end

#max_retriesObject

Returns the value of attribute max_retries.



26
27
28
# File 'lib/parse/client.rb', line 26

def max_retries
  @max_retries
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#quietObject

Returns the value of attribute quiet.



28
29
30
# File 'lib/parse/client.rb', line 28

def quiet
  @quiet
end

#retried_exceptionsObject

Returns the value of attribute retried_exceptions.



32
33
34
# File 'lib/parse/client.rb', line 32

def retried_exceptions
  @retried_exceptions
end

#sessionObject

Returns the value of attribute session.



25
26
27
# File 'lib/parse/client.rb', line 25

def session
  @session
end

#session_tokenObject

Returns the value of attribute session_token.



24
25
26
# File 'lib/parse/client.rb', line 24

def session_token
  @session_token
end

#timeoutObject

Returns the value of attribute timeout.



29
30
31
# File 'lib/parse/client.rb', line 29

def timeout
  @timeout
end

Instance Method Details

#application_configObject



127
128
129
# File 'lib/parse/client.rb', line 127

def application_config
  Parse::Application.config(self)
end

#batchObject



131
132
133
# File 'lib/parse/client.rb', line 131

def batch
  Parse::Batch.new(self)
end

#cloud_function(function_name) ⇒ Object



135
136
137
# File 'lib/parse/client.rb', line 135

def cloud_function(function_name)
  Parse::Cloud::Function.new(function_name, self)
end

#delete(uri) ⇒ Object



123
124
125
# File 'lib/parse/client.rb', line 123

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

#file(data) ⇒ Object



139
140
141
# File 'lib/parse/client.rb', line 139

def file(data)
  Parse::File.new(data, self)
end

#get(uri) ⇒ Object



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

def get(uri)
  request(uri)
end

#installation(object_id = nil) ⇒ Object



151
152
153
# File 'lib/parse/client.rb', line 151

def installation(object_id = nil)
  Parse::Installation.new(object_id, self)
end

#object(class_name, data = nil) ⇒ Object



143
144
145
# File 'lib/parse/client.rb', line 143

def object(class_name, data = nil)
  Parse::Object.new(class_name, data, self)
end

#post(uri, body) ⇒ Object



115
116
117
# File 'lib/parse/client.rb', line 115

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

#push(data, channel = '') ⇒ Object



147
148
149
# File 'lib/parse/client.rb', line 147

def push(data, channel = '')
  Parse::Push.new(data, channel, self)
end

#put(uri, body) ⇒ Object



119
120
121
# File 'lib/parse/client.rb', line 119

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

#query(class_name) ⇒ Object



155
156
157
# File 'lib/parse/client.rb', line 155

def query(class_name)
  Parse::Query.new(class_name, self)
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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/parse/client.rb', line 87

def request(uri, method = :get, body = nil, query = nil, content_type = nil)
  headers = {}

  {
    'Content-Type'                  => content_type || 'application/json',
    'User-Agent'                    => "Parse for Ruby, #{VERSION}",
    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

  uri = ::File.join(path, uri)
  response = @session.send(method, uri, query || body || {}, headers)
  response.body

# NOTE: Don't leak our internal libraries to our clients.
# Extend this list of exceptions as needed.
rescue Faraday::Error::ClientError => e
  raise Parse::ConnectionError, e.message
end

#user(data) ⇒ Object



159
160
161
# File 'lib/parse/client.rb', line 159

def user(data)
  Parse::User.new(data, self)
end