Class: PackageCloud::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/package_cloud/repository.rb

Instance Method Summary collapse

Methods inherited from Object

#method_missing, #respond_to?

Constructor Details

#initialize(attrs, config) ⇒ Repository

Returns a new instance of Repository



5
6
7
8
# File 'lib/package_cloud/repository.rb', line 5

def initialize(attrs, config)
  @attrs = attrs
  @config = config
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PackageCloud::Object

Instance Method Details

#create_gpg_key(file_path) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/package_cloud/repository.rb', line 80

def create_gpg_key(file_path)
  file_data = File.new(file_path, 'rb')
  base_url = @config.base_url
  url = PackageCloud::Util.compute_url(@config.base_url, paths["gpg_keys"])
  params = { keydata: file_data }

  print "Attempting to upload key file #{file_path}... "

  begin
    RestClient::Request.execute(:method => 'post',
                                :url => url,
                                :timeout => nil,
                                :payload => { :gpg_key => params })
  rescue RestClient::UnprocessableEntity => e
    print "error: ".color(:red)
    json = JSON.parse(e.response)
    puts json["error"]
    puts ""
    exit(1)
  end

  print "success!\n".color(:green)
end

#create_master_token(name) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/package_cloud/repository.rb', line 116

def create_master_token(name)
  url = PackageCloud::Util.compute_url(@config.base_url, paths["create_master_token"])
  begin
    resp = RestClient.post(url, :master_token => {:name => name})
    resp = JSON.parse(resp)
  rescue RestClient::UnprocessableEntity => e
    print "error:\n".color(:red)
    json = JSON.parse(e.response)
    json.each do |k,v|
      puts "\n\t#{k}: #{v.join(", ")}\n"
    end
    puts ""
    exit(1)
  end
  resp
end

#create_package(file_path, dist_id, files = nil, filetype = nil, coordinates = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/package_cloud/repository.rb', line 34

def create_package(file_path, dist_id, files=nil, filetype=nil, coordinates=nil)
  file_data = File.new(file_path, 'rb')
  base_url = @config.base_url
  url = PackageCloud::Util.compute_url(base_url, paths["create_package"])
  params = { :package_file => file_data,
             :distro_version_id => dist_id }

 if coordinates
   params.merge!(:coordinates => coordinates)
 end

  if filetype == "dsc"
    file_ios = files.inject([]) do |memo, f|
      memo << File.new(f, 'rb')
    end
    params.merge!({:source_files => file_ios})
  end

  RestClient::Request.execute(:method => 'post',
                              :url => url,
                              :timeout => nil,
                              :payload => { :package =>  params })
  print "success!\n".color(:green)
end

#gpg_keysObject



104
105
106
107
108
# File 'lib/package_cloud/repository.rb', line 104

def gpg_keys
  url = PackageCloud::Util.compute_url(@config.base_url, paths["gpg_keys"])
  attrs = JSON.parse(RestClient.get(url))
  attrs["gpg_keys"].map { |a| GpgKey.new(a, @config) }
end

#install_script(type) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/package_cloud/repository.rb', line 59

def install_script(type)
  url = urls["install_script"].gsub(/:package_type/, type)

  # the URL we've obtained above already contains the correct tokens
  # because the install script URL uses a master token for access, not
  # your API token, so if we pass @config.base_url in to compute_url,
  # we'll end up generating a URL like: https://token:@https://token:@...
  # because @config.base_url has the url with the API token in it and url
  # has the url (lol) with the master token in it.
  #
  # so just pass url in here.
  url = PackageCloud::Util.compute_url(url, '')
  script = RestClient.get(url)

  # persist the script to a tempfile to make it easier to execute
  file = Tempfile.new('foo')
  file.write(script)
  file.close
  file
end

#master_tokensObject



110
111
112
113
114
# File 'lib/package_cloud/repository.rb', line 110

def master_tokens
  url = PackageCloud::Util.compute_url(@config.base_url, paths["master_tokens"])
  attrs = JSON.parse(RestClient.get(url))
  attrs.map { |a| MasterToken.new(a, @config) }
end

#parse_dsc(dsc_path, dist_id) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/package_cloud/repository.rb', line 10

def parse_dsc(dsc_path, dist_id)
  file_data = File.new(dsc_path, 'rb')
  base_url = @config.base_url
  url = PackageCloud::Util.compute_url(base_url, paths["package_contents"])
  begin
    resp = RestClient::Request.execute(:method => 'post',
                                       :url => url,
                                       :timeout => nil,
                                       :payload => { :package => {:package_file      => file_data,
                                                                  :distro_version_id => dist_id}})
    resp = JSON.parse(resp)
    print "success!\n"
    resp["files"]
  rescue RestClient::UnprocessableEntity => e
    print "error:\n".color(:red)
    json = JSON.parse(e.response)
    json.each do |k,v|
      puts "\n\t#{k}: #{v.join(", ")}\n"
    end
    puts ""
    exit(1)
  end
end

#private_humanObject



172
173
174
# File 'lib/package_cloud/repository.rb', line 172

def private_human
  send(:private) ? "private".color(:red) : "public".color(:green)
end

#promote(dist, package_name, dest_repo_name, scope = nil) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/package_cloud/repository.rb', line 133

def promote(dist, package_name, dest_repo_name, scope=nil)
  begin
    url = PackageCloud::Util.compute_url(@config.base_url, paths["self"] + "/" + [dist, package_name, "promote.json"].compact.join("/"))
    resp = if scope
             RestClient.post(url, destination: dest_repo_name, scope: scope)
           else
             RestClient.post(url, destination: dest_repo_name)
           end
    resp = JSON.parse(resp)
  rescue RestClient::UnprocessableEntity, RestClient::ResourceNotFound => e
    print "error:\n".color(:red)
    json = JSON.parse(e.response)
    json.each do |k,v|
      puts "\n\t#{k}: #{v.join(", ")}\n"
    end
    puts ""
    exit(1)
  end
end

#yank(dist, package_name, scope = nil) ⇒ Object



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

def yank(dist, package_name, scope=nil)
  begin
    url = PackageCloud::Util.compute_url(@config.base_url, paths["self"] + "/" + [dist, package_name].compact.join("/"))
    if scope
      RestClient.delete(url, params: { scope: scope })
    else
      RestClient.delete(url)
    end
  rescue RestClient::ResourceNotFound => e
    print "error:\n".color(:red)
    json = JSON.parse(e.response)
    json.each do |k,v|
      puts "\n\t#{k}: #{v.join(", ")}\n"
    end
    puts ""
    exit(1)
  end
end