class Packagecloud::Client

Attributes

connection[R]
credentials[R]

Public Class Methods

new(credentials, user_agent="packagecloud-ruby click to toggle source
# File lib/packagecloud/client.rb, line 47
def initialize(credentials, user_agent="packagecloud-ruby #{Packagecloud::VERSION}", connection=Connection.new)
  @credentials = credentials
  @connection = connection
  @user_agent = user_agent

  scheme = self.connection.scheme
  host = self.connection.host
  port = self.connection.port
  token = self.credentials.token

  @excon = Excon.new("#{scheme}://#{token}@#{host}:#{port}", :connect_timeout => @connection.connect_timeout)
  assert_valid_credentials
end

Public Instance Methods

create_read_tokens(repo, master_token_id, read_token_name) click to toggle source
# File lib/packagecloud/client.rb, line 185
def create_read_tokens(repo, master_token_id, read_token_name)
  assert_valid_repo_name(repo)
  url = "/api/v1/repos/#{username}/#{repo}/master_tokens/#{master_token_id}/read_tokens.json"
  response = post(url, "read_token[name]=#{read_token_name}", "application/x-www-form-urlencoded")
  parsed_json_result(response)
end
create_repository(repo, private=false) click to toggle source
# File lib/packagecloud/client.rb, line 82
def create_repository(repo, private=false)
  assert_valid_repo_name(repo)
  privacy = private ? 1 : 0
  body = { "repository" => { "name" => repo, "private" => privacy.to_s } }
  response = post("/api/v1/repos.json", body.to_json)
  parsed_json_result(response)
end
delete_package(repo, distro, distro_release, package_filename) click to toggle source
# File lib/packagecloud/client.rb, line 124
def delete_package(repo, distro, distro_release, package_filename)
  assert_valid_repo_name(repo)
  url = "/api/v1/repos/#{username}/#{repo}/#{distro}/#{distro_release}/#{package_filename}"
  response = delete(url)
  parsed_json_result(response)
end
delete_read_token(repo, master_token_id, read_token_id) click to toggle source
# File lib/packagecloud/client.rb, line 192
def delete_read_token(repo, master_token_id, read_token_id)
  assert_valid_repo_name(repo)
  url = "/api/v1/repos/#{username}/#{repo}/master_tokens/#{master_token_id}/read_tokens/#{read_token_id}"
  response = delete(url)
  Result.new.tap do |result|
    result.succeeded = (response.status == 204)
  end
end
distributions() click to toggle source
# File lib/packagecloud/client.rb, line 61
def distributions
  response = get("/api/v1/distributions.json")
  parsed_json_result(response)
end
find_distribution_id(distro_query) click to toggle source
# File lib/packagecloud/client.rb, line 165
def find_distribution_id(distro_query)
  distros = distributions
  if distros.succeeded
    deb_distros = distro_map distros.response["deb"]
    rpm_distros = distro_map distros.response["rpm"]
    py_distros = distro_map distros.response["py"]
    node_distros = distro_map distros.response["node"]
    all_distros = deb_distros.merge(rpm_distros).merge(py_distros).merge(node_distros)
    result = all_distros.select { |distro, id| distro.include?(distro_query) }
    if result.size > 1
      keys = result.map { |x| x.first }.join(' ')
      raise ArgumentError, "'#{distro_query}' is ambiguous, did you mean: #{keys}?"
    elsif result.size == 1
      result.first[1] # [["ubuntu/breezy", 1]]
    else
      nil
    end
  end
end
gem_version() click to toggle source
# File lib/packagecloud/client.rb, line 77
def gem_version
  response = get("/api/v1/gem_version.json")
  parsed_json_result(response)
end
list_packages(repo) click to toggle source
# File lib/packagecloud/client.rb, line 118
def list_packages(repo)
  assert_valid_repo_name(repo)
  response = get("/api/v1/repos/#{username}/#{repo}/packages.json")
  parsed_json_result(response)
end
list_read_tokens(repo, master_token_id) click to toggle source
# File lib/packagecloud/client.rb, line 201
def list_read_tokens(repo, master_token_id)
  assert_valid_repo_name(repo)
  response = get("/api/v1/repos/#{username}/#{repo}/master_tokens/#{master_token_id}/read_tokens.json")
  parsed_json_result(response)
end
package_contents(repo, package, distro_version_id=nil) click to toggle source
# File lib/packagecloud/client.rb, line 90
def package_contents(repo, package, distro_version_id=nil)
  assert_valid_repo_name(repo)
  if distro_version_id.nil?
    raise "No distribution supplied for package_contents!"
  end

  url = "/api/v1/repos/#{username}/#{repo}/packages/contents.json"

  mixed_msg = MIME::Multipart::FormData.new

  package.file.rewind
  pkg_data = MIME::Application.new(package.file.read)
  pkg_data.headers.set('Content-Transfer-Encoding', 'binary')
  mixed_msg.add(pkg_data, "package[package_file]", package.filename)

  if distro_version_id.is_a? String
    distro_version = find_distribution_id(distro_version_id)
    raise "Cannot find distribution: #{distro_version_id}" if distro_version.nil?
    mixed_msg.add(MIME::Text.new(distro_version), "package[distro_version_id]")
  else
    mixed_msg.add(MIME::Text.new(distro_version_id), "package[distro_version_id]")
  end

  response = multipart_post(url, mixed_msg)

  parsed_json_result(response)
end
put_package(repo, package, distro_version_id=nil) click to toggle source
# File lib/packagecloud/client.rb, line 131
def put_package(repo, package, distro_version_id=nil)
  assert_valid_repo_name(repo)

  url = "/api/v1/repos/#{username}/#{repo}/packages.json"

  mixed_msg = MIME::Multipart::FormData.new

  if distro_version_id != nil
    if distro_version_id.is_a? String
      distro_version = find_distribution_id(distro_version_id)
      raise "Cannot find distribution: #{distro_version_id}" if distro_version.nil?
      mixed_msg.add(MIME::Text.new(distro_version), "package[distro_version_id]")
    else
      mixed_msg.add(MIME::Text.new(distro_version_id), "package[distro_version_id]")
    end
  end

  package.file.rewind
  pkg_data = MIME::Application.new(package.file.read)
  pkg_data.headers.set('Content-Transfer-Encoding', 'binary')
  mixed_msg.add(pkg_data, "package[package_file]", package.filename)

  package.source_files.each do |filename, io|
    io.rewind
    src_pkg_data = MIME::Application.new(io.read)
    src_pkg_data.headers.set('Content-Transfer-Encoding', 'binary')
    mixed_msg.add(src_pkg_data, "package[source_files][]", filename)
  end

  response = multipart_post(url, mixed_msg)

  prepare_result(response) { |result| result.response = "" }
end
repositories() click to toggle source
# File lib/packagecloud/client.rb, line 66
def repositories
  response = get("/api/v1/repos.json")
  parsed_json_result(response)
end
repository(repo) click to toggle source
# File lib/packagecloud/client.rb, line 71
def repository(repo)
  assert_valid_repo_name(repo)
  response = get("/api/v1/repos/#{username}/#{repo}.json")
  parsed_json_result(response)
end

Private Instance Methods

assert_valid_credentials() click to toggle source
# File lib/packagecloud/client.rb, line 218
def assert_valid_credentials
  result = distributions
  if result.succeeded == false && result.response.downcase.include?('unauthenticated')
    raise UnauthenticatedException
  end
end
assert_valid_repo_name(repo) click to toggle source
# File lib/packagecloud/client.rb, line 208
def assert_valid_repo_name(repo)
  if repo.include?("/")
    raise InvalidRepoNameException.new("The repo name: #{repo} is " \
                                       "invalid. It looks like you are " \
                                       "using the fully qualified name " \
                                       "(fqname) instead of just the " \
                                       "repo name. Please try again.")
  end
end
delete(url) click to toggle source
# File lib/packagecloud/client.rb, line 257
def delete(url)
  request(url, 'DELETE')
end
distro_map(distros) click to toggle source
# File lib/packagecloud/client.rb, line 225
def distro_map(distros)
  result = {}
  distros.each do |distro|
    name = distro["index_name"]
    distro["versions"].each do |version|
      version_name = version["index_name"]
      key = "#{name}/#{version_name}"
      result[key] = version["id"]
    end
  end
  result
end
get(url) click to toggle source
# File lib/packagecloud/client.rb, line 253
def get(url)
  request(url, :get)
end
multipart_post(url, mixed_msg) click to toggle source
# File lib/packagecloud/client.rb, line 242
def multipart_post(url, mixed_msg)
  boundary = mixed_msg.boundary
  content_type = "multipart/form-data; boundary=#{boundary}"
  body = mixed_msg.to_s
  post(url, body, content_type)
end
parsed_json_result(response) click to toggle source

returns the parsed body of a successful result

# File lib/packagecloud/client.rb, line 279
def parsed_json_result(response)
  prepare_result(response) { |result| result.response = MultiJson.load(response.data[:body]) }
end
post(url, body, content_type="application/json") click to toggle source
# File lib/packagecloud/client.rb, line 249
def post(url, body, content_type="application/json")
  request(url, :post, body, content_type)
end
prepare_result(response) { |result| ... } click to toggle source
# File lib/packagecloud/client.rb, line 283
def prepare_result(response)
  result = Result.new
  if response.status == 201 || response.status == 200
    result.succeeded = true
    yield result
  else
    result.response = response.data[:body]
    result.succeeded = false
  end
  result
end
request(url, method=:get, body=nil, content_type=nil) click to toggle source
# File lib/packagecloud/client.rb, line 265
def request(url, method=:get, body=nil, content_type=nil)
  headers = { "User-Agent" => user_agent }
  if content_type != nil
    headers.merge!({ "Content-Type" => content_type })
  end
  request_params = { :method => method, :path => url, :headers => headers }
  if body != nil
    request_params.merge!({ :body => body })
  end

  @excon.request(request_params.merge({:read_timeout => connection.read_timeout, :write_timeout => connection.write_timeout }))
end
user_agent() click to toggle source
# File lib/packagecloud/client.rb, line 238
def user_agent
  "packagecloud-ruby #{VERSION}/#{@user_agent}"
end
username() click to toggle source
# File lib/packagecloud/client.rb, line 261
def username
  self.credentials.username
end