Class: Pkghub::API::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/pkghub/api/package.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Package

Returns a new instance of Package.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pkghub/api/package.rb', line 12

def initialize(options)

  # Validate options
  missing_options = [:path, :distro].reject do |option|
    options.has_key?(option)
  end

  if missing_options.size > 0
    raise "Missing options: #{missing_options.join(", ")}"
  end

  # Default to making package active if not told otherwise
  options[:active] = true if options[:active].nil?

  # Check file is there
  @path = options[:path]
  @distro = options[:distro]
  raise "Couldn't find file #{@path}" unless File.exists?(@path)

end

Instance Method Details

#upload(project, token) ⇒ Object

Upload file to pkghub.io



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

def upload(project, token)

  uri = URI.parse("https://pkghub.io/api/v1/upload")
  n = Net::HTTP.new(uri.host, uri.port)
  n.use_ssl = true

  File.open(@path) do |package_file|
    req = Net::HTTP::Post::Multipart.new uri.path,
          "token"   => token,
          "project" => project,
          "distro"  => @distro,
          "file"    => UploadIO.new(package_file, "application/x-deb", File.basename(@path))

    res = n.start do |http|
      http.request(req) 
    end

    if res.code == "200"
      puts "Success"
    else
      raise "Error uploading to pkghub.io API, code: #{res.code} body: #{res.body}"
    end
  end

end