Class: Github::Downloads

Inherits:
Object
  • Object
show all
Defined in:
lib/github/downloads.rb

Defined Under Namespace

Classes: Download, UnexpectedResponse

Constant Summary collapse

GITHUB_BASE_URL =
"https://api.github.com"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, user, repos) ⇒ Downloads

Returns a new instance of Downloads.



13
14
15
16
17
18
# File 'lib/github/downloads.rb', line 13

def initialize(client, user, repos)
  @client = client
  @user = user
  @repos = repos
  @last_response = nil
end

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



9
10
11
# File 'lib/github/downloads.rb', line 9

def last_response
  @last_response
end

#uploaderObject

Returns the value of attribute uploader.



8
9
10
# File 'lib/github/downloads.rb', line 8

def uploader
  @uploader
end

Class Method Details

.connect(user, password, repos) ⇒ Object



20
21
22
23
24
25
# File 'lib/github/downloads.rb', line 20

def self.connect(user, password, repos)
  client = Client.connect(GITHUB_BASE_URL, user, password)
  new(client, user, repos).tap do |downloader|
    downloader.uploader = Github::S3Uploader.new # default uploader
  end
end

Instance Method Details

#create(file_path, description = "", options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/github/downloads.rb', line 45

def create(file_path, description = "", options={})
  delete(:name => File.basename(file_path)) if options[:overwrite]
  
  @last_response = @client.post(downloads_resource_path, {
    :name         => File.basename(file_path),
    :description  => description,
    :content_type => MIME::Types.type_for(file_path)[0] || MIME::Types["application/octet-stream"][0],
    :size         => File.size?(file_path)
  })

  if @last_response.success?
    uploader.upload(File.expand_path(file_path), @last_response.data)
  end
end

#delete(options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/github/downloads.rb', line 60

def delete(options={})
  if download_id = options[:id]
    delete_download_with_id(download_id)
  elsif file_name = options[:name]
    download = list.find { |download| download.name == file_name }
    delete_download_with_id(download.download_id) if download
  else
    raise "Either :id or :name should be specified"
  end
end

#listObject



35
36
37
38
39
40
41
42
43
# File 'lib/github/downloads.rb', line 35

def list
  @last_response = @client.get(downloads_resource_path)

  if @last_response.success?
    from_response_data(@last_response.data)
  else
    raise UnexpectedResponse, @last_response
  end
end