class SemaphoreClient::HttpClient

Public Class Methods

new(auth_token, api_url, api_version, logger, auto_paginate) click to toggle source
# File lib/semaphore_client/http_client.rb, line 28
def initialize(auth_token, api_url, api_version, logger, auto_paginate)
  @auth_token = auth_token
  @api_url = api_url
  @api_version = api_version
  @logger = logger
  @auto_paginate = auto_paginate
end

Public Instance Methods

delete(path, params = nil, options = {}) click to toggle source
# File lib/semaphore_client/http_client.rb, line 48
def delete(path, params = nil, options = {})
  api_call(:delete, path, params, options)
end
get(path, params = nil, options = {}) click to toggle source
# File lib/semaphore_client/http_client.rb, line 36
def get(path, params = nil, options = {})
  api_call(:get, path, params, options)
end
patch(path, params = nil, options = {}) click to toggle source
# File lib/semaphore_client/http_client.rb, line 44
def patch(path, params = nil, options = {})
  api_call(:patch, path, params, options)
end
post(path, params = nil, options = {}) click to toggle source
# File lib/semaphore_client/http_client.rb, line 40
def post(path, params = nil, options = {})
  api_call(:post, path, params, options)
end

Private Instance Methods

api_call(method, path, params = nil, options = {}) click to toggle source
# File lib/semaphore_client/http_client.rb, line 54
def api_call(method, path, params = nil, options = {})
  response = connection.public_send(method, "/#{@api_version}/#{path}", params)

  if auto_paginate?(options)
    links = parse_links(response)

    if links[:next]
      # recursivly follow the :next link
      next_response = api_call(method, links[:next], params, options)

      # append the rest to the body of the original request
      response.body.concat(next_response.body)
    end
  end

  response
end
auto_paginate?(options) click to toggle source
# File lib/semaphore_client/http_client.rb, line 72
def auto_paginate?(options)
  # first check the options, then the global settings
  options.key?(:auto_paginate) ? options[:auto_paginate] : @auto_paginate
end
connection() click to toggle source
# File lib/semaphore_client/http_client.rb, line 87
def connection
  @connection ||= Faraday.new(:url => @api_url, :headers => headers) do |conn|
    conn.request :json
    conn.response :json

    conn.use SemaphoreClient::HttpClient::ResponseErrorMiddleware

    if @logger
      conn.response :logger, @logger, :headers => false, :bodies => true
    end

    conn.adapter Faraday.default_adapter
  end
end
headers() click to toggle source
# File lib/semaphore_client/http_client.rb, line 102
def headers
  { "Authorization" => "Token #{@auth_token}" }
end