Module: Twimage
- Defined in:
- lib/twimage.rb,
lib/twimage/image.rb,
lib/twimage/version.rb
Defined Under Namespace
Classes: Image, ImageNotFound, ImageURLInvalid, ServiceURLInvalid
Constant Summary collapse
- USER_AGENT =
"Twimage #{Twimage::VERSION} http://github.com/cannikin/twimage"
- SERVICES =
[{ :name => :twitpic, :service_match => /twitpic\.com/, :full_url_modifier => lambda { |url| url + '/full' }, :image_css_match => 'body > img' }, { :name => :yfrog, :service_match => /yfrog\.com/, :full_url_modifier => lambda { |url| url.gsub(/\.com/, '.com/z') }, :image_css_match => '#the-image img' }, { :name => :instagram, :service_match => [/instagr\.am/, /instagram\.com/], :image_css_match => '.photo'}, { :name => :lockerz, :service_match => /lockerz\.com/, :image_css_match => '#photo' } ]
- VERSION =
"0.1.1"
Class Method Summary collapse
- .enforce_protocol(url) ⇒ Object
-
.find_service(url) ⇒ Object
figure out which service this is by matching against regexes.
- .get(url) ⇒ Object
-
.get_image(url) ⇒ Object
download the actual image and put into a tempfile.
-
.get_image_url(service, url) ⇒ Object
tear apart the HTML on the returned service page and find the source of the image.
Class Method Details
.enforce_protocol(url) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/twimage.rb', line 80 def self.enforce_protocol(url) if url =~ /^http/ url elsif url =~ /^\/\// "http:" + url end end |
.find_service(url) ⇒ Object
figure out which service this is by matching against regexes
54 55 56 57 58 59 60 |
# File 'lib/twimage.rb', line 54 def self.find_service(url) return SERVICES.find do |service| [service[:service_match]].flatten.find do |regex| url.match(regex) end end end |
.get(url) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/twimage.rb', line 33 def self.get(url) service_url = HTTParty.get(url, :headers => { 'User-Agent' => USER_AGENT }).request.path.to_s # first point HTTParty at this URL and follow any redirects to get to the final page service = find_service(service_url) # check the resulting service_url for which service we're hitting full_res_service_url = service[:full_url_modifier] ? service[:full_url_modifier].call(service_url) : service_url # get the full res version of the service_url #debugger image_url = get_image_url(service, full_res_service_url) # get the URL to the image #debugger image = get_image(image_url) # get the image itself #debugger p({ :url => url, :image_url => image_url, :service_url => service_url }) return Image.new(:service => service[:name], :service_url => service_url, :image_url => image_url, :image => image) end |
.get_image(url) ⇒ Object
download the actual image and put into a tempfile
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/twimage.rb', line 90 def self.get_image(url) #debugger # get the image itself response = HTTParty.get(url, :headers => { 'User-Agent' => USER_AGENT }) if response.code == 200 return response.body.force_encoding('utf-8') else raise ImageURLInvalid, "The image_url #{url} was not found (returned a 404)" end end |
.get_image_url(service, url) ⇒ Object
tear apart the HTML on the returned service page and find the source of the image
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/twimage.rb', line 64 def self.get_image_url(service, url) # get the content of the image page begin image_tag = Nokogiri::HTML(open(url, 'User-Agent' => USER_AGENT)).css(service[:image_css_match]).first rescue OpenURI::HTTPError raise ServiceURLInvalid, "The service URL #{url} was not found (returned a 404)" end # get the URL to the actual image file if image_tag return enforce_protocol(image_tag['src']) else raise ImageNotFound, "The service URL #{url} did not contain an identifiable image" end end |