-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi-caller.rb
47 lines (36 loc) · 1.14 KB
/
api-caller.rb
1
2
3
4
5
6
7
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
class ApiCaller
require 'net/http'
require 'uri'
require 'json'
def initialize args
args = { ssl: true, headers: {}, post_params: {} }.merge args
@http = Net::HTTP.new(args[:uri].host, args[:uri].port)
@http.use_ssl = args[:ssl]
@default_headers = args[:headers]
@default_post_params = args[:post_params]
@base_path = args[:base_path]
end
def get path, headers = {}
request = Net::HTTP:Get.new [@base_path, path].compact.join("/")
headers = @default_headers.merge headers
headers.each do |header_key, header_value|
request.add_field header_key, header_value
end
make_request request
end
def post path, parameters = {}, headers = {}
request = Net::HTTP::Post.new [@base_path, path].compact.join('/')
headers = @default_headers.merge headers
parameters = @default_post_params.merge parameters
headers.each do |header_key, header_value|
request.add_field header_key, header_value
end
#request.body = parameters.to_json
request.set_form_data parameters
make_request request
end
private
def make_request request
@http.request request
end
end