straight_dope 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
6
+
7
+ /.idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in straight_dope.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,41 @@
1
+ == Straight Dope
2
+
3
+ StraightDope is a gem for getting the URLs of media content from sites such as yfrog and Twitpic. Currently supported services:
4
+
5
+ * yfrog
6
+ * TwitPic
7
+ * Lockerz
8
+ * Twitgoo
9
+ * img.ly
10
+ * Mobypicture
11
+
12
+ Coming soon:
13
+
14
+ * Posterous
15
+
16
+ == Getting Started
17
+
18
+ Add this to your Gemfile and run the +bundle+ command.
19
+
20
+ gem "straight_dope"
21
+
22
+ To use it, simply call extract_media on a string.
23
+
24
+ StraightDope.extract_media("put your urls here")
25
+
26
+ StraightDope will return you an array of URL strings, that you can use directly in your HTML tags.
27
+
28
+ == Future Enhancements
29
+
30
+ * oEmbed support
31
+ * API keys
32
+ * Video support
33
+ * Different sizes
34
+
35
+ == Reference
36
+
37
+ Straight Dope was inspired from these blog posts.
38
+
39
+ http://jasonneylon.wordpress.com/2011/02/01/twitter-picture-previews-with-ruby
40
+
41
+ http://railstips.org/blog/archives/2009/03/04/following-redirects-with-nethttp
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,110 @@
1
+ require 'net/http'
2
+
3
+ module StraightDope
4
+
5
+ class YfrogAdapter
6
+ def self.match?(url)
7
+ !(url =~ /yfrog\.com/).nil?
8
+ end
9
+
10
+ def self.extract(url)
11
+ uri = URI.parse(url)
12
+ id = uri.path
13
+ "http://yfrog.com#{uri.path}:medium"
14
+ end
15
+ end
16
+
17
+ class TwitPicAdapter
18
+ def self.match?(url)
19
+ !(url =~ /twitpic\.com/).nil?
20
+ end
21
+
22
+ def self.extract(url)
23
+ uri = URI.parse(url)
24
+ id = uri.path
25
+ "http://twitpic.com/show/full#{uri.path}"
26
+ end
27
+ end
28
+
29
+ class TwitGooAdapter
30
+ def self.match?(url)
31
+ !(url =~ /twitgoo\.com/).nil?
32
+ end
33
+
34
+ def self.extract(url)
35
+ uri = URI.parse(url)
36
+ id = uri.path
37
+ "http://twitgoo.com/show/img#{uri.path}"
38
+ end
39
+ end
40
+
41
+ class ImglyAdapter
42
+ def self.match?(url)
43
+ !(url =~ /img\.ly/).nil?
44
+ end
45
+
46
+ def self.extract(url)
47
+ uri = URI.parse(url)
48
+ id = uri.path
49
+ "http://img.ly/show/full#{uri.path}"
50
+ end
51
+ end
52
+
53
+ class PlixiAdapter
54
+ def self.match?(url)
55
+ !(url =~ /plixi\.com/).nil?
56
+ end
57
+
58
+ def self.extract(url)
59
+ uri = URI.parse(url)
60
+ "http://api.plixi.com/api/tpapi.svc/imagefromurl?size=large&url=#{url}"
61
+ end
62
+ end
63
+
64
+ class MobyAdapter
65
+ def self.match?(url)
66
+ !(url =~ /moby\.to/).nil?
67
+ end
68
+
69
+ def self.extract(url)
70
+ uri = URI.parse(url)
71
+ id = uri.path
72
+ "http://moby.to/#{id}:full"
73
+ end
74
+ end
75
+
76
+ def self.extract_media(content)
77
+ urls = URI.extract content
78
+ media_urls = []
79
+
80
+ urls.each do |u|
81
+ url = process_redirects(u)
82
+ adapters = [YfrogAdapter, TwitPicAdapter, TwitGooAdapter, ImglyAdapter, PlixiAdapter, MobyAdapter]
83
+ adapters.each do |adapter|
84
+ if adapter.match? url
85
+ media_urls << adapter.extract(url)
86
+ break
87
+ end
88
+ end
89
+ end
90
+
91
+ media_urls
92
+ end
93
+
94
+ def self.process_redirects(u)
95
+ response = Net::HTTP.get_response(URI.parse(u))
96
+ return u unless response.kind_of?(Net::HTTPRedirection)
97
+
98
+ limit = 3
99
+
100
+ while limit > 0 && response.kind_of?(Net::HTTPRedirection)
101
+
102
+ u = response['location']
103
+ response = Net::HTTP.get_response(URI.parse(u))
104
+
105
+ limit -= 1
106
+ end
107
+ u
108
+ end
109
+
110
+ end
@@ -0,0 +1,3 @@
1
+ module StraightDope
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "straight_dope/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "straight_dope"
7
+ s.version = StraightDope::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = "Jaryl Sim"
10
+ s.email = "jaryl@tinkerbox.com.sg"
11
+ s.homepage = "http://www.tinkerbox.com.sg"
12
+ s.summary = "Convert page URLs to media URLs"
13
+ s.description = "Straight Dope gets the actual URL for media content that is usually hidden behind a page."
14
+
15
+ s.rubyforge_project = "straight_dope"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: straight_dope
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - Jaryl Sim
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-22 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Straight Dope gets the actual URL for media content that is usually hidden behind a page.
22
+ email: jaryl@tinkerbox.com.sg
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - .gitignore
31
+ - Gemfile
32
+ - README.rdoc
33
+ - Rakefile
34
+ - lib/straight_dope.rb
35
+ - lib/straight_dope/version.rb
36
+ - straight_dope.gemspec
37
+ homepage: http://www.tinkerbox.com.sg
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ hash: 3
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project: straight_dope
66
+ rubygems_version: 1.7.2
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Convert page URLs to media URLs
70
+ test_files: []
71
+