class Giddy::Image

Public Class Methods

new(attrs, mediator) click to toggle source
# File lib/giddy/image.rb, line 5
def initialize(attrs, mediator)
  @attrs = Utils.rubified_hash(attrs)
  @mediator = mediator
end

Public Instance Methods

date_created() click to toggle source
# File lib/giddy/image.rb, line 47
def date_created
  parse_date @attrs[:date_created]
end
date_submitted() click to toggle source
# File lib/giddy/image.rb, line 51
def date_submitted
  parse_date @attrs[:date_submitted]
end
download(size) click to toggle source
# File lib/giddy/image.rb, line 23
def download(size)
  sizekey = size[:size_key]
  result = downloader.get_image_download_authorizations(@attrs[:image_id], [sizekey])
  auths = result[sizekey][:authorizations]
  raise ImageDownloadError, "No authorizations available." if auths.length == 0
  download_request auths.first[:download_token]
end
download_largest() click to toggle source
# File lib/giddy/image.rb, line 41
def download_largest
  authorizations = largest_available[:authorizations]
  return nil if authorizations.length == 0
  download_request authorizations.first[:download_token]
end
download_request(token) click to toggle source
# File lib/giddy/image.rb, line 36
def download_request(token)
  result = downloader.create_download_request(token)
  result[@attrs[:image_id]]
end
downloader() click to toggle source
# File lib/giddy/image.rb, line 14
def downloader
  @downloader ||= Download.new(@mediator)
end
largest_available() click to toggle source
# File lib/giddy/image.rb, line 31
def largest_available
  result = downloader.get_largest_image_download_authorizations(@attrs[:image_id])
  result[@attrs[:image_id]]
end
method_missing(method, *args, &block) click to toggle source
# File lib/giddy/image.rb, line 10
def method_missing(method, *args, &block)
  @attrs.fetch(method, nil)
end
sizes() click to toggle source

return sorted sizes, smallest to largest

# File lib/giddy/image.rb, line 19
def sizes
  sizes_downloadable_images.sort { |a,b| a[:file_size_in_bytes] <=> b[:file_size_in_bytes] }
end
to_s() click to toggle source
# File lib/giddy/image.rb, line 55
def to_s
  as = @attrs.map { |k,v| "#{k}=#{v}" }.join(", ")
  "<Image #{as}>" 
end

Private Instance Methods

parse_date(d) click to toggle source
# File lib/giddy/image.rb, line 61
def parse_date(d)
  # date looks like /Date(1145593612000-0700)/ or /Date(1235980800000)/
  fmt = (d.include?('-') or d.include?('+')) ? "%Q%z" : "%Q"
  DateTime.strptime d.slice(6, d.length-8), fmt
end