Module: Vendor::API

Extended by:
API
Included in:
API
Defined in:
lib/vendor/api.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Instance Method Details

#api_key(username, password) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/vendor/api.rb', line 17

def api_key(username, password)
  perform do
    url = resource(username, password)["users/#{username}/api_key.json"]
    Vendor.ui.debug "GET #{url}"

    response = url.get
    JSON.parse(response.body)["api_key"]
  end
end

#api_uriObject



13
14
15
# File 'lib/vendor/api.rb', line 13

def api_uri
  ENV["API_URI"] || 'http://vendorkit.com'
end

#download(name, version) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vendor/api.rb', line 42

def download(name, version)
  perform do
    info = meta(name)

    # Does the version exist?
    unless info['versions'].find { |v| Vendor::Version.create(v[0]) == Vendor::Version.create(version) }
      raise Error.new("Could not find a valid version for '#{name}' that matches '#{version}'")
    end

    url = resource["vendors/#{slugerize(name)}/versions/#{version}/download"]
    Vendor.ui.debug "GET #{url}"
    Vendor.ui.info "Downloading #{name} #{version}"

    binary = url.get

    filename = "#{name}-#{version}.vendor"
    tmpfile = File.join(Dir.mktmpdir(filename), filename)

    file = File.new(tmpfile, 'w')
    file.write binary
    file.close

    file
  end
end

#meta(name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vendor/api.rb', line 27

def meta(name)
  @meta ||= {}

  perform :on_404 => "Could not find a valid vendor '#{name}'" do
    unless @meta[name]
      url = resource["vendors/#{slugerize(name)}.json"]
      Vendor.ui.debug "GET #{url}"

      @meta[name] = JSON.parse(url.get.body)
    end

    @meta[name]
  end
end

#push(options) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vendor/api.rb', line 68

def push(options)
  perform do
    url = resource["vendors.json"]
    Vendor.ui.debug "POST #{url}"

    response = url.post :version => { :package => File.new(options[:file]) }, :api_key => options[:api_key]
    json = JSON.parse(response.body)

    if json["status"] == "ok"
      json["url"]
    else
      raise Error.new(json["message"])
    end
  end
end