Class: Crowdin::Web::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/crowdin-api/core/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, method, path, query = {}, headers = {}, destination = nil) ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
13
14
15
16
# File 'lib/crowdin-api/core/request.rb', line 8

def initialize(client, method, path, query = {}, headers = {}, destination = nil)
  @client      = client
  @method      = method
  @full_path   = client.config.target_api_url + path
  @payload     = Payload.new(method, query).perform
  @headers     = headers
  @destination = destination
  @errors      = []
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/crowdin-api/core/request.rb', line 6

def client
  @client
end

Instance Method Details

#performObject



18
19
20
21
# File 'lib/crowdin-api/core/request.rb', line 18

def perform
  process_request!
  process_response!
end

#process_request!Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/crowdin-api/core/request.rb', line 23

def process_request!
  return @response = client.connection[@full_path].delete        if delete_request?
  return @response = client.connection[@full_path].get(@payload) if get_request?

  client.connection[@full_path].send(@method, @payload, @headers) { |response, _, _| @response = response }
rescue StandardError => error
  client.log! error.class

  @errors << "Something went wrong while proccessing request. Details - #{error.class}"
end

#process_response!Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/crowdin-api/core/request.rb', line 34

def process_response!
  return fetch_errors if @errors.any?

  begin
    if @response
      doc = JSON.parse(@response.body)

      client.log! "args: #{@response.request.args}"
      client.log! "body: #{doc}"

      data = fetch_response_data(doc)

      @errors.any? ? fetch_errors : data
    end
  rescue StandardError => error
    client.log! error

    @errors << "Something went wrong while proccessing response. Details - #{error.class}"

    fetch_errors
  end
end