Class: RailLocatorApi::APIRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/rail-locator-api/api_request.rb

Instance Method Summary collapse

Constructor Details

#initialize(builder: nil) ⇒ APIRequest

Returns a new instance of APIRequest.



4
5
6
# File 'lib/rail-locator-api/api_request.rb', line 4

def initialize(builder: nil)
  @request_builder = builder
end

Instance Method Details

#delete(params: nil, headers: nil, format: nil, body: {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rail-locator-api/api_request.rb', line 84

def delete(params: nil, headers: nil, format: nil, body: {})
  validate_api_key
  begin
    response = self.rest_client(format).delete do |request|
      configure_request(request: request, params: params, headers: headers, body: body)
    end
    if [nil, 'json'].include?(format)
      parse_response(response)
    else
      response
    end
  rescue => e
    handle_error(e)
  end
end

#get(params: nil, headers: nil, format: nil, body: {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rail-locator-api/api_request.rb', line 67

def get(params: nil, headers: nil, format: nil, body: {})
  validate_api_key

  begin
    response = self.rest_client(format).get do |request|
      configure_request(request: request, params: params, headers: headers, body: body)
    end
    if [nil, 'json'].include?(format)
      parse_response(response)
    else
      response
    end
  rescue => e
    handle_error(e)
  end
end

#patch(params: nil, headers: nil, format: nil, body: {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rail-locator-api/api_request.rb', line 100

def patch(params: nil, headers: nil, format: nil, body: {})
  validate_api_key
  begin
    response = self.rest_client(format).patch do |request|
      configure_request(request: request, params: params, headers: headers, body: body)
    end
    if [nil, 'json'].include?(format)
      parse_response(response)
    else
      response
    end
  rescue => e
    handle_error(e)
  end
end

#post(params: nil, headers: nil, format: nil, body: {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rail-locator-api/api_request.rb', line 8

def post(params: nil, headers: nil, format: nil, body: {})
  validate_api_key
  begin
    headers ||= {}
    if is_multipart?(body)
      if self.api_auth_method == :base64
        headers['Authorization'] =  "Basic " + Base64::encode64("#{self.api_user_email}:#{self.api_user_password}")
      end
      if [:keycloak].include?(self.api_auth_method)
        unless RailLocatorApi::Request.token_alive?(RailLocatorApi::Request.access_token)
          if RailLocatorApi::Request.token_alive?(RailLocatorApi::Request.refresh_token)
            response = RailLocatorApi.refresh_access_token
            RailLocatorApi::Request.access_token = response.try(:dig, "access_token")
            RailLocatorApi::Request.refresh_token = response.try(:dig, "refresh_token")
          else
            error_params = { title: "UNAUTHORIZED", status_code: 401 }
            error = RailLocatorApiError.new("Token is unavailable", error_params)
            raise error
          end
        end
        headers['Authorization'] = "Bearer #{RailLocatorApi::Request.access_token}"
      end
      if [:api_key].include?(self.api_auth_method)
        request.headers['X-Api-Key'] = "#{RailLocatorApi::Request.api_key}"
        if self.api_user_id.present?
          request.headers['X-User-Id'] = self.api_user_id.to_s
        end
      end
      headers['User-Agent'] = "RailLocatorApi/#{RailLocatorApi::VERSION} Ruby gem"
      headers['Accept-Language'] = RailLocatorApi::Request.language.to_s
      if @request_builder.without_ratelimit
        headers['X-Is-Request-Without-RateLimit'] = "true"
      end
      if @request_builder.is_allow_access_to_coordinates
        headers['X-Is-Allow-Access-To-Coordinates'] = "true"
      end
      url = URI("#{self.api_url}#{format.present? ? ".#{format}": ""}")
      http = Net::HTTP.new(url.host, url.port);
      request = Net::HTTP::Post.new(url, headers)
      request.set_form body.to_params, 'multipart/form-data'
      if @request_builder.debug
        p request
      end
      http.request(request)
    else
      response = self.rest_client(format).post do |request|
        configure_request(request: request, params: params, headers: headers, body: body)
      end
      if [nil, 'json'].include?(format)
        parse_response(response)
      else
        response
      end
    end
  rescue => e
    handle_error(e)
  end
end

#put(params: nil, headers: nil, format: nil, body: {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rail-locator-api/api_request.rb', line 116

def put(params: nil, headers: nil, format: nil, body: {})
  validate_api_key
  begin
    response = self.rest_client(format).put do |request|
      configure_request(request: request, params: params, headers: headers, body: body)
    end
    if [nil, 'json'].include?(format)
      parse_response(response)
    else
      response
    end
  rescue => e
    handle_error(e)
  end
end