class GitlabRuby::Client

Constants

BASE_URL
LATEST_VERSION

Public Class Methods

debug() click to toggle source
# File lib/gitlab_ruby/client.rb, line 13
def self.debug
  @debug ||= false
end
debug=(v) click to toggle source
# File lib/gitlab_ruby/client.rb, line 17
def self.debug=(v)
  @debug = !!v
end
new(params={}) click to toggle source
# File lib/gitlab_ruby/client.rb, line 6
def initialize(params={})
  @token = params[:token]
  @urlstring = ""
  @endpoint = params[:endpoint] || BASE_URL
  @version = params[:version] || LATEST_VERSION
end

Public Instance Methods

api_call(method_name, options, verb=:get) click to toggle source
# File lib/gitlab_ruby/client.rb, line 37
def api_call(method_name, options, verb=:get)
  response = connection(method_name, options, verb)
  puts response.inspect if self.class.debug
  result = parse_response(response)
  if result.is_a? Array
    GitlabRuby::PaginatedResponse.new(
      client: self,
      array: result.map { |item| APIObject.new(item) },
      headers: response.headers
    )
  elsif result.is_a? Hash
    APIObject.new(result)
  end
end
delete() click to toggle source
# File lib/gitlab_ruby/client.rb, line 33
def delete
  GitlabRuby::QueryChain.new(client: self, verb: :delete)
end
get() click to toggle source
# File lib/gitlab_ruby/client.rb, line 29
def get
  GitlabRuby::QueryChain.new(client: self, verb: :get)
end
post() click to toggle source
# File lib/gitlab_ruby/client.rb, line 25
def post
  GitlabRuby::QueryChain.new(client: self, verb: :post)
end
put() click to toggle source
# File lib/gitlab_ruby/client.rb, line 21
def put
  GitlabRuby::QueryChain.new(client: self, verb: :put)
end

Private Instance Methods

connection(method_name, options, verb) click to toggle source
# File lib/gitlab_ruby/client.rb, line 66
def connection(method_name, options, verb)
  conn = Faraday.new(url: versioned_url) do |faraday|
    faraday.request  :url_encoded
    faraday.response(:logger) if self.class.debug
    faraday.adapter  Faraday.default_adapter
    faraday.headers['User-Agent'] = "GitlabRuby gem v#{VERSION}"
    faraday.headers['PRIVATE-TOKEN'] = @token
  end

  case verb
    when :put then conn.put(method_name, options)
    when :post then conn.post(method_name, options)
    when :delete then conn.delete(method_name, options)
    else conn.get(method_name, options)
  end
end
parse_response(response) click to toggle source
# File lib/gitlab_ruby/client.rb, line 58
def parse_response(response)
  if response.status.to_i >= 400
    GitlabRuby::check_response_status(response)
  else
    JSON.parse(response.body)
  end
end
versioned_url() click to toggle source
# File lib/gitlab_ruby/client.rb, line 54
def versioned_url
  BASE_URL + @version + "/"
end