bitly_quickly 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bitly_quickly.rb +20 -7
- data/lib/bitly_quickly/version.rb +1 -1
- data/spec/lib/bitly_quickly_spec.rb +66 -22
- data/spec/spec_helper.rb +37 -19
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc642eb00ffcbbe76c53e87d63f13eac6821cc36
|
4
|
+
data.tar.gz: f6deeab3d454752ebea1fca7dc1abab848211bc4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f4ba7db16ebe33411651facba34be59a88ac15d17aca34c063e0e49faf72ad436d5b2c180f66616b1aae00ec1a080ecfa981dff3f9fafe9cbd4b9c29294ac9e
|
7
|
+
data.tar.gz: 326d27a4874010aa9853e4055a85707823842ff60b8aa04ebfbda6cc46e6619633240592f635c08a34e357c6e8cbcb8fc971b203a71486c5d6f3c4b271e6c98e
|
data/lib/bitly_quickly.rb
CHANGED
@@ -5,7 +5,12 @@ require 'bitly_quickly/version'
|
|
5
5
|
|
6
6
|
# V3 Wrapper
|
7
7
|
class BitlyQuickly
|
8
|
-
class
|
8
|
+
class BitlyError < StandardError; end
|
9
|
+
class RateLimitExceededError < BitlyError; end
|
10
|
+
class TemporarilyUnavailableError < BitlyError; end
|
11
|
+
class NotFoundError < BitlyError; end
|
12
|
+
class InvalidRequestOrResponseError < BitlyError; end
|
13
|
+
class UnknownError < BitlyError; end
|
9
14
|
|
10
15
|
DEFAULT_API_ADDRESS = 'https://api-ssl.bitly.com'
|
11
16
|
OJ_OPTIONS = {
|
@@ -67,9 +72,6 @@ class BitlyQuickly
|
|
67
72
|
access_token: access_token,
|
68
73
|
longUrl: long_url
|
69
74
|
},
|
70
|
-
headers: {
|
71
|
-
user_agent: 'Photolane.co'
|
72
|
-
}
|
73
75
|
}
|
74
76
|
)
|
75
77
|
|
@@ -91,9 +93,20 @@ class BitlyQuickly
|
|
91
93
|
def response_to_json(response)
|
92
94
|
json_response = parse_response response
|
93
95
|
|
94
|
-
|
95
|
-
|
96
|
-
|
96
|
+
case json_response[:status_code]
|
97
|
+
when 200
|
98
|
+
return json_response
|
99
|
+
when 403
|
100
|
+
raise RateLimitExceededError, json_response[:status_txt]
|
101
|
+
when 404
|
102
|
+
raise NotFoundError, json_response[:status_txt]
|
103
|
+
when 500
|
104
|
+
raise InvalidRequestOrResponseError, json_response[:status_txt]
|
105
|
+
when 503
|
106
|
+
raise TemporarilyUnavailableError, json_response[:status_txt]
|
107
|
+
else
|
108
|
+
raise UnknownError, json_response[:status_txt]
|
109
|
+
end
|
97
110
|
end
|
98
111
|
|
99
112
|
def parse_response(response)
|
@@ -39,19 +39,11 @@ describe BitlyQuickly do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'makes request' do
|
42
|
-
expect(@client.make_shorten_request('http://
|
42
|
+
expect(@client.make_shorten_request('http://example.org/1')).to be_kind_of(Typhoeus::Request)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
|
47
|
-
before do
|
48
|
-
@client = BitlyQuickly.new access_token: 'token'
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'raises an execption' do
|
52
|
-
expect { @client.shorten('http://www.google.com/1') }.to raise_error
|
53
|
-
end
|
54
|
-
end
|
46
|
+
# Success
|
55
47
|
|
56
48
|
context 'Response with valid token' do
|
57
49
|
before do
|
@@ -59,11 +51,11 @@ describe BitlyQuickly do
|
|
59
51
|
end
|
60
52
|
|
61
53
|
it 'raises an execption' do
|
62
|
-
expect { @client.shorten('http://
|
54
|
+
expect { @client.shorten('http://example.org/200/1') }.to_not raise_error
|
63
55
|
end
|
64
56
|
|
65
57
|
it 'status code' do
|
66
|
-
expect(@client.shorten('http://
|
58
|
+
expect(@client.shorten('http://example.org/200/1')).to eq('http://pht.io/1eyUhF1')
|
67
59
|
end
|
68
60
|
end
|
69
61
|
|
@@ -74,22 +66,74 @@ describe BitlyQuickly do
|
|
74
66
|
|
75
67
|
it 'makes many requests' do
|
76
68
|
request_urls = [
|
77
|
-
'http://
|
78
|
-
'http://
|
79
|
-
'http://
|
80
|
-
'http://
|
81
|
-
'http://
|
69
|
+
'http://example.org/200/1',
|
70
|
+
'http://example.org/200/2',
|
71
|
+
'http://example.org/200/3',
|
72
|
+
'http://example.org/200/4',
|
73
|
+
'http://example.org/200/5',
|
82
74
|
]
|
83
75
|
|
84
76
|
response_urls = {
|
85
|
-
'http://
|
86
|
-
'http://
|
87
|
-
'http://
|
88
|
-
'http://
|
89
|
-
'http://
|
77
|
+
'http://example.org/200/1' => 'http://pht.io/1eyUhF1',
|
78
|
+
'http://example.org/200/2' => 'http://pht.io/1eyUhF2',
|
79
|
+
'http://example.org/200/3' => 'http://pht.io/1eyUhF3',
|
80
|
+
'http://example.org/200/4' => 'http://pht.io/1eyUhF4',
|
81
|
+
'http://example.org/200/5' => 'http://pht.io/1eyUhF5',
|
90
82
|
}
|
91
83
|
|
92
84
|
expect(@client.shorten(request_urls)).to eq(response_urls)
|
93
85
|
end
|
94
86
|
end
|
87
|
+
|
88
|
+
# Errors
|
89
|
+
|
90
|
+
context 'Response 403' do
|
91
|
+
before do
|
92
|
+
@client = BitlyQuickly.new access_token: 'token'
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'raises an execption' do
|
96
|
+
expect { @client.shorten('http://example.org/403') }.to raise_error
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'Response 503' do
|
101
|
+
before do
|
102
|
+
@client = BitlyQuickly.new access_token: 'token'
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'raises an execption' do
|
106
|
+
expect { @client.shorten('http://example.org/503') }.to raise_error
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context 'Response 404' do
|
111
|
+
before do
|
112
|
+
@client = BitlyQuickly.new access_token: 'token'
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'raises an execption' do
|
116
|
+
expect { @client.shorten('http://example.org/404') }.to raise_error
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'Response 500' do
|
121
|
+
before do
|
122
|
+
@client = BitlyQuickly.new access_token: 'token'
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'raises an execption' do
|
126
|
+
expect { @client.shorten('http://example.org/500') }.to raise_error
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context 'Response 666' do
|
131
|
+
before do
|
132
|
+
@client = BitlyQuickly.new access_token: 'token'
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'raises an execption' do
|
136
|
+
expect { @client.shorten('http://example.org/666') }.to raise_error
|
137
|
+
end
|
138
|
+
end
|
95
139
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'simplecov'
|
2
|
-
require 'webmock
|
2
|
+
require 'webmock' # Disable all HTTP access
|
3
3
|
|
4
4
|
# Coverage tool, needs to be started as soon as possible
|
5
5
|
SimpleCov.start do
|
@@ -8,22 +8,40 @@ end
|
|
8
8
|
|
9
9
|
require 'bitly_quickly'
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
body: '{ "data": [ ], "status_code": 500, "status_txt": "INVALID_ARG_ACCESS_TOKEN" }'
|
18
|
-
)
|
19
|
-
|
20
|
-
1.upto(5) do |n|
|
21
|
-
stub_request(:get, %(https://api-ssl.bitly.com/v3/shorten?access_token=valid_token&longUrl=http://www.google.com/#{ n })).
|
22
|
-
with(headers: { 'User-Agent' => 'Photolane.co' }).
|
23
|
-
to_return(
|
24
|
-
status: 200,
|
25
|
-
body: %({ "status_code": 200, "status_txt": "OK", "data": { "long_url": "http:\/\/www.google.com\/#{ n }", "url": "http:\/\/pht.io\/1eyUhF#{ n }", "hash": "1eyUhFo", "global_hash": "2V6CFi", "new_hash": 0 } })
|
26
|
-
)
|
27
|
-
end
|
28
|
-
end
|
11
|
+
1.upto(5) do |n|
|
12
|
+
WebMock.stub_request(:get, %(https://api-ssl.bitly.com/v3/shorten?access_token=valid_token&longUrl=http://example.org/200/#{ n })).
|
13
|
+
to_return(
|
14
|
+
status: 200,
|
15
|
+
body: %({ "status_code": 200, "status_txt": "OK", "data": { "long_url": "http:\/\/example.org\/200\/#{ n }", "url": "http:\/\/pht.io\/1eyUhF#{ n }", "hash": "1eyUhFo", "global_hash": "2V6CFi", "new_hash": 0 } })
|
16
|
+
)
|
29
17
|
end
|
18
|
+
|
19
|
+
WebMock.stub_request(:get, 'https://api-ssl.bitly.com/v3/shorten?access_token=token&longUrl=http://example.org/403').
|
20
|
+
to_return(
|
21
|
+
status: 200,
|
22
|
+
body: '{ "data": null, "status_code": 403, "status_txt": "RATE_LIMIT_EXCEEDED" }'
|
23
|
+
)
|
24
|
+
|
25
|
+
WebMock.stub_request(:get, 'https://api-ssl.bitly.com/v3/shorten?access_token=token&longUrl=http://example.org/404').
|
26
|
+
to_return(
|
27
|
+
status: 200,
|
28
|
+
body: '{ "data": null, "status_code": 404, "status_txt": "NOT_FOUND" }'
|
29
|
+
)
|
30
|
+
|
31
|
+
WebMock.stub_request(:get, 'https://api-ssl.bitly.com/v3/shorten?access_token=token&longUrl=http://example.org/500').
|
32
|
+
to_return(
|
33
|
+
status: 200,
|
34
|
+
body: '{ "data": null, "status_code": 500, "status_txt": "INVALID_URI" }'
|
35
|
+
)
|
36
|
+
|
37
|
+
WebMock.stub_request(:get, 'https://api-ssl.bitly.com/v3/shorten?access_token=token&longUrl=http://example.org/503').
|
38
|
+
to_return(
|
39
|
+
status: 200,
|
40
|
+
body: '{ "data": null, "status_code": 503, "status_txt": "UNKNOWN_ERROR" }'
|
41
|
+
)
|
42
|
+
|
43
|
+
WebMock.stub_request(:get, 'https://api-ssl.bitly.com/v3/shorten?access_token=token&longUrl=http://example.org/666').
|
44
|
+
to_return(
|
45
|
+
status: 200,
|
46
|
+
body: '{ "data": null, "status_code": 666, "status_txt": "WHO_KNOWS" }'
|
47
|
+
)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitly_quickly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oldrich Vetesnik
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -146,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
146
146
|
version: '0'
|
147
147
|
requirements: []
|
148
148
|
rubyforge_project:
|
149
|
-
rubygems_version: 2.2.
|
149
|
+
rubygems_version: 2.2.2
|
150
150
|
signing_key:
|
151
151
|
specification_version: 4
|
152
152
|
summary: Shorten URLs with bit.ly API
|