Skip to content

Instantly share code, notes, and snippets.

@cgallagher
Created March 13, 2013 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgallagher/5149857 to your computer and use it in GitHub Desktop.
Save cgallagher/5149857 to your computer and use it in GitHub Desktop.
Include this in your services directory of your Rails application in order to pull in and parse a hashtag RSS feed from Instagram.
class Instagrammer
cattr_accessor :config, :url
class << self
def perform(url, options = {})
defaults = {:expires_in => 15.minutes }
self.config = defaults.merge(options)
self.url = url
doc = fetch_feed
instagram_images = doc.xpath('//item').map do |i|
{'title' => i.xpath('title').inner_text,
'link' => i.xpath('link').inner_text,
'description' => i.xpath('description').inner_text,
'image_url' => i.xpath('//content')[0]['url'],
'thumbnail_url' => i.xpath('//thumbnail')[0]['url'],
'author' => i.xpath('//credit').inner_text
}
end
instagram_images
end
end
private
def self.fetch_feed
unless doc = Nokogiri::HTML(Rails.cache.read(self.url)) and self.config[:cache]
doc = Nokogiri::HTML(open(self.url))
doc = doc.remove_namespaces!
Rails.cache.write(url, doc.to_s, :expires_in => self.config[:expires_in]) if self.config[:cache]
puts "No cache or expired - grabbed a fresh copy of the feed."
else
puts "Hit a cached version of the feed."
end
doc
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment