Class: UnofficialBuildkiteClient::HttpClient
- Inherits:
-
Object
- Object
- UnofficialBuildkiteClient::HttpClient
- Defined in:
- lib/unofficial_buildkite_client/http_client.rb
Instance Method Summary collapse
-
#initialize(authorization_header: nil, logger:) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #request(method, url, params: nil, json: true, auth: true) ⇒ Object
Constructor Details
#initialize(authorization_header: nil, logger:) ⇒ HttpClient
Returns a new instance of HttpClient.
7 8 9 10 |
# File 'lib/unofficial_buildkite_client/http_client.rb', line 7 def initialize(authorization_header: nil, logger:) @authorization_header = @logger = logger end |
Instance Method Details
#request(method, url, params: nil, json: true, auth: true) ⇒ Object
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 |
# File 'lib/unofficial_buildkite_client/http_client.rb', line 12 def request(method, url, params: nil, json: true, auth: true) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true logger.info("method: #{method} url: #{url} params: #{params.inspect}") request = case method when :get uri.query = URI.encode_www_form(params) if params Net::HTTP::Get.new(uri.request_uri) when :post Net::HTTP::Post.new(uri.request_uri).tap do |req| req.body = params.to_json if params end else raise NotImplementedError end request["Content-Type"] = request["Accept"] = "application/json" if json request["Authorization"] = @authorization_header if auth response = http.request(request) case response when Net::HTTPSuccess if json JSON.parse(response.body, symbolize_names: true) else response.body end when Net::HTTPRedirection request(:get, response["location"], json: json, auth: false) else response.error! end end |