Class: ZQuickblox::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/zquickblox/request.rb

Constant Summary collapse

API_ENDPOINT =
"https://api.quickblox.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Returns a new instance of Request.



9
10
11
12
13
14
15
16
17
# File 'lib/zquickblox/request.rb', line 9

def initialize
  @connection = Faraday.new(url: Request::API_ENDPOINT) do |faraday|
    faraday.request  :url_encoded
    faraday.response :logger
    faraday.adapter  Faraday.default_adapter
  end

  @headers = {}
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/zquickblox/request.rb', line 6

def connection
  @connection
end

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'lib/zquickblox/request.rb', line 5

def errors
  @errors
end

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/zquickblox/request.rb', line 5

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



5
6
7
# File 'lib/zquickblox/request.rb', line 5

def method
  @method
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/zquickblox/request.rb', line 5

def params
  @params
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/zquickblox/request.rb', line 5

def response
  @response
end

#response_bodyObject

Returns the value of attribute response_body.



5
6
7
# File 'lib/zquickblox/request.rb', line 5

def response_body
  @response_body
end

#uriObject (readonly)

Returns the value of attribute uri.



7
8
9
# File 'lib/zquickblox/request.rb', line 7

def uri
  @uri
end

Instance Method Details

#after_requestObject



26
27
# File 'lib/zquickblox/request.rb', line 26

def after_request
end

#before_requestObject



23
24
# File 'lib/zquickblox/request.rb', line 23

def before_request
end

#executeObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zquickblox/request.rb', line 29

def execute
  before_request

  get    if @method == :get
  post   if @method == :post
  put    if @method == :put
  delete if @method == :delete

  if @response.status != 404 && @response.body.length > 1
    @response_body = JSON.parse(@response.body)
  else
    @response_body = {}
  end

  @errors = @response_body["errors"] if @response.status != 404
  if @errors
    if @errors.kind_of?(Hash)
      raise ZQuickblox::Error.new(messages: @errors["base"]) if !@errors["base"].nil?
      message = ""
      @errors.each do |key, value|
        message += "; " + key
        message += " " + value.join(", ")
      end
      raise ZQuickblox::Error.new(messages: message)
    end
  end

  after_request
end

#header(key, value) ⇒ Object



19
20
21
# File 'lib/zquickblox/request.rb', line 19

def header(key, value)
  headers[key] = value
end