class DangerBoss::Driver::Http

Attributes

raw[R]

Public Class Methods

new(base_url:, username: nil, password: nil) click to toggle source
# File lib/danger_boss/driver/http.rb, line 16
def initialize(base_url:, username: nil, password: nil)
  @base_url = base_url
  @username = username
  @password = password
  @raw = Faraday.new(url: base_url) do |faraday|
    if username && password
      faraday.basic_auth(username, password)
    end

    faraday.headers.merge!(
      {
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
        'User-Agent' => "danger_boss/#{VERSION} (https://github.com/ess/danger_boss)",
      }
    )

    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter
  end
end

Public Instance Methods

get(path:, params: {}) click to toggle source
# File lib/danger_boss/driver/http.rb, line 38
def get(path:, params: {})
  Try() {raw.get(path, params)}.
    bind {|response| process_response(response)}
end

Private Instance Methods

check_http_status(response) click to toggle source
# File lib/danger_boss/driver/http.rb, line 59
def check_http_status(response)
  Success(response).
    bind {|response|
      status = response.status
  
      if status < 200 || status > 299
        return Failure("API returned a #{status} status")
      end

      Success(response.body)
    }
end
check_response_status(objectified) click to toggle source
# File lib/danger_boss/driver/http.rb, line 72
def check_response_status(objectified)
  if objectified['status'].to_s.downcase != 'success'
    return Failure("API returned an unsuccessful response")
  end

  Success(objectified['data'])
end
construct_url(path:, params: {}) click to toggle source
# File lib/danger_boss/driver/http.rb, line 44
def construct_url(path:, params: {})
  base_url.tap do |url|
    url.path = File.join(url.path, path)
    url.query_values = params
    return url
  end
end
parse_body(body) click to toggle source
# File lib/danger_boss/driver/http.rb, line 80
def parse_body(body)
  Try() {JSON.load(body)}.to_result
end
process_response(response) click to toggle source
# File lib/danger_boss/driver/http.rb, line 52
def process_response(response)
  Success(response).
    bind {|response| check_http_status(response)}.
    bind {|body| parse_body(body)}.
    bind {|objectified| check_response_status(objectified)}
end