bitly_quickly 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d359577bdaa34c60135ce813d2df657b16c69a4
4
- data.tar.gz: 50d8201cc818ce629dd95f6e5a4fa2146308922c
3
+ metadata.gz: cc642eb00ffcbbe76c53e87d63f13eac6821cc36
4
+ data.tar.gz: f6deeab3d454752ebea1fca7dc1abab848211bc4
5
5
  SHA512:
6
- metadata.gz: ad9bab5c499ea454db79bcb8cd57b9f9b7709ffa5abf0568d45da2adab39462ccc55b6557880b32ecd0ff47c32dcbb65bb4c609599961aa20b3617cd5bb8f500
7
- data.tar.gz: 61ba5554b55af1bd439f0f1cb2e645ffcacbd0bcbffb4da66aecb0099723b4c914bf165af0344cc3380dfaaacda6201ec75e96f9688d4e61242360ffcf423ad0
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 InvalidAccessToken < StandardError; end
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
- raise InvalidAccessToken if json_response[:status_txt] == 'INVALID_ARG_ACCESS_TOKEN'
95
-
96
- json_response
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)
@@ -1,3 +1,3 @@
1
1
  class BitlyQuickly
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -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://www.google.com/1')).to be_kind_of(Typhoeus::Request)
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
- context 'Response with invalid token' do
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://www.google.com/1') }.to_not raise_error
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://www.google.com/1')).to eq('http://pht.io/1eyUhF1')
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://www.google.com/1',
78
- 'http://www.google.com/2',
79
- 'http://www.google.com/3',
80
- 'http://www.google.com/4',
81
- 'http://www.google.com/5',
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://www.google.com/1' => 'http://pht.io/1eyUhF1',
86
- 'http://www.google.com/2' => 'http://pht.io/1eyUhF2',
87
- 'http://www.google.com/3' => 'http://pht.io/1eyUhF3',
88
- 'http://www.google.com/4' => 'http://pht.io/1eyUhF4',
89
- 'http://www.google.com/5' => 'http://pht.io/1eyUhF5',
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/rspec' # Disable all HTTP access
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
- RSpec.configure do |config|
12
- config.before(:each) do
13
- stub_request(:get, 'https://api-ssl.bitly.com/v3/shorten?access_token=token&longUrl=http://www.google.com/1').
14
- with(headers: { 'User-Agent' => 'Photolane.co' }).
15
- to_return(
16
- status: 200,
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.2
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-01-30 00:00:00.000000000 Z
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.1
149
+ rubygems_version: 2.2.2
150
150
  signing_key:
151
151
  specification_version: 4
152
152
  summary: Shorten URLs with bit.ly API