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 = "default", connection = Connection.new) ⇒ Client

Returns a new instance of Client.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/packagecloud/client.rb', line 47

def initialize(credentials, user_agent="default", 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}")
  assert_valid_credentials
  assert_compatible_version
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



163
164
165
166
167
168
# File 'lib/packagecloud/client.rb', line 163

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



83
84
85
86
87
88
89
# File 'lib/packagecloud/client.rb', line 83

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



62
63
64
65
# File 'lib/packagecloud/client.rb', line 62

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

#find_distribution_id(distro_query) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/packagecloud/client.rb', line 145

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"]
    all_distros = deb_distros.merge(rpm_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



78
79
80
81
# File 'lib/packagecloud/client.rb', line 78

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



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

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

  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) ⇒ 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
# File 'lib/packagecloud/client.rb', line 119

def put_package(repo, package)
  assert_valid_repo_name(repo)

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

  mixed_msg = MIME::Multipart::FormData.new

  if package.distro_version_id != nil
    mixed_msg.add(MIME::Text.new(package.distro_version_id), "package[distro_version_id]")
  end

  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|
    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



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

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

#repository(repo) ⇒ Object



72
73
74
75
76
# File 'lib/packagecloud/client.rb', line 72

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