class SemaphoreCI::API::Client

Public Class Methods

new(token, url, prefix, link_parser_class) click to toggle source
# File lib/semaphore_ci/api.rb, line 4
def initialize(token, url, prefix, link_parser_class)
  @token = token
  @url = url
  @prefix = prefix
  @link_parser = link_parser_class.new

  @conn = Faraday.new(url: @url) do |conn|
    # uncomment to enable Faraday debugging
    # conn.response :logger
    # conn.token_auth token # FIXME: make we work for V2 API
    conn.adapter :semaphore_net_http
  end
end

Public Instance Methods

get(path) click to toggle source
# File lib/semaphore_ci/api.rb, line 18
def get(path)
  response = @conn.get(path.prepend(@prefix), auth_token: @token)
  result = json_from(response)

  while (next_link = @link_parser.parse(response).by_rel('next'))
    response = @conn.get(next_link.target.to_s)
    next_result = json_from(response)

    case [result.class, next_result.class]
    when [Array, Array]
      # V2 pagination
      result.concat(next_result)
    when [Hash, Hash]
      # V1 pagination
      next_result.each do |key, value|
        if value.is_a? Array
          result[key].concat(value)
        elsif value.is_a? Hash
          result[key].merge(value)
        else
          result[key] = value
        end
      end
    else raise 'Unsupported API response'
    end
  end

  result
end

Private Instance Methods

json_from(response) click to toggle source
# File lib/semaphore_ci/api.rb, line 50
def json_from(response)
  if (content_type = response['content-type'])
    # should be something like "application/json; charset=utf-8"
    content_type['application/json'] || raise('JSON body expected')
  end

  JSON.parse(response.body)
end