Class: FroyoApi::Photo

Inherits:
Client
  • Object
show all
Defined in:
lib/froyo_api/photo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Client

#request

Constructor Details

#initializePhoto

Returns a new instance of Photo.



7
8
9
10
# File 'lib/froyo_api/photo.rb', line 7

def initialize
  @href = attributes['href']
  @type = attributes['type']
end

Instance Attribute Details

#hrefObject (readonly)

Returns the value of attribute href.



5
6
7
# File 'lib/froyo_api/photo.rb', line 5

def href
  @href
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/froyo_api/photo.rb', line 5

def type
  @type
end

Instance Method Details

#download(directory = '.') ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/froyo_api/photo.rb', line 19

def download(directory = '.')
  file = File.join(directory, filename)

  File.open(file, 'wb') do |out|
    process_response = lambda do |response|
      response.read_body do |chunk|
        out.write(chunk)
      end
    end

    options = {
      url:            href,
      method:         :get,
      block_response: process_response,
      headers:        {
        accept: type
      }
    }

    RestClient::Request.execute(options)
  end
end

#filenameObject



12
13
14
15
16
17
# File 'lib/froyo_api/photo.rb', line 12

def filename
  @filename ||= begin
    uri = URI.parse(href)
    File.basename(uri.path)
  end
end