Class: Packagecloud::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/packagecloud/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials, user_agent = "packagecloud-ruby #{Packagecloud::VERSION}", connection = Connection.new) ⇒ Client

Returns a new instance of Client.



47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



44
45
46
# File 'lib/packagecloud/client.rb', line 44

def connection
  @connection
end

#credentialsObject (readonly)

Returns the value of attribute credentials.



45
46
47
# File 'lib/packagecloud/client.rb', line 45

def credentials
  @credentials
end

Instance Method Details

#create_read_tokens(repo, master_token_id, read_token_name) ⇒ Object



172
173
174
175
176
177
# File 'lib/packagecloud/client.rb', line 172

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) ⇒ Object



82
83
84
85
86
87
88
# 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) ⇒ Object



112
113
114
115
116
117
# File 'lib/packagecloud/client.rb', line 112

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

#distributionsObject



61
62
63
64
# 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) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/packagecloud/client.rb', line 153

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"]
    all_distros = deb_distros.merge(rpm_distros).merge(py_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_versionObject



77
78
79
80
# 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) ⇒ Object



106
107
108
109
110
# File 'lib/packagecloud/client.rb', line 106

def list_packages(repo)
  assert_valid_repo_name(repo)
  response = get("/api/v1/repos/#{username}/#{repo}/packages.json")
  parsed_json_result(response)
end

#package_contents(repo, package) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/packagecloud/client.rb', line 90

def package_contents(repo, package)
  assert_valid_repo_name(repo)
  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)

  response = multipart_post(url, mixed_msg)

  parsed_json_result(response)
end

#put_package(repo, package, distro_version_id = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/packagecloud/client.rb', line 119

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

#repositoriesObject



66
67
68
69
# File 'lib/packagecloud/client.rb', line 66

def repositories
  response = get("/api/v1/repos.json")
  parsed_json_result(response)
end

#repository(repo) ⇒ Object



71
72
73
74
75
# 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