Class: Thumbnail::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/thumbnail/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

:access_key_id and :secret_access_key are required. Optionally, you can indicate :action => :redirect (default is :thumbnail), :size => :small (default is :large), and :default_image => '<image_url>' (only available in conjunction with :action => :redirect)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/thumbnail/client.rb', line 15

def initialize(opts={})
  if opts[:action] && ![:thumbnail, :redirect].include?(opts[:action])
    raise ArgumentError, "Bad action. Must be thumbnail or redirect. Defaults to thumbnail." 
  end
  
  if opts[:size] && ![:large, :small].include?(opts[:size])
    raise ArgumentError, "Size must be large or small. Defaults to large"
  end
  
  unless opts[:access_key_id] && opts[:secret_access_key]
    raise ArgumentError, "Must include your access_key_id and your secret_access_key."
  end 
  
  @size = opts[:size] || :large
  @action = opts[:action] || :thumbnail 
  @access_key_id = opts[:access_key_id]
  @secret_access_key = opts[:secret_access_key]
      
  if opts[:default_image] && (@action == :thumbnail)
    raise ArgumentError, "Can only specify a default_image on the redirect action."
  end
      
  @default_image = opts[:default_image]
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



9
10
11
# File 'lib/thumbnail/client.rb', line 9

def action
  @action
end

#sizeObject

Returns the value of attribute size.



9
10
11
# File 'lib/thumbnail/client.rb', line 9

def size
  @size
end

Instance Method Details

#get(url) ⇒ Object

The main method for using this library. get() can take a single url, or an array of them (only available for the thumbnail action). For a request with :action => :redirect it will return the composed query url; for a thumbnail action it will build your query, send it to Amazon, and build and return the Thumbnail::Response object.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/thumbnail/client.rb', line 46

def get(url)
  if (@action == :redirect) 
    if url.is_a?(Array)
      raise ArgumentError, "Can only get a single url on a redirect action"
    else
      @url = url
      return query
    end
  end
  
  url.each{|u| URI.parse(u)}
  
  @url = url
  xml = Net::HTTP.get(URI.parse(query))
  Response.build(xml)
end