Module: Hub::GitHubAPI::HttpMethods
- Included in:
- Hub::GitHubAPI
- Defined in:
- lib/hub/github_api.rb
Overview
Methods for performing HTTP requests
Requires access to a ‘config` object that implements `proxy_uri(with_ssl)`
Defined Under Namespace
Modules: ResponseMethods
Instance Method Summary collapse
- #apply_authentication(req, url) ⇒ Object
- #configure_connection(req, url) {|url| ... } ⇒ Object
- #create_connection(url) ⇒ Object
- #get(url, &block) ⇒ Object
- #perform_request(url, type) ⇒ Object
- #post(url, params = nil) ⇒ Object
- #post_form(url, params) ⇒ Object
Instance Method Details
#apply_authentication(req, url) ⇒ Object
183 184 185 186 187 |
# File 'lib/hub/github_api.rb', line 183 def apply_authentication req, url user = url.user || config.username(url.host) pass = config.password(url.host, user) req.basic_auth user, pass end |
#configure_connection(req, url) {|url| ... } ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/hub/github_api.rb', line 172 def configure_connection req, url if ENV['HUB_TEST_HOST'] req['Host'] = url.host url = url.dup url.scheme = 'http' url.host, test_port = ENV['HUB_TEST_HOST'].split(':') url.port = test_port.to_i if test_port end yield url end |
#create_connection(url) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/hub/github_api.rb', line 189 def create_connection url use_ssl = 'https' == url.scheme proxy_args = [] if proxy = config.proxy_uri(use_ssl) proxy_args << proxy.host << proxy.port if proxy.userinfo require 'cgi' # proxy user + password proxy_args.concat proxy.userinfo.split(':', 2).map {|a| CGI.unescape a } end end http = Net::HTTP.new(url.host, url.port, *proxy_args) if http.use_ssl = use_ssl # FIXME: enable SSL peer verification! http.verify_mode = OpenSSL::SSL::VERIFY_NONE end return http end |
#get(url, &block) ⇒ Object
134 135 136 |
# File 'lib/hub/github_api.rb', line 134 def get url, &block perform_request url, :Get, &block end |
#perform_request(url, type) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/hub/github_api.rb', line 153 def perform_request url, type url = URI.parse url unless url.respond_to? :host require 'net/https' req = Net::HTTP.const_get(type).new(url.request_uri) # TODO: better naming? http = configure_connection(req, url) do |host_url| create_connection host_url end apply_authentication(req, url) yield req if block_given? res = http.start { http.request(req) } res.extend ResponseMethods res rescue SocketError => err raise Context::FatalError, "error with #{type.to_s.upcase} #{url} (#{err.})" end |
#post(url, params = nil) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/hub/github_api.rb', line 138 def post url, params = nil perform_request url, :Post do |req| if params req.body = JSON.dump params req['Content-Type'] = 'application/json' end yield req if block_given? req['Content-Length'] = req.body ? req.body.length : 0 end end |
#post_form(url, params) ⇒ Object
149 150 151 |
# File 'lib/hub/github_api.rb', line 149 def post_form url, params post(url) {|req| req.set_form_data params } end |