url_shortener 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -1
- data/README.rdoc +25 -1
- data/lib/url_shortener/client.rb +34 -4
- data/lib/url_shortener/error.rb +4 -0
- data/lib/url_shortener.rb +1 -0
- data/spec/url_shortener/client_spec.rb +107 -18
- metadata +2 -2
data/History.txt
CHANGED
data/README.rdoc
CHANGED
@@ -9,7 +9,11 @@ To use the url_shortener gem you need a bit.ly login and Api key.
|
|
9
9
|
|
10
10
|
==USAGE
|
11
11
|
|
12
|
-
|
12
|
+
===To Shorten Urls
|
13
|
+
|
14
|
+
* authorize = UrlShortener::Authorize.new 'your_bitly_login', 'your_bitly_api_key'
|
15
|
+
|
16
|
+
* For example; authorize = UrlShortener::Authorize.new 'bitlyapidemo', 'R_0da49e0a9118ff35f52f629d2d71bf07'
|
13
17
|
|
14
18
|
* client = UrlShortener::Client.new authorize
|
15
19
|
|
@@ -23,6 +27,26 @@ OR
|
|
23
27
|
|
24
28
|
* results.each {|result| puts result['shortUrl']}
|
25
29
|
|
30
|
+
==To Expand Urls
|
31
|
+
|
32
|
+
* authorize = UrlShortener::Authorize.new 'your_bitly_login', 'your_bitly_api_key'
|
33
|
+
|
34
|
+
* client = UrlShortener::Client.new authorize
|
35
|
+
|
36
|
+
* result = client.expand(:shortUrl => 'http://bit.ly/1RmnUT')
|
37
|
+
|
38
|
+
OR
|
39
|
+
|
40
|
+
* result = client.expand(:hash => '2bYgqR')
|
41
|
+
|
42
|
+
* result['longUrl']
|
43
|
+
|
44
|
+
OR
|
45
|
+
|
46
|
+
* results = client.shorten('http://www.yahoo.com', 'http://www.google.com', 'http://github.com')
|
47
|
+
|
48
|
+
* results.each {|result| puts result['shortUrl']}
|
49
|
+
|
26
50
|
|
27
51
|
==Tests
|
28
52
|
|
data/lib/url_shortener/client.rb
CHANGED
@@ -8,16 +8,39 @@ module UrlShortener
|
|
8
8
|
validated?
|
9
9
|
end
|
10
10
|
|
11
|
-
|
11
|
+
# To shorten multiple URLs in a single API call, pass multiple urls
|
12
|
+
# e.g. shorten('http://www.google.com', 'http://www.bit.ly')
|
13
|
+
def shorten(*urls)
|
12
14
|
# HTTParty accepts a hash for url parameters but if more than one parameters are passed
|
13
15
|
# to bitly then the url needs to have longUrl parameter multiple times but we cant have
|
14
16
|
# duplicate keys in a hash hence this solution
|
15
|
-
|
17
|
+
urls.collect!{|url| CGI.escape(url)}
|
18
|
+
end_point_with_params = "#{end_point('shorten')}?longUrl=#{urls.join('&longUrl=')}"
|
16
19
|
interface(end_point_with_params).get['nodeKeyVal']
|
17
20
|
end
|
18
21
|
|
19
|
-
|
20
|
-
|
22
|
+
# Provide parameter as a key value pair of existing short url or its hash
|
23
|
+
# e.g. expand :shortUrl => 'http://bit.ly/15DlK' OR :hash => '31IqMl'
|
24
|
+
def expand(option)
|
25
|
+
check_request_parameters(option)
|
26
|
+
/^.*\/(.*)$/.match option[:shortUrl] if option[:shortUrl]
|
27
|
+
request_param = option[:hash] ? option[:hash] : $1
|
28
|
+
result = interface(nil, {:rest_url => end_point('expand')}.merge(option)).get[request_param]
|
29
|
+
return result['longUrl'] if result
|
30
|
+
end
|
31
|
+
|
32
|
+
# If a complex url need to be passed then use end_point_with_params else
|
33
|
+
# set first parameter to nil while invoking this method and
|
34
|
+
# pass rest_url option e.g.
|
35
|
+
# interface(nil, {:rest_url => end_point('expand'), :another_key => 'value'})
|
36
|
+
def interface(end_point_with_params, options={})
|
37
|
+
if end_point_with_params.nil? && options[:rest_url].nil?
|
38
|
+
raise "You must provide either the endpoint as the first parameter or set the :rest_url options"
|
39
|
+
end
|
40
|
+
rest_url = end_point_with_params ? end_point_with_params : options[:rest_url]
|
41
|
+
options.delete(:rest_url)
|
42
|
+
params = common_query_parameters.merge!(options)
|
43
|
+
UrlShortener::Interface.new(rest_url, :query => params)
|
21
44
|
end
|
22
45
|
|
23
46
|
def common_query_parameters
|
@@ -34,6 +57,13 @@ module UrlShortener
|
|
34
57
|
|
35
58
|
private
|
36
59
|
|
60
|
+
def check_request_parameters(option)
|
61
|
+
unless (option[:hash] || option[:shortUrl])
|
62
|
+
raise UrlShortener::IncompleteRequestParameter, "Either hash or the shorUrl is required to complete the operation."
|
63
|
+
end
|
64
|
+
true
|
65
|
+
end
|
66
|
+
|
37
67
|
def validated?
|
38
68
|
valid_authorize_attribute?
|
39
69
|
end
|
data/lib/url_shortener/error.rb
CHANGED
data/lib/url_shortener.rb
CHANGED
@@ -83,46 +83,135 @@ describe UrlShortener::Client do
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
-
describe "
|
86
|
+
describe "API actions" do
|
87
87
|
before(:each) do
|
88
88
|
authorize = UrlShortener::Authorize.new 'login', 'key'
|
89
89
|
@client = UrlShortener::Client.new(authorize)
|
90
90
|
@interface = stub('UrlShortener::Interface', :get => {})
|
91
91
|
@client.stub!(:interface).and_return(@interface)
|
92
92
|
end
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
93
|
+
describe "#shorten" do
|
94
|
+
before(:each) do
|
95
|
+
@url = 'http://www.domain.com/path?params=value'
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should do the url escaping" do
|
99
|
+
CGI.should_receive(:escape).with(@url)
|
100
|
+
@client.shorten(@url)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should get the end point" do
|
104
|
+
@client.should_receive(:end_point)
|
105
|
+
@client.shorten(@url)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should use the interface to connect to bitly" do
|
109
|
+
@client.should_receive(:interface).and_return(@interface)
|
110
|
+
@client.shorten(@url)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should get the data using interface" do
|
114
|
+
@interface.should_receive(:get)
|
115
|
+
@client.shorten(@url)
|
116
|
+
end
|
117
|
+
|
102
118
|
end
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
119
|
+
|
120
|
+
describe "#expand" do
|
121
|
+
before(:each) do
|
122
|
+
@hash = 'qweWE'
|
123
|
+
@short_url = 'http://bit.ly/wesSD'
|
124
|
+
@end_point = 'http://api.bit.ly/expand'
|
125
|
+
@client.stub!(:end_point).and_return(@end_point)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should raise IncompleteRequestParameter when key value pair for neither hash nor shortUrl is not present" do
|
129
|
+
lambda {@client.expand(:invalid => 'anyvalue')}.should raise_error(UrlShortener::IncompleteRequestParameter)
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should get the end point" do
|
133
|
+
@client.should_receive(:end_point)
|
134
|
+
@client.expand(:hash => @hash)
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should use the interface to connect to bitly and pass the hash when url hash is used" do
|
138
|
+
@client.should_receive(:interface).with(nil, {:rest_url => @end_point, :hash => 'qweWE'}).and_return(@interface)
|
139
|
+
@client.expand(:hash => @hash)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should use the interface to connect to bitly and pass the shortUrl when shortUrl is used" do
|
143
|
+
@client.should_receive(:interface).with(nil, {:rest_url => @end_point, :shortUrl => 'http://bit.ly/wesSD'}).and_return(@interface)
|
144
|
+
@client.expand(:shortUrl => @short_url)
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should get the data using interface" do
|
148
|
+
@interface.should_receive(:get)
|
149
|
+
@client.expand(:hash => @hash)
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should return the long url of the hash provided" do
|
153
|
+
@interface.should_receive(:get).and_return('qweWE' => {'longUrl' => 'http://www.goog.com'})
|
154
|
+
@client.expand(:hash => @hash).should eql('http://www.goog.com')
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should return the long url for the shortUrl provided" do
|
158
|
+
@interface.should_receive(:get).and_return('wesSD' => {'longUrl' => 'http://www.goog.com'})
|
159
|
+
@client.expand(:shortUrl => @short_url).should eql('http://www.goog.com')
|
160
|
+
end
|
161
|
+
|
107
162
|
end
|
108
|
-
|
109
163
|
end
|
110
164
|
|
111
165
|
describe "#interface" do
|
112
166
|
before(:each) do
|
113
167
|
authorize = UrlShortener::Authorize.new 'login', 'key'
|
114
168
|
@client = UrlShortener::Client.new(authorize)
|
115
|
-
@
|
169
|
+
@common_query_parameters = {}
|
170
|
+
@client.stub!(:common_query_parameters).and_return(@common_query_parameters)
|
171
|
+
@first_parameter = '/some/uri'
|
172
|
+
@url = '/a/url'
|
116
173
|
end
|
117
174
|
|
118
175
|
it "should initialize the interface object" do
|
119
176
|
UrlShortener::Interface.should_receive(:new).with('/some/uri', :query => {})
|
120
|
-
@client.interface(
|
177
|
+
@client.interface(@first_parameter)
|
121
178
|
end
|
122
179
|
|
123
180
|
it "should get the common query parameters" do
|
124
181
|
@client.should_receive(:common_query_parameters)
|
125
|
-
@client.interface(
|
182
|
+
@client.interface(@first_parameter)
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should use the rest_url key from the options if first_parameter is set to nil" do
|
186
|
+
UrlShortener::Interface.should_receive(:new).with('/a/url', :query => {})
|
187
|
+
@client.interface(nil, {:rest_url => @url})
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should use the first parameter if rest_url option as well as the first parameter is provided" do
|
191
|
+
UrlShortener::Interface.should_receive(:new).with('/some/uri', :query => {})
|
192
|
+
@client.interface(@first_parameter, {:rest_url => @url})
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should raise an error when neither the first parameter is set nor the rest_url options" do
|
196
|
+
lambda { @client.interface(nil, {}) }.should raise_error("You must provide either the endpoint as the first parameter or set the :rest_url options")
|
197
|
+
end
|
198
|
+
|
199
|
+
it "should remove the rest_url key value from the options" do
|
200
|
+
options = {:rest_url => @url}
|
201
|
+
options.should_receive(:delete).with(:rest_url)
|
202
|
+
@client.interface(@first_parameter, options)
|
203
|
+
end
|
204
|
+
|
205
|
+
it "should merge the rest of the options passed to the common_query_parameters" do
|
206
|
+
options = {:rest_url => @url, :tt => 'val'}
|
207
|
+
@common_query_parameters.should_receive(:merge!).with(options)
|
208
|
+
@client.interface(@first_parameter, options)
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should keys for the parameters that are passed in the options" do
|
212
|
+
options = {:rest_url => @url, :tt => 'val'}
|
213
|
+
UrlShortener::Interface.should_receive(:new).with('/a/url', :query => {:tt => 'val'})
|
214
|
+
@client.interface(nil, options)
|
126
215
|
end
|
127
216
|
end
|
128
217
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: url_shortener
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nasir Jamal
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-09 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|