twimage 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,18 +4,70 @@ require 'httparty'
4
4
  require 'tempfile'
5
5
 
6
6
  $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
7
-
8
7
  require 'twimage/version'
9
- require 'twimage/base'
10
- require 'twimage/twitpic'
11
- require 'twimage/yfrog'
8
+ require 'twimage/image'
12
9
 
13
10
  module Twimage
14
11
 
15
12
  class ServiceURLInvalid < StandardError; end # thrown if the service_url returns a 404
16
- class ImageNotFound < StandardError; end # thrown if the service_url doesn't contain an image
13
+ class ImageNotFound < StandardError; end # thrown if the service_url doesn't contain an expected image tag
17
14
  class ImageURLInvalid < StandardError; end # thrown if the image_url found in the service_url returns a 404
15
+
16
+ SERVICES = [{ :name => :twitpic,
17
+ :service_match => /twitpic\.com/,
18
+ :full_url_modifier => lambda { |url| url + '/full' },
19
+ :image_css_match => 'body > img' },
20
+ { :name => :yfrog,
21
+ :service_match => /yfrog\.com/,
22
+ :full_url_modifier => lambda { |url| url.gsub(/\.com/, '.com/z') },
23
+ :image_css_match => '#the-image img' }]
24
+
25
+ def self.get(url)
26
+ service_url = HTTParty.get(url).request.path.to_s # first point HTTParty at this URL and follow any redirects to get to the final page
27
+ service = find_service(service_url) # check the resulting service_url for which service we're hitting
28
+ 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
29
+ image_url = get_image_url(service, full_res_service_url) # get the URL to the image
30
+ image = get_image(image_url) # get the image itself
31
+
32
+ return Image.new(:service => service[:name], :service_url => service_url, :image_url => image_url, :image => image)
33
+ end
34
+
35
+
36
+ # figure out which service this is by matching against regexes
37
+ def self.find_service(url)
38
+ return SERVICES.find do |service|
39
+ url.match(service[:service_match])
40
+ end
41
+ end
42
+
43
+
44
+ # tear apart the HTML on the returned service page and find the source of the image
45
+ def self.get_image_url(service, url)
46
+ # get the content of the image page
47
+ begin
48
+ image_tag = Nokogiri::HTML(open(url)).css(service[:image_css_match]).first
49
+ rescue OpenURI::HTTPError
50
+ raise ServiceURLInvalid, "The service URL #{url} was not found (returned a 404)"
51
+ end
52
+
53
+ # get the URL to the actual image file
54
+ if image_tag
55
+ return image_tag['src']
56
+ else
57
+ raise ImageNotFound, "The service URL #{url} did not contain an identifiable image"
58
+ end
59
+ end
60
+
18
61
 
62
+ # download the actual image and put into a tempfile
63
+ def self.get_image(url)
64
+ # get the image itself
65
+ response = HTTParty.get(url)
66
+ if response.code == 200
67
+ return response.body.force_encoding('utf-8')
68
+ else
69
+ raise ImageURLInvalid, "The image_url #{url} was not found (returned a 404)"
70
+ end
71
+ end
72
+
19
73
  end
20
-
21
- #puts Twimage::Yfrog.new('http://yfrog.com/hsjdhxqj').inspect
@@ -0,0 +1,17 @@
1
+ module Twimage
2
+ class Image
3
+
4
+ attr_reader :service, :service_url, :image_url, :tempfile
5
+
6
+ def initialize(options)
7
+ @service = options[:service]
8
+ @service_url = options[:service_url]
9
+ @image_url = options[:image_url]
10
+
11
+ extension = @image_url.match(/(\.\w+)(\?|$)/)[1]
12
+ @tempfile = Tempfile.new(['twimage', extension])
13
+ @tempfile << options[:image]
14
+ end
15
+
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module Twimage
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: twimage
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.11
5
+ version: 0.0.12
6
6
  platform: ruby
7
7
  authors:
8
8
  - Rob Cameron
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-11 00:00:00 -04:00
13
+ date: 2011-05-14 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -51,10 +51,8 @@ files:
51
51
  - README.md
52
52
  - Rakefile
53
53
  - lib/twimage.rb
54
- - lib/twimage/base.rb
55
- - lib/twimage/twitpic.rb
54
+ - lib/twimage/image.rb
56
55
  - lib/twimage/version.rb
57
- - lib/twimage/yfrog.rb
58
56
  - twimage.gemspec
59
57
  has_rdoc: true
60
58
  homepage: ""
@@ -1,18 +0,0 @@
1
- module Twimage
2
- class Base
3
-
4
- attr_accessor :tempfile, :image_url, :service_url, :extension
5
-
6
- def initialize(service_url)
7
- response = HTTParty.get(@image_url)
8
- @extension = @image_url.match(/(\.\w+)(\?|$)/)[1]
9
- if response.code == 200
10
- @tempfile = Tempfile.new(['twimage',@extension])
11
- @tempfile << response.body.force_encoding('utf-8')
12
- else
13
- raise ImageURLInvalid, "The image_url #{@image_url} was not found (returned a 404)"
14
- end
15
- end
16
-
17
- end
18
- end
@@ -1,22 +0,0 @@
1
- module Twimage
2
- class Twitpic < Twimage::Base
3
-
4
- def initialize(service_url)
5
- @service_url = service_url
6
- full_res_service_url = @service_url + '/full'
7
- begin
8
- image_tag = Nokogiri::HTML(open(full_res_service_url)).css('body > img').first
9
- rescue OpenURI::HTTPError
10
- raise ServiceURLInvalid, "The service URL #{@service_url} was not found (returned a 404)"
11
- end
12
-
13
- if image_tag
14
- @image_url = image_tag['src']
15
- super
16
- else
17
- raise ImageNotFound, "The service URL #{@service_url} did not contain an identifiable image"
18
- end
19
- end
20
-
21
- end
22
- end
@@ -1,22 +0,0 @@
1
- module Twimage
2
- class Yfrog < Twimage::Base
3
-
4
- def initialize(service_url)
5
- @service_url = service_url
6
- full_res_service_url = @service_url.gsub(/\.com/, '.com/z')
7
- begin
8
- image_tag = Nokogiri::HTML(open(full_res_service_url)).css('#the-image img').first
9
- rescue OpenURI::HTTPError
10
- raise ServiceURLInvalid, "The service URL #{@service_url} was not found (returned a 404)"
11
- end
12
-
13
- if image_tag
14
- @image_url = image_tag['src']
15
- super
16
- else
17
- raise ImageNotFound, "The service URL #{@service_url} did not contain an identifiable image"
18
- end
19
- end
20
-
21
- end
22
- end