Skip to content

Instantly share code, notes, and snippets.

@ezodude
Created March 27, 2011 11:49
Show Gist options
  • Save ezodude/889142 to your computer and use it in GitHub Desktop.
Save ezodude/889142 to your computer and use it in GitHub Desktop.
FlickrService
# encoding: utf-8
class FlickrService
def initialize
@api_key = YAML.load_file(File.join(Dir.getwd, "config/flickr_service.yml"))["api_key"]
end
def photo_info_for(flickr_image_uri)
begin
result = {}
res = RestClient.get("http://api.flickr.com/services/rest/?method=flickr.photos.getInfo" \
+ "&api_key=#{@api_key}" \
+ "&photo_id=#{get_photo_id_from(flickr_image_uri)}")
doc = Nokogiri::XML(res.body)
owner = doc.at("rsp photo owner")
result[:username] = owner.attribute('username').value if owner && owner.attribute('username')
result
rescue RestClient::Exception => e
{}
end
end
def photo_sources_and_sizes_for(flickr_image_uri)
begin
res = RestClient.get("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes" \
+ "&api_key=#{@api_key}" \
+ "&photo_id=#{get_photo_id_from(flickr_image_uri)}")
doc = Nokogiri::XML(res.body)
size_nodes = doc.search("rsp sizes size")
return [] unless size_nodes
size_nodes.map do |size_node|
{
:width => size_node.attribute('width').value.to_i,
:height => size_node.attribute('height').value.to_i,
:source => size_node.attribute('source').value
}
end
rescue RestClient::Exception => e
[]
end
end
def squared_photo_for(flickr_image_uri)
res = RestClient.get("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes" \
+ "&api_key=#{@api_key}" \
+ "&photo_id=#{get_photo_id_from(flickr_image_uri)}")
doc = Nokogiri::XML(res.body)
square_node = doc.search("rsp sizes size[@label=Square]").first
return nil unless square_node
square_node.attribute('source').value
end
private
def get_photo_id_from(flickr_image_uri)
flickr_image_uri.split("/").last
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment