Class: PageGlimpse
Constant Summary collapse
- InvalidAPIKeyError =
Class.new(StandardError)
- PG_API_URL =
'http://images.pageglimpse.com/v1'.freeze
- PG_VALID_SIZE_VALUES =
[:small, :medium, :large].freeze
- PG_VALID_ROOT_VALUES =
[:yes, :no].freeze
- PG_IMAGE_FORMAT =
:png- ACTION_PARAMS =
{ :thumbnails => [:devkey, :url, :size, :root, :nothumb], :'thumbnails/exists' => [:devkey, :url, :size], :'thumbnails/request' => [:devkey, :url] }.freeze
- DEFAULT_OPTIONS =
{ :size => :medium, :root => true, :nothumb => nil }.freeze
Instance Attribute Summary collapse
-
#defaults ⇒ Object
Returns the value of attribute defaults.
-
#devkey ⇒ Object
Returns the value of attribute devkey.
-
#nothumb ⇒ Object
Returns the value of attribute nothumb.
-
#root ⇒ Object
Returns the value of attribute root.
-
#size ⇒ Object
Returns the value of attribute size.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#exists?(url, options = {}) ⇒ Boolean
(also: #exist?)
Check if a thumbnail exists.
-
#initialize(devkey, defaults = {}) ⇒ PageGlimpse
constructor
A new instance of PageGlimpse.
-
#request!(url, options = {}) ⇒ Object
Request URL to be captured.
-
#save!(url, path, options = {}) ⇒ Object
Export single thumbnail into a image file (PNG).
-
#thumbnail(url, options = {}) ⇒ Object
(also: #thumbnails)
Get single thumbnail image (PNG).
-
#thumbnail_url(url, options = {}) ⇒ Object
Get thumbnail URL for linking.
Constructor Details
#initialize(devkey, defaults = {}) ⇒ PageGlimpse
Returns a new instance of PageGlimpse.
38 39 40 41 |
# File 'lib/page_glimpse.rb', line 38 def initialize(devkey, defaults = {}) @defaults = DEFAULT_OPTIONS.merge(defaults) @devkey = devkey end |
Instance Attribute Details
#defaults ⇒ Object
Returns the value of attribute defaults.
31 32 33 |
# File 'lib/page_glimpse.rb', line 31 def defaults @defaults end |
#devkey ⇒ Object
Returns the value of attribute devkey.
31 32 33 |
# File 'lib/page_glimpse.rb', line 31 def devkey @devkey end |
#nothumb ⇒ Object
Returns the value of attribute nothumb.
31 32 33 |
# File 'lib/page_glimpse.rb', line 31 def nothumb @nothumb end |
#root ⇒ Object
Returns the value of attribute root.
31 32 33 |
# File 'lib/page_glimpse.rb', line 31 def root @root end |
#size ⇒ Object
Returns the value of attribute size.
31 32 33 |
# File 'lib/page_glimpse.rb', line 31 def size @size end |
#url ⇒ Object
Returns the value of attribute url.
31 32 33 |
# File 'lib/page_glimpse.rb', line 31 def url @url end |
Instance Method Details
#exists?(url, options = {}) ⇒ Boolean Also known as: exist?
Check if a thumbnail exists
87 88 89 90 91 92 93 94 95 |
# File 'lib/page_glimpse.rb', line 87 def exists?(url, = {}) = defaults.merge() @url = url @size = valid_size_value([:size]) query_url = generate_url('thumbnails', 'exists') response = self.class.get(query_url) handle_response(response.code) end |
#request!(url, options = {}) ⇒ Object
Request URL to be captured.
77 78 79 80 81 82 83 84 |
# File 'lib/page_glimpse.rb', line 77 def request!(url, = {}) = defaults.merge() @url = url query_url = generate_url('thumbnails', 'request') response = self.class.get(query_url) handle_response(response.code) end |
#save!(url, path, options = {}) ⇒ Object
Export single thumbnail into a image file (PNG).
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/page_glimpse.rb', line 63 def save!(url, path, = {}) begin data = thumbnail(url, ) path = File.join(path, "#{url.gsub(/(http:)|[\/]/, '')}.#{PG_IMAGE_FORMAT}") if File.directory?(path) File.new(path, 'w') File.open(path, 'w') { |f| f.write(data) } rescue false else true end end |
#thumbnail(url, options = {}) ⇒ Object Also known as: thumbnails
Get single thumbnail image (PNG).
55 56 57 58 59 |
# File 'lib/page_glimpse.rb', line 55 def thumbnail(url, = {}) response = self.class.get(thumbnail_url(url, ).gsub(PG_API_URL, '')) handle_response(response) response.body end |
#thumbnail_url(url, options = {}) ⇒ Object
Get thumbnail URL for linking.
44 45 46 47 48 49 50 51 52 |
# File 'lib/page_glimpse.rb', line 44 def thumbnail_url(url, = {}) = defaults.merge() @url = url @size = valid_size_value([:size]) @root = valid_root_value([:root]) @nothumb = [:nothumb] query_url = PG_API_URL + generate_url('thumbnails') end |