Class: Flickrip::UrlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/flickrip/url_parser.rb

Constant Summary collapse

SET_REGEX =
/^http.*flickr.*\/(.*)\/sets\/(\d+)/
IN_SET_REGEX =
/^http.*flickr.*\/(.*)\/(\d+)\/in\/set-(\d+)/
IMAGE_REGEX =
/^http.*flickr.*\/(.*)\/(\d+)/

Class Method Summary collapse

Class Method Details

.is_image?(url) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/flickrip/url_parser.rb', line 42

def self.is_image?(url)
  h = self.parse(url)
  h.has_key?(:user) && h.has_key?(:imageid)
end

.is_in_set?(url) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/flickrip/url_parser.rb', line 37

def self.is_in_set?(url)
  h = self.parse(url)
  h.has_key?(:user) && h.has_key?(:setid) && h.has_key?(:imageid)
end

.is_set?(url) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/flickrip/url_parser.rb', line 32

def self.is_set?(url)
  h = self.parse(url)
  h.has_key?(:user) && h.has_key?(:setid)
end

.parse(url) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/flickrip/url_parser.rb', line 8

def self.parse(url)
  return {} if url.nil?
  # test for "/in-set/"
  g = url.scan IN_SET_REGEX
  if g.count == 1
    return Hash[ [:user,:imageid,:setid].zip g[0] ]
  end

  # test for "/sets/"
  g = url.scan SET_REGEX
  if g.count == 1
    return Hash[ [:user,:setid].zip g[0] ]
  end

  # fall back to single image
  g = url.scan IMAGE_REGEX
  if g.count == 1
    return Hash[ [:user,:imageid].zip g[0] ]
  end

  # if we get here, we didnt have a valid flickr url
  {}
end