Sha256: a1862357838d565cd0d23ceea3722fa4163a726479a02a8429c6c305fa14dd56
Contents?: true
Size: 1.96 KB
Versions: 1
Compression:
Stored size: 1.96 KB
Contents
module AppConfig module FlickrSearch def self.options(config) config['flickr'].each do |key, val| const_set(key, val) end end def search_and_save_flickr_image(text) uri = URI(search_flickr_for(text)) download_and_write_to_file uri end def search_flickr_for(search_term) response_body = build_query_for(search_term).read_body response_data = response_body.to_s.match(/\[([^\}]+)\}/)[0] .sub(/^\[/, '') json_response = JSON.parse response_data construct_image_url_from json_response rescue JSON::ParserError puts "\nJSON Parser Error Detected. Retrying ..." search_flickr_for(new_word) end def image_list Dir[TEMP_DIR + '/*'] end private def download_and_write_to_file(uri) Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| filepath = File.join(TEMP_DIR, File.basename(uri.path)) open(filepath, 'wb') do |file| file.write http.get(uri).read_body file.close end end rescue URI::InvalidURIError puts 'URI::InvalidURIError Detected' end def build_query_for(word) uri = uri_for word Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| request = Net::HTTP::Get.new uri http.request request end end def uri_for(search_text) URI( uri_builder( url: API_URL, format: API_FORMAT, sort: API_SORT, method: API_METHOD, text: search_text, tag_mode: :all, api_key: API_KEY ) ) end def uri_builder(params = {}) url = params[:url] params.reject! { |k, _| k == :url } url + params.each_pair.map { |k, v| "#{k}=#{v}" }.join('&') end def construct_image_url_from(json) <<-URL.gsub(/\s+/, '').strip https://farm#{json['farm']}.staticflickr.com/ #{json['server']}/#{json['id']}_#{json['secret']}.jpg URL end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
flickr_collager-0.0.1 | lib/app_config/flickr_search.rb |