Skip to content
This repository was archived by the owner on Feb 19, 2024. It is now read-only.

add new methods #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Gemfile.lock
doc/
pkg/
vendor/cache/*.gem
coverage/
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sudo: false
language: ruby
before_install:
- gem install bundler
rvm:
- 1.8.7
- 1.9.3
- 2.1.0
- 2.4.3
- 2.5.0
- 2.5.8
- 2.6.6
- 2.7.1
8 changes: 4 additions & 4 deletions lib/packagecloud.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'packagecloud/version'
require 'packagecloud/client'
require 'packagecloud/package'
require 'packagecloud/credentials'
require "packagecloud/version"
require "packagecloud/client"
require "packagecloud/package"
require "packagecloud/credentials"
206 changes: 117 additions & 89 deletions lib/packagecloud/client.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'multi_json'
require 'mime'
require 'excon'
require 'packagecloud/result'
require 'packagecloud/connection'
require 'packagecloud/version'
require "multi_json"
require "mime"
require "excon"
require "packagecloud/result"
require "packagecloud/connection"
require "packagecloud/version"

module Packagecloud
SUPPORTED_EXTENSIONS = ["deb", "dsc", "gem", "rpm", "whl", "zip", "egg", "egg-info", "tar", "bz2", "Z", "gz", "tgz"]
Expand Down Expand Up @@ -44,7 +44,7 @@ class Client
attr_reader :connection
attr_reader :credentials

def initialize(credentials, user_agent="packagecloud-ruby #{Packagecloud::VERSION}", connection=Connection.new)
def initialize(credentials, user_agent = "packagecloud-ruby #{Packagecloud::VERSION}", connection = Connection.new)
@credentials = credentials
@connection = connection
@user_agent = user_agent
Expand All @@ -54,7 +54,7 @@ def initialize(credentials, user_agent="packagecloud-ruby #{Packagecloud::VERSIO
port = self.connection.port
token = self.credentials.token

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

Expand All @@ -79,15 +79,15 @@ def gem_version
parsed_json_result(response)
end

def create_repository(repo, private=false)
def create_repository(repo, private = false)
assert_valid_repo_name(repo)
privacy = private ? 1 : 0
body = { "repository" => { "name" => repo, "private" => privacy.to_s } }
body = {"repository" => {"name" => repo, "private" => privacy.to_s}}
response = post("/api/v1/repos.json", body.to_json)
parsed_json_result(response)
end

def package_contents(repo, package, distro_version_id=nil)
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!"
Expand All @@ -99,7 +99,7 @@ def package_contents(repo, package, distro_version_id=nil)

package.file.rewind
pkg_data = MIME::Application.new(package.file.read)
pkg_data.headers.set('Content-Transfer-Encoding', 'binary')
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
Expand All @@ -121,21 +121,51 @@ def list_packages(repo)
parsed_json_result(response)
end

def gem_versions(repo, package_name)
assert_valid_repo_name(repo)
response = get("/api/v1/repos/#{username}/#{repo}/package/gem/#{package_name}/versions.json")
parsed_json_result(response)
end

def gem_show(repo, package_name, version)
assert_valid_repo_name(repo)
response = get("/api/v1/repos/#{username}/#{repo}/package/gem/#{package_name}/#{version}.json")
parsed_json_result(response)
end

def node_versions(repo, package_name)
assert_valid_repo_name(repo)
response = get("/api/v1/repos/#{username}/#{repo}/package/node/#{package_name}/versions.json")
parsed_json_result(response)
end

def node_show(repo, package_name, version)
assert_valid_repo_name(repo)
response = get("/api/v1/repos/#{username}/#{repo}/package/node/#{package_name}/#{version}.json")
parsed_json_result(response)
end

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

def put_package(repo, package, distro_version_id=nil)
def all_packages(repo)
assert_valid_repo_name(repo)
response = get("/api/v1/repos/#{username}/#{repo}/packages.json")
parsed_json_result(response)
end

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
unless 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?
Expand All @@ -147,13 +177,13 @@ def put_package(repo, package, distro_version_id=nil)

package.file.rewind
pkg_data = MIME::Application.new(package.file.read)
pkg_data.headers.set('Content-Transfer-Encoding', 'binary')
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')
src_pkg_data.headers.set("Content-Transfer-Encoding", "binary")
mixed_msg.add(src_pkg_data, "package[source_files][]", filename)
end

Expand All @@ -172,12 +202,10 @@ def find_distribution_id(distro_query)
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(' ')
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
Expand Down Expand Up @@ -205,92 +233,92 @@ def list_read_tokens(repo, master_token_id)
end

private
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

def assert_valid_credentials
result = distributions
if result.succeeded == false && result.response.downcase.include?('unauthenticated')
raise UnauthenticatedException
end
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

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
def assert_valid_credentials
result = distributions
if result.succeeded == false && result.response.downcase.include?("unauthenticated")
raise UnauthenticatedException
end
end

def user_agent
"packagecloud-ruby #{VERSION}/#{@user_agent}"
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

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
def user_agent
"packagecloud-ruby #{VERSION}/#{@user_agent}"
end

def post(url, body, content_type="application/json")
request(url, :post, body, content_type)
end
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

def get(url)
request(url, :get)
end
def post(url, body, content_type = "application/json")
request(url, :post, body, content_type)
end

def delete(url)
request(url, 'DELETE')
end
def get(url)
request(url, :get)
end

def username
self.credentials.username
end
def delete(url)
request(url, "DELETE")
end

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
def username
credentials.username
end

@excon.request(request_params.merge({:read_timeout => connection.read_timeout, :write_timeout => connection.write_timeout }))
def request(url, method=:get, body=nil, content_type=nil)
headers = {"User-Agent" => user_agent}
unless content_type.nil?
headers["Content-Type"] = content_type
end

# returns the parsed body of a successful result
def parsed_json_result(response)
prepare_result(response) { |result| result.response = MultiJson.load(response.data[:body]) }
request_params = {method: method, path: url, headers: headers}
unless body.nil?
request_params[:body] = body
end

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
@excon.request(request_params.merge({read_timeout: connection.read_timeout, write_timeout: connection.write_timeout}))
end

# returns the parsed body of a successful result
def parsed_json_result(response)
prepare_result(response) { |result| result.response = MultiJson.load(response.data[:body]) }
end

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
end
end
4 changes: 2 additions & 2 deletions lib/packagecloud/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Packagecloud
MAJOR_VERSION = "1"
MINOR_VERSION = "0"
PATCH_VERSION = "8"
MINOR_VERSION = "1"
PATCH_VERSION = "0"

VERSION = [MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION].join(".")
end
Loading