forked from piotrmurach/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.rb
More file actions
59 lines (45 loc) · 1.58 KB
/
request.rb
File metadata and controls
59 lines (45 loc) · 1.58 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
# encoding: utf-8
module Github
# Defines HTTP verbs
module Request
METHODS = [:get, :post, :put, :delete, :patch]
METHODS_WITH_BODIES = [:post, :put, :patch]
def get_request(path, params = ParamsHash.empty)
request(:get, path, params).auto_paginate
end
def patch_request(path, params = ParamsHash.empty)
request(:patch, path, params)
end
def post_request(path, params = ParamshHash.empty)
request(:post, path, params)
end
def put_request(path, params = ParamsHash.empty)
request(:put, path, params)
end
def delete_request(path, params = ParamsHash.empty)
request(:delete, path, params)
end
def request(method, path, params) # :nodoc:
unless METHODS.include?(method)
raise ArgumentError, "unkown http method: #{method}"
end
puts "EXECUTED: #{method} - #{path} with PARAMS: #{params}" if ENV['DEBUG']
conn_options = params.options.merge(current_options)
conn = connection(conn_options)
if conn.path_prefix != '/' && path.index(conn.path_prefix) != 0
path = (conn.path_prefix + path).gsub(/\/(\/)*/, '/')
end
response = conn.send(method) do |request|
case method.to_sym
when *(METHODS - METHODS_WITH_BODIES)
request.body = params.data if params.has_key?('data')
request.url(path, params.to_hash)
when *METHODS_WITH_BODIES
request.path = path
request.body = params.data unless params.empty?
end
end
ResponseWrapper.new(response, self)
end
end # Request
end # Github