Class: PageGlimpse

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/page_glimpse.rb

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

Instance Method Summary collapse

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

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
# File 'lib/page_glimpse.rb', line 87

def exists?(url, options = {})
  options = defaults.merge(options)
  @url = url
  @size = valid_size_value(options[: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, options = {})
  options = defaults.merge(options)
  @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, options = {})
  begin
    data = thumbnail(url, options)
    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, options = {})
  response = self.class.get(thumbnail_url(url, options).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, options = {})
  options = defaults.merge(options)
  @url = url
  @size = valid_size_value(options[:size])
  @root = valid_root_value(options[:root])
  @nothumb = options[:nothumb]
  
  query_url = PG_API_URL + generate_url('thumbnails')
end