Class: Flckr::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/flckr/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Api

Returns a new instance of Api.



10
11
12
# File 'lib/flckr/api.rb', line 10

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#download(url, dir) ⇒ Object



22
23
24
25
# File 'lib/flckr/api.rb', line 22

def download(url, dir)
  response = conn.get url
  File.open("#{dir}/#{SecureRandom.hex(5)}.jpg", 'wb') { |fp| fp.write(response.body) }
end

#download_by(keywords, dir) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/flckr/api.rb', line 14

def download_by(keywords, dir)
  ten_keywords = Dict.make_ten_words keywords
  urls = search_all ten_keywords
  urls.map do |url|
    download(url, dir)
  end
end

#generate_url(photo_id:, server_id:, farm_id:, secret:) ⇒ Object



27
28
29
# File 'lib/flckr/api.rb', line 27

def generate_url(photo_id:, server_id:, farm_id:, secret:)
  "https://farm#{farm_id}.staticflickr.com/#{server_id}/#{photo_id}_#{secret}_m.jpg"
end

#search(keywords) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/flckr/api.rb', line 38

def search(keywords)
  [].tap do |responses|
    client = conn
    client.in_parallel do
      keywords.each do |keyword|
        responses << client.get('/services/rest', params(@api_key, keyword))
      end
    end
  end
end

#search_all(keywords) ⇒ Object



31
32
33
34
35
36
# File 'lib/flckr/api.rb', line 31

def search_all(keywords)
  search(keywords)
    .map { |r| parse r }
    .reject(&:nil?)
    .map { |e| generate_url e }
end