multi_shorten 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +51 -0
- data/lib/multi_shorten/client.rb +48 -0
- data/lib/multi_shorten/url_shorteners.rb +157 -0
- data/lib/multi_shorten.rb +4 -0
- metadata +80 -0
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
README
|
2
|
+
======
|
3
|
+
|
4
|
+
Use multiple url shorteners with a single gem
|
5
|
+
|
6
|
+
Install
|
7
|
+
-------
|
8
|
+
|
9
|
+
<code>gem install multi_shorten</code>
|
10
|
+
|
11
|
+
Usage
|
12
|
+
-----
|
13
|
+
<code>
|
14
|
+
require 'multi_shorten'
|
15
|
+
client = MultiShorten::Client.new
|
16
|
+
</code>
|
17
|
+
<b>For single shortener apis</b>
|
18
|
+
<code>
|
19
|
+
client.shorten({:mode => "single", :url => "http://www.google.com", :shortener => "b54"})
|
20
|
+
</code>
|
21
|
+
|
22
|
+
<b>Sample response for this request</b>
|
23
|
+
<code>
|
24
|
+
{:status => :success, :short_url => "http://b54.in/9o"}
|
25
|
+
</code>
|
26
|
+
|
27
|
+
<b>For multiple apis</b>
|
28
|
+
<code>
|
29
|
+
client.shorten({:mode => "multiple", :url => "http://www.google.com", :shorteners => ["b54", "qr_cx"]})
|
30
|
+
</code>
|
31
|
+
|
32
|
+
<b>Sample response for this request </b>
|
33
|
+
<code>
|
34
|
+
{"b54" => {:status => :fail }, "qr_cx" => {:status => :success, :short_url => "http://qr.cx/9o"}}
|
35
|
+
</code>
|
36
|
+
|
37
|
+
Available Shortener Codes
|
38
|
+
--------------------
|
39
|
+
|
40
|
+
[b54](http://b54.in)
|
41
|
+
[linkee](http://linkee.com)
|
42
|
+
[goo_gl](http://goo.gl)
|
43
|
+
[is_gd](htpp://is.gd)
|
44
|
+
[jumbo_tweet](http://jmb.tw/)
|
45
|
+
[meta_mark](http://metamark.net)
|
46
|
+
[mt_ny](http://mtny.mobi")
|
47
|
+
[qr_cx](http://qr.cx)
|
48
|
+
[shortr](http://shortr.info)
|
49
|
+
|
50
|
+
Use the above codes to shorten your urls with the respective service
|
51
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require_relative 'url_shorteners'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
module MultiShorten
|
4
|
+
# Client class for MultiShorten
|
5
|
+
# initialize the client with
|
6
|
+
# client = MultiShorten::Client.new
|
7
|
+
class Client
|
8
|
+
# The only method required to use all the features
|
9
|
+
# @param [Hash] params hash with all the required parameters
|
10
|
+
# @return [Hash] the result hash with status and if successful, the shortened url as well.
|
11
|
+
def shorten params
|
12
|
+
raise "Parameter should be a hash" unless params.is_a?(Hash)
|
13
|
+
return missing_parameter_error(:url) if params[:url].nil?
|
14
|
+
return missing_parameter_error(:mode) if params[:mode].nil?
|
15
|
+
case params[:mode]
|
16
|
+
when "single"
|
17
|
+
return missing_parameter_error(:shortener) if params[:shortener].nil?
|
18
|
+
single(params[:shortener], params[:url])
|
19
|
+
when "multiple"
|
20
|
+
return missing_parameter_error(:shorteners) if params[:shorteners].nil
|
21
|
+
multiple(params[:shorteners], params[:url])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def single shortener, url
|
28
|
+
"MultiShorten::#{shortener.camelize}".constantize.shorten(url) rescue fail
|
29
|
+
end
|
30
|
+
|
31
|
+
def multiple shorteners, url
|
32
|
+
result = {}
|
33
|
+
shorteners.each do |shortener|
|
34
|
+
result[shortener] = single(shortener, url)
|
35
|
+
end
|
36
|
+
result
|
37
|
+
end
|
38
|
+
|
39
|
+
def missing_parameter_error param
|
40
|
+
raise "Missing required parameter: #{param}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def fail
|
44
|
+
{:status => :fail}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'pp'
|
4
|
+
require 'json'
|
5
|
+
# The main MultiShorten module
|
6
|
+
module MultiShorten
|
7
|
+
|
8
|
+
# Base class from which all other classes are derived
|
9
|
+
class UrlShortener
|
10
|
+
# HTTPArty is included here so that its appicable to all the sub classes
|
11
|
+
include HTTParty
|
12
|
+
|
13
|
+
# Returns shortened version of the url for the corresponding. This is the abstract class
|
14
|
+
# @param [String] url the url to be shortened
|
15
|
+
# @return [Hash] the resulting hash with status -> either :success or :fail. If success it also includes the :short_url
|
16
|
+
def self.shorten url
|
17
|
+
end
|
18
|
+
|
19
|
+
# The hash that is returned when there is a failure
|
20
|
+
def fail
|
21
|
+
{ :status => :fail }
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
# URL Shortener for B54
|
27
|
+
class B54 < UrlShortener
|
28
|
+
|
29
|
+
base_uri "http://b54.in"
|
30
|
+
format :json
|
31
|
+
|
32
|
+
def self.shorten url
|
33
|
+
response = get "/api", :query => {:action => "shorturl", :url => URI.encode(url), :format => "json"} rescue return fail
|
34
|
+
if response["shorturl"]
|
35
|
+
{:status => :success, :short_url => URI.unescape(response["shorturl"])}
|
36
|
+
else
|
37
|
+
fail
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
# URL Shortener for Linkee
|
44
|
+
class Linkee < UrlShortener
|
45
|
+
base_uri "http://api.linkee.com"
|
46
|
+
format :json
|
47
|
+
def self.shorten url
|
48
|
+
response = get "/1.0/shorten", :query => {:input => URI.encode(url)}
|
49
|
+
if response["status"]["code"] == 200
|
50
|
+
{:status => :success, :short_url => response["result"]}
|
51
|
+
else
|
52
|
+
{:status => :fail}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# URL Shortener for goo.gl
|
58
|
+
class GooGl < UrlShortener
|
59
|
+
base_uri "https://www.googleapis.com/urlshortener"
|
60
|
+
format :json
|
61
|
+
def self.shorten url
|
62
|
+
response = post "/v1/url", :headers => {"Content-Type" => "application/json"}, :body => JSON.dump({:longUrl => URI.encode(url)})
|
63
|
+
unless response.parsed_response["id"].nil?
|
64
|
+
{ :status => :success, :short_url => response.parsed_response["id"] }
|
65
|
+
else
|
66
|
+
fail
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# URL Shortener for is.gd
|
72
|
+
class IsGd < UrlShortener
|
73
|
+
base_uri "http://is.gd/"
|
74
|
+
format :json
|
75
|
+
def self.shorten url
|
76
|
+
response = get "/create.php", :query => {:format => "json", :url => URI.encode(url)}
|
77
|
+
unless response.parsed_response["shorturl"].nil?
|
78
|
+
{ :status => :success, :short_url => response.parsed_response["shorturl"] }
|
79
|
+
else
|
80
|
+
fail
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# URL Shortener for Jumbo Tweet
|
86
|
+
class JumboTweet < UrlShortener
|
87
|
+
base_uri "http://jmb.tw"
|
88
|
+
format :plain
|
89
|
+
def self.shorten url
|
90
|
+
response = get "/api/create", :query => { :newurl => URI.encode(url)}
|
91
|
+
if response.match("^http://jmb.tw/.+")
|
92
|
+
{ :status => :success, :short_url => response.parsed_response }
|
93
|
+
else
|
94
|
+
fail
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# URL Shortener for metamark
|
100
|
+
class MetaMark < UrlShortener
|
101
|
+
base_uri "http://metamark.net"
|
102
|
+
format :plain
|
103
|
+
def self.shorten url
|
104
|
+
response = get "/api/rest/simple", :query => { :long_url => URI.encode(url)}
|
105
|
+
if response.match("^http://xrl.us/.+")
|
106
|
+
{ :status => :success, :short_url => response.parsed_response }
|
107
|
+
else
|
108
|
+
fail
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# URL Shortener for mtny.mobi
|
114
|
+
class MtNy < UrlShortener
|
115
|
+
base_uri "http://mtny.mobi"
|
116
|
+
format :json
|
117
|
+
def self.shorten url
|
118
|
+
response = get "/api", :query => {:type => "json", :source => "web", :url => URI.encode(url)}
|
119
|
+
unless response.parsed_response["url"].nil?
|
120
|
+
{ :status => :success, :short_url => response.parsed_response["url"] }
|
121
|
+
else
|
122
|
+
fail
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# URL Shortener for qr.cx
|
128
|
+
class QrCx < UrlShortener
|
129
|
+
base_uri "http://qr.cx"
|
130
|
+
format :plain
|
131
|
+
|
132
|
+
def self.shorten url
|
133
|
+
response = get "/api/?", :query => {:longurl => URI.encode(url)}
|
134
|
+
if response.match("^http://qr.cx/.+")
|
135
|
+
{ :status => :success, :short_url => response.parsed_response }
|
136
|
+
else
|
137
|
+
fail
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
# URL Shortener for shortr.info
|
144
|
+
class Shortr < UrlShortener
|
145
|
+
base_uri "http://shortr.info"
|
146
|
+
format :json
|
147
|
+
def self.shorten url
|
148
|
+
response = get "/make-shortr.php", :query => {:format => "json", :url => URI.encode(url)}
|
149
|
+
unless response.parsed_response["shortr"]["result"]["created"].nil?
|
150
|
+
{ :status => :success, :short_url => response.parsed_response["shortr"]["result"]["created"] }
|
151
|
+
else
|
152
|
+
fail
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: multi_shorten
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rony Varghese
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: active_support
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Use multiple url shorteners to shorten single or multiple urls
|
47
|
+
email: ronyv250289@gmail.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- lib/multi_shorten/client.rb
|
53
|
+
- lib/multi_shorten/url_shorteners.rb
|
54
|
+
- lib/multi_shorten.rb
|
55
|
+
- README.md
|
56
|
+
homepage: http://rubygems.org/gems/multi_shorten
|
57
|
+
licenses: []
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.8.24
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Multiple URL Shorteners in one place
|
80
|
+
test_files: []
|