Class: Flickrcaptionr::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/flickrcaptionr/fetcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(fetchers = []) ⇒ Fetcher

Returns a new instance of Fetcher.



2
3
4
5
6
7
8
9
10
11
# File 'lib/flickrcaptionr/fetcher.rb', line 2

def initialize(fetchers=[])
  if not fetchers or (fetchers and fetchers.size == 0)
    fetchers = [Flickrcaptionr::Fetchers::Flickr, Flickrcaptionr::Fetchers::OEmbed]
  end
  @fetchers = []
  # Spin up each fetcher
  for f in fetchers
    @fetchers.push(f.new)
  end
end

Instance Method Details

#do_fetch(to_get, fetcher) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/flickrcaptionr/fetcher.rb', line 26

def do_fetch(to_get, fetcher)
  out_name = File.basename(to_get)
  if !Flickrcaptionr::Config.output_path or Flickrcaptionr::Config.output_path == ''
    out_name = File.join(Dir.pwd, out_name)
  else
    out_name = File.join(Flickrcaptionr::Config.output_path, out_name)
  end
  unless File.exists?(out_name)
    puts "Downloading #{to_get}..."
    fetcher.download_file(to_get, out_name)
    puts "Written file to #{out_name}"
  else
    puts "Already got #{to_get}, not redownloading"
  end
  return out_name
end

#fetch(url) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/flickrcaptionr/fetcher.rb', line 12

def fetch(url)
  puts "Fetching #{url}..."
  for f in @fetchers
    if f.can_handle_url?(url)
      to_get = f.fetch(url)
      return do_fetch(to_get, f)
    end
  end
  # If none of the matchers hit it but we appear to be a direct image URL, just go get it!
  if /^.+\.(png|gif|jpg|jpeg)$/i.match(url)
    return do_fetch(url, Flickrcaptionr::Fetchers::Base.new)
  end
  raise Flickrcaptionr::RequestNotFetchableException, "Couldn't find any suitable image fetcher for this URL. Try supplying a direct image URL if possible."
end