Module: GithubPivotalFlow::GitHubAPI::HttpMethods
- Included in:
- GithubPivotalFlow::GitHubAPI
- Defined in:
- lib/github_pivotal_flow/github_api.rb
Defined Under Namespace
Modules: ResponseMethods
Instance Method Summary collapse
- #apply_authentication(req, url) ⇒ Object
- #byte_size(str) ⇒ Object
- #configure_connection(req, url) {|url| ... } ⇒ Object
- #create_connection(url) ⇒ Object
- #finalize_request(req, url) ⇒ Object
- #get(url, &block) ⇒ Object
- #perform_request(url, type) {|req| ... } ⇒ Object
- #post(url, params = nil) ⇒ Object
- #post_form(url, params) ⇒ Object
- #request_uri(url) ⇒ Object
Instance Method Details
#apply_authentication(req, url) ⇒ Object
158 159 160 161 162 |
# File 'lib/github_pivotal_flow/github_api.rb', line 158 def apply_authentication req, url user = url.user ? CGI.unescape(url.user) : config.github_username(url.host) pass = config.github_password(url.host, user) req.basic_auth user, pass end |
#byte_size(str) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/github_pivotal_flow/github_api.rb', line 106 def byte_size str if str.respond_to? :bytesize then str.bytesize elsif str.respond_to? :length then str.length else 0 end end |
#configure_connection(req, url) {|url| ... } ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/github_pivotal_flow/github_api.rb', line 147 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
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/github_pivotal_flow/github_api.rb', line 170 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 # 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 |
#finalize_request(req, url) ⇒ Object
164 165 166 167 168 |
# File 'lib/github_pivotal_flow/github_api.rb', line 164 def finalize_request(req, url) if !req['Accept'] || req['Accept'] == '*/*' req['Accept'] = 'application/vnd.github.v3+json' end end |
#get(url, &block) ⇒ Object
91 92 93 |
# File 'lib/github_pivotal_flow/github_api.rb', line 91 def get url, &block perform_request url, :Get, &block end |
#perform_request(url, type) {|req| ... } ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/github_pivotal_flow/github_api.rb', line 117 def perform_request url, type url = URI.parse url unless url.respond_to? :host require 'net/https' req = Net::HTTP.const_get(type).new request_uri(url) # TODO: better naming? http = configure_connection(req, url) do |host_url| create_connection host_url end req['User-Agent'] = "Hub 1.11.1" apply_authentication(req, url) yield req if block_given? finalize_request(req, url) begin res = http.start { http.request(req) } res.extend ResponseMethods return res rescue SocketError => err raise Context::FatalError, "error with #{type.to_s.upcase} #{url} (#{err.message})" end end |
#post(url, params = nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/github_pivotal_flow/github_api.rb', line 95 def post url, params = nil perform_request url, :Post do |req| if params req.body = JSON.dump params req['Content-Type'] = 'application/json;charset=utf-8' end yield req if block_given? req['Content-Length'] = byte_size req.body end end |
#post_form(url, params) ⇒ Object
113 114 115 |
# File 'lib/github_pivotal_flow/github_api.rb', line 113 def post_form url, params post(url) {|req| req.set_form_data params } end |
#request_uri(url) ⇒ Object
141 142 143 144 145 |
# File 'lib/github_pivotal_flow/github_api.rb', line 141 def request_uri url str = url.request_uri str = '/api/v3' << str if url.host != 'api.github.com' && url.host != 'gist.github.com' str end |