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

#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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/packagecloud/client.rb', line 132

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

#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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/packagecloud/client.rb', line 106

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