Class: Thornbed::Providers::Imgur

Inherits:
Provider
  • Object
show all
Defined in:
lib/thornbed/providers/imgur.rb

Constant Summary

Constants inherited from Provider

Provider::USER_AGENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Provider

inherited, #provider_name, #valid?

Constructor Details

#initializeImgur

Returns a new instance of Imgur.



10
11
12
# File 'lib/thornbed/providers/imgur.rb', line 10

def initialize
  self.pattern = /^https?:\/\/(www\.|i\.)?imgur\.com\/(gallery\/)?(?<pid>\w{5,})(?<ptype>\.gif|\.jpg|\.jpeg|\.png)?(\?full)?$/
end

Instance Attribute Details

#patternObject

Returns the value of attribute pattern.



8
9
10
# File 'lib/thornbed/providers/imgur.rb', line 8

def pattern
  @pattern
end

Instance Method Details

#get(url) ⇒ Object

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/thornbed/providers/imgur.rb', line 14

def get(url)
  raise Thornbed::NotValid, url if !valid?(url)

  # imgur pic_id is ambiguous since the chars for thumb etc... can be part of the id
  # so we check the embed page
  # first get the pseudo id
  md = pattern.match(url)
  pid = md[:pid]

  # from Alan Schaaf, Founder & CEO, Imgur.com
  # If the image ID is 6 or 8 characters then it's a thumbnail.
  if [6,8].include? pid.size
    pid = pid[0...-1]
  end

  raise Thornbed::NotValid, url if not pid

  ptype = md[:ptype] || ".jpg"

  {
    "url" => "http://i.imgur.com/#{pid}#{ptype}",
    "type" => "photo",
    "provider_name" => provider_name,
    "provider_url" => "http://imgur.com",
    "thumbnail_url" => "http://i.imgur.com/#{pid}s#{ptype}",
    "version" => "1.0",
    "page" => "http://imgur.com/#{pid}",
    "width" => nil,
    "height" => nil
  }
end