Class: Reggora::Requests

Inherits:
Object
  • Object
show all
Defined in:
lib/reggora/Adapters/requests.rb

Instance Method Summary collapse

Constructor Details

#initialize(auth_token, integration_token, type) ⇒ Requests

Returns a new instance of Requests.



4
5
6
7
8
9
# File 'lib/reggora/Adapters/requests.rb', line 4

def initialize(auth_token, integration_token, type)
  @uri = URI("#{$base_api_uri}#{type}")
  @http = Net::HTTP.new(@uri.host, @uri.port)
  @http.use_ssl = true
  @header = {"Content-Type" => 'application/json', "Authorization" => "Bearer #{auth_token}", "integration" => integration_token}
end

Instance Method Details

#delete(url, params = {}) ⇒ Object



58
59
60
61
62
63
# File 'lib/reggora/Adapters/requests.rb', line 58

def delete(url, params = {})
  api_endpoint = full_uri url
  request = Net::HTTP::Delete.new(api_endpoint, @header)
  request.body = params.to_json
  send_request request
end

#full_uri(path) ⇒ Object



11
12
13
# File 'lib/reggora/Adapters/requests.rb', line 11

def full_uri(path)
  URI("#{@uri.to_s}#{path}")
end

#get(url, params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/reggora/Adapters/requests.rb', line 15

def get(url, params = {})
  api_endpoint = full_uri url
  api_endpoint.query = URI.encode_www_form(params) unless params.empty?
  send_request Net::HTTP::Get.new(api_endpoint.request_uri, @header)
end

#handle_response(response) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/reggora/Adapters/requests.rb', line 74

def handle_response(response)
  print "\n------ Response -----\n"
  puts response.read_body
  print "---------------------------"
  case response
  when Net::HTTPSuccess then
    json_parse(response.read_body)
  when Net::HTTPBadRequest then
    res = json_parse(response.read_body)
    raise res.inspect if res["error"].nil?
    res
  when Net::HTTPUnauthorized then
    raise "Unauthorized."
  when Net::HTTPInternalServerError then
    raise "Internal server error"
  else
    raise "Unknown error #{response}: #{response.inspect}"
  end
end

#json_parse(res) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/reggora/Adapters/requests.rb', line 94

def json_parse(res)
  begin
    JSON.parse(res)
  rescue JSON::ParserError
    res
  end
end

#post(url, params = {}, query_params = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/reggora/Adapters/requests.rb', line 21

def post(url, params = {}, query_params = {})
  api_endpoint = full_uri url
  api_endpoint.query = URI.encode_www_form(query_params) unless query_params.empty?
  request = Net::HTTP::Post.new(api_endpoint, @header)
  request.body = params.to_json
  send_request request
end

#post_file(url, params) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/reggora/Adapters/requests.rb', line 29

def post_file(url, params)
  api_endpoint = full_uri url
  header = @header.dup
  boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"
  header["Content-Type"] = "multipart/form-data; boundary=#{boundary}"

  request = Net::HTTP::Post.new(api_endpoint, header)
  request.body = ""
  params.each do |k, v|
    if k.to_s == 'file'
      mime_type = MIME::Types.type_for(v)
      request.body += "--#{boundary}\r\nContent-Disposition: form-data; name=\"#{k.to_s}\"; filename=\"#{v}\"\r\nContent-Type: #{mime_type[0]}\r\n"
    else
      request.body += "--#{boundary}\r\nContent-Disposition: form-data; name=\"#{k.to_s}\"\r\n\r\n#{v}\r\n"
    end
  end

  request.body += "\r\n\r\n--#{boundary}--"
  send_request request

end

#put(url, params = {}) ⇒ Object



51
52
53
54
55
56
# File 'lib/reggora/Adapters/requests.rb', line 51

def put(url, params = {})
  api_endpoint = full_uri url
  request = Net::HTTP::Put.new(api_endpoint, @header)
  request.body = params.to_json
  send_request request
end

#send_request(request) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/reggora/Adapters/requests.rb', line 65

def send_request(request)
  begin
    handle_response @http.request(request)
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPUnauthorized,
      Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, Errno::ECONNREFUSED => e
    raise e.inspect
  end
end