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
85 lines (72 loc) · 2.35 KB
/
request.rb
File metadata and controls
85 lines (72 loc) · 2.35 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# encoding: utf-8
require_relative 'connection'
require_relative 'response_wrapper'
require_relative 'request/oauth2'
require_relative 'request/basic_auth'
require_relative 'request/jsonize'
module Github
# A class responsible for dispatching http requests
class Request
include Connection
HTTP_METHODS = [:get, :head, :post, :put, :delete, :patch]
METHODS_WITH_BODIES = [:post, :put, :patch]
# Return http verb
#
# @return [Symbol]
attr_reader :action
# Return url
#
# @return [String]
attr_accessor :path
# Return api this request is associated with
#
# @return [Github::API]
attr_reader :api
# Create a new Request
#
# @return [Github::Request]
#
# @api public
def initialize(action, path, api)
@action = action
@path = path
@api = api
end
# Performs a request
#
# @param [Symbol] method - The Symbol the HTTP verb
# @param [String] path - String relative URL to access
# @param [ParamsHash] params - ParamsHash to configure the request API
#
# @return [Github::ResponseWrapper]
#
# @api private
def call(current_options, params)
unless HTTP_METHODS.include?(action)
raise ArgumentError, "unknown http method: #{method}"
end
puts "EXECUTED: #{action} - #{path} with PARAMS: #{params.request_params}" if ENV['DEBUG']
request_options = params.options
connection_options = current_options.merge(request_options)
conn = connection(api, connection_options)
self.path = Utils::Url.normalize(self.path)
if conn.path_prefix != '/' && self.path.index(conn.path_prefix) != 0
self.path = (conn.path_prefix + self.path).gsub(/\/(\/)*/, '/')
end
response = conn.send(action) do |request|
case action.to_sym
when *(HTTP_METHODS - METHODS_WITH_BODIES)
request.body = params.data if params.key?('data')
if params.key?('encoder')
request.params.params_encoder(params.encoder)
end
request.url(self.path, params.request_params)
when *METHODS_WITH_BODIES
request.url(self.path, connection_options[:query] || {})
request.body = params.data unless params.empty?
end
end
ResponseWrapper.new(response, api)
end
end # Request
end # Github