ruby-bitly 0.1.7 → 0.2.0
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 +4 -4
- data/.travis.yml +6 -0
- data/CHANGELOG +7 -0
- data/Gemfile +0 -12
- data/Gemfile.lock +2 -17
- data/README.rdoc +71 -15
- data/fixtures/vcr_cassettes/expand_a_long_url_should_result_an_error.yml +1 -1
- data/fixtures/vcr_cassettes/expand_a_short_url_to_it_long_url.yml +1 -1
- data/fixtures/vcr_cassettes/get_clicks_by_short_url.yml +1 -1
- data/fixtures/vcr_cassettes/get_clicks_by_short_url_without_ssl.yml +44 -0
- data/fixtures/vcr_cassettes/shorten_bitly_url.yml +1 -1
- data/fixtures/vcr_cassettes/shorten_long_url.yml +1 -1
- data/fixtures/vcr_cassettes/shorten_long_url_with_preferred_domain.yml +1 -1
- data/lib/ruby-bitly.rb +33 -6
- data/lib/ruby-bitly/version.rb +1 -1
- data/ruby-bitly.gemspec +13 -11
- data/spec/ruby-bitly_spec.rb +259 -87
- data/spec/spec_helper.rb +6 -0
- metadata +11 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8e00677c2229d902e7a313440f78d6e87a94a60
|
4
|
+
data.tar.gz: 033846db3c3691ade37782e9f436022f2aa2e090
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7fd6f69ce0a634a1328f0ce699be96b524661cc1f1d6105c15d0c6fdbd36876c41d4a18d8e42607ac499007d60214f41964e7967cec7d81ffc96793b45d5e7bd
|
7
|
+
data.tar.gz: bf4b2ee95401e3efdf5615c844432a64eae8c5af684321fb20eac07a73d9eee0e5f0584c45aadba4b56e3298151ad7c903576634f57b75212df1d99848bdceb5
|
data/.travis.yml
ADDED
data/CHANGELOG
CHANGED
data/Gemfile
CHANGED
@@ -2,15 +2,3 @@ source "https://rubygems.org"
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in ruby-bitly.gemspec
|
4
4
|
gemspec
|
5
|
-
|
6
|
-
group :development, :test do
|
7
|
-
|
8
|
-
platforms :ruby_18 do
|
9
|
-
gem 'ruby-debug'
|
10
|
-
end
|
11
|
-
|
12
|
-
platforms :ruby_19, :ruby_20 do
|
13
|
-
gem 'debugger', '1.6.5'
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-bitly (0.
|
4
|
+
ruby-bitly (0.2.0)
|
5
5
|
rest-client (~> 2.0)
|
6
6
|
|
7
7
|
GEM
|
@@ -9,22 +9,14 @@ GEM
|
|
9
9
|
specs:
|
10
10
|
addressable (2.4.0)
|
11
11
|
byebug (9.0.6)
|
12
|
-
columnize (0.9.0)
|
13
12
|
crack (0.4.3)
|
14
13
|
safe_yaml (~> 1.0.0)
|
15
|
-
debugger (1.6.5)
|
16
|
-
columnize (>= 0.3.1)
|
17
|
-
debugger-linecache (~> 1.2.0)
|
18
|
-
debugger-ruby_core_source (~> 1.3.1)
|
19
|
-
debugger-linecache (1.2.0)
|
20
|
-
debugger-ruby_core_source (1.3.8)
|
21
14
|
diff-lcs (1.2.5)
|
22
15
|
domain_name (0.5.20160826)
|
23
16
|
unf (>= 0.0.5, < 1.0.0)
|
24
17
|
hashdiff (0.3.0)
|
25
18
|
http-cookie (1.0.3)
|
26
19
|
domain_name (~> 0.5)
|
27
|
-
linecache (1.3.1)
|
28
20
|
mime-types (3.1)
|
29
21
|
mime-types-data (~> 3.2015)
|
30
22
|
mime-types-data (3.2016.0521)
|
@@ -47,11 +39,6 @@ GEM
|
|
47
39
|
diff-lcs (>= 1.2.0, < 2.0)
|
48
40
|
rspec-support (~> 3.5.0)
|
49
41
|
rspec-support (3.5.0)
|
50
|
-
ruby-debug (0.10.4)
|
51
|
-
columnize (>= 0.1)
|
52
|
-
ruby-debug-base (~> 0.10.4.0)
|
53
|
-
ruby-debug-base (0.10.4)
|
54
|
-
linecache (>= 0.3)
|
55
42
|
safe_yaml (1.0.4)
|
56
43
|
unf (0.1.4)
|
57
44
|
unf_ext
|
@@ -67,13 +54,11 @@ PLATFORMS
|
|
67
54
|
|
68
55
|
DEPENDENCIES
|
69
56
|
byebug
|
70
|
-
debugger (= 1.6.5)
|
71
57
|
rake
|
72
58
|
rspec (~> 3.5)
|
73
59
|
ruby-bitly!
|
74
|
-
ruby-debug
|
75
60
|
vcr (~> 3.0)
|
76
61
|
webmock (~> 2.1)
|
77
62
|
|
78
63
|
BUNDLED WITH
|
79
|
-
1.13.
|
64
|
+
1.13.2
|
data/README.rdoc
CHANGED
@@ -1,17 +1,49 @@
|
|
1
1
|
= A simple bit.ly ruby client.
|
2
2
|
|
3
|
+
{<img src="https://travis-ci.org/dx7/ruby-bitly.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/dx7/ruby-bitly]
|
3
4
|
{<img src="https://codeclimate.com/github/dx7/ruby-bitly/badges/gpa.svg" />}[https://codeclimate.com/github/dx7/ruby-bitly]
|
4
5
|
|
5
6
|
|
6
|
-
==
|
7
|
+
== Configuration
|
8
|
+
You need to load the gem:
|
9
|
+
|
7
10
|
require 'ruby-bitly'
|
8
11
|
|
9
|
-
|
12
|
+
Set global configuration:
|
13
|
+
|
14
|
+
Bitly.config do |c|
|
15
|
+
c.login = 'login-here'
|
16
|
+
c.api_key = 'key-here'
|
17
|
+
c.key = 'key-here' # deprecated: use api_key instead
|
18
|
+
c.use_ssl = false # read more below
|
19
|
+
c.proxy = 'http://localhost:8888' # read more below
|
20
|
+
end
|
21
|
+
|
22
|
+
Or set them individualy:
|
23
|
+
|
24
|
+
Bitly.login = 'login-here'
|
25
|
+
Bitly.api_key = 'key-here'
|
26
|
+
Bitly.key = 'key-here' # deprecated: use api_key instead
|
27
|
+
Bitly.use_ssl = false # read more below
|
28
|
+
Bitly.proxy = 'http://localhost:8888' # read more below
|
29
|
+
|
30
|
+
Or set them on methods if you prefer (see it below).
|
31
|
+
|
32
|
+
|
33
|
+
== Shorten
|
34
|
+
# old way
|
35
|
+
bitly = Bitly.shorten("https://dx7.github.io/")
|
36
|
+
|
37
|
+
# old way setting local configuration
|
10
38
|
bitly = Bitly.shorten("https://dx7.github.io/", "login-here", "key-here")
|
11
39
|
|
12
40
|
# new way
|
13
|
-
bitly = Bitly.shorten(long_url: "https://dx7.github.io/"
|
41
|
+
bitly = Bitly.shorten(long_url: "https://dx7.github.io/")
|
42
|
+
|
43
|
+
# new way setting local configuration
|
44
|
+
bitly = Bitly.shorten(long_url: "https://dx7.github.io/", login: "login-here", api_key: "key-here", domain: "my.do")
|
14
45
|
|
46
|
+
# result
|
15
47
|
bitly.url #=> "http://bit.ly/2dAjjfo"
|
16
48
|
|
17
49
|
y bitly
|
@@ -26,14 +58,19 @@
|
|
26
58
|
|
27
59
|
|
28
60
|
== Expand
|
29
|
-
|
61
|
+
# old way
|
62
|
+
bitly = Bitly.expand("http://bit.ly/2dAjjfo")
|
30
63
|
|
31
|
-
# old way
|
64
|
+
# old way setting local configuration
|
32
65
|
bitly = Bitly.expand("http://bit.ly/2dAjjfo", "login-here", "key-here")
|
33
66
|
|
34
67
|
# new way
|
68
|
+
bitly = Bitly.expand(short_url: "http://bit.ly/2dAjjfo")
|
69
|
+
|
70
|
+
# new way setting local configuration
|
35
71
|
bitly = Bitly.expand(short_url: "http://bit.ly/2dAjjfo", login: "login-here", api_key: "key-here")
|
36
72
|
|
73
|
+
# result
|
37
74
|
y bitly
|
38
75
|
:short_url: http://bit.ly/2dAjjfo
|
39
76
|
:long_url: https://dx7.github.io/
|
@@ -44,14 +81,19 @@
|
|
44
81
|
|
45
82
|
|
46
83
|
== Get Clicks
|
47
|
-
|
84
|
+
# old way
|
85
|
+
bitly = Bitly.get_clicks("http://bit.ly/2dAjjfo")
|
48
86
|
|
49
|
-
# old way
|
87
|
+
# old way setting local configuration
|
50
88
|
bitly = Bitly.get_clicks("http://bit.ly/2dAjjfo", "login-here", "key-here")
|
51
89
|
|
52
90
|
# new way
|
91
|
+
bitly = Bitly.get_clicks(short_url: "http://bit.ly/2dAjjfo")
|
92
|
+
|
93
|
+
# new way setting local configuration
|
53
94
|
bitly = Bitly.get_clicks(short_url: "http://bit.ly/2dAjjfo", login: "login-here", api_key: "key-here")
|
54
95
|
|
96
|
+
# result
|
55
97
|
y bitly
|
56
98
|
:short_url: https://bit.ly/2dAjjfo
|
57
99
|
:global_hash: 2dAkyet
|
@@ -62,7 +104,21 @@
|
|
62
104
|
:status_txt: OK
|
63
105
|
|
64
106
|
|
65
|
-
==
|
107
|
+
== Proxy
|
108
|
+
All calls will use the proxy specified by environment variable "http_proxy" by default.
|
109
|
+
|
110
|
+
You can set the proxy directly if you prefer:
|
111
|
+
|
112
|
+
Bitly.proxy = 'http://localhost:8888'
|
113
|
+
|
114
|
+
|
115
|
+
== SSL
|
116
|
+
All calls will use SSL by default. You can disable it:
|
117
|
+
|
118
|
+
Bitly.use_ssl = false
|
119
|
+
|
120
|
+
|
121
|
+
== Command Line
|
66
122
|
Usage
|
67
123
|
bitly [options] url
|
68
124
|
|
@@ -93,17 +149,17 @@
|
|
93
149
|
-V, --verbose Verbose output
|
94
150
|
|
95
151
|
|
96
|
-
|
152
|
+
== Authentication
|
97
153
|
bit.ly API requires authentication credentials.
|
98
154
|
|
99
|
-
You need a free bit.ly
|
100
|
-
If you already
|
155
|
+
You'll need a free bit.ly login and apiKey. Sign up here: http://bit.ly/a/sign_up
|
156
|
+
If you've already signed up, you can find your apiKey here: http://bit.ly/a/your_api_key
|
101
157
|
|
102
|
-
|
103
|
-
bitly -l <
|
158
|
+
Using commmand line you can supply credentials as parameters. For example:
|
159
|
+
bitly -l <login-here> -k <key-here> -s http://dx7.github.io
|
104
160
|
|
105
|
-
Or you can create the file ~/.bitly (YAML format) with
|
106
|
-
login: <
|
161
|
+
Or you can create the file ~/.bitly (YAML format) with that content:
|
162
|
+
login: <login-here>
|
107
163
|
key: <key-here>
|
108
164
|
|
109
165
|
|
@@ -2,7 +2,7 @@
|
|
2
2
|
http_interactions:
|
3
3
|
- request:
|
4
4
|
method: get
|
5
|
-
uri:
|
5
|
+
uri: https://api-ssl.bitly.com/v3/clicks?apiKey=my-api-key&login=my-login&shortUrl=http://bit.ly/xlii42
|
6
6
|
body:
|
7
7
|
encoding: US-ASCII
|
8
8
|
string: ''
|
@@ -0,0 +1,44 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: http://api.bitly.com/v3/clicks?apiKey=my-api-key&login=my-login&shortUrl=http://bit.ly/xlii42
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
Accept:
|
11
|
+
- "*/*"
|
12
|
+
Accept-Encoding:
|
13
|
+
- gzip, deflate
|
14
|
+
User-Agent:
|
15
|
+
- rest-client/2.0.0 (darwin15.6.0 x86_64) ruby/2.3.0p0
|
16
|
+
Host:
|
17
|
+
- api.bitly.com
|
18
|
+
response:
|
19
|
+
status:
|
20
|
+
code: 200
|
21
|
+
message: OK
|
22
|
+
headers:
|
23
|
+
Server:
|
24
|
+
- nginx
|
25
|
+
Date:
|
26
|
+
- Sun, 02 Oct 2016 03:40:18 GMT
|
27
|
+
Content-Type:
|
28
|
+
- application/json; charset=utf-8
|
29
|
+
Content-Length:
|
30
|
+
- '187'
|
31
|
+
Etag:
|
32
|
+
- '"a6d2ce859ab41b205deb5d920fa10e3143a79bf2"'
|
33
|
+
Age:
|
34
|
+
- '356'
|
35
|
+
Connection:
|
36
|
+
- keep-alive
|
37
|
+
body:
|
38
|
+
encoding: UTF-8
|
39
|
+
string: '{"status_code": 200, "data": {"clicks": [{"short_url": "http://bit.ly/xlii42",
|
40
|
+
"global_hash": "cunZEP", "user_clicks": 2, "user_hash": "cT1Izu", "global_clicks":
|
41
|
+
2}]}, "status_txt": "OK"}'
|
42
|
+
http_version:
|
43
|
+
recorded_at: Sun, 02 Oct 2016 03:46:13 GMT
|
44
|
+
recorded_with: VCR 3.0.3
|
data/lib/ruby-bitly.rb
CHANGED
@@ -1,16 +1,37 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require 'yaml'
|
3
|
-
require '
|
3
|
+
require 'rest-client'
|
4
4
|
require 'json'
|
5
5
|
require 'ostruct'
|
6
6
|
|
7
7
|
class Bitly < OpenStruct
|
8
8
|
|
9
|
-
REST_API_URL = "http://api.
|
9
|
+
REST_API_URL = "http://api.bitly.com"
|
10
|
+
REST_API_URL_SSL = "https://api-ssl.bitly.com"
|
10
11
|
ACTION_PATH = { :shorten => '/v3/shorten', :expand => '/v3/expand', :clicks => '/v3/clicks' }
|
12
|
+
RestClient.proxy = ENV['http_proxy']
|
11
13
|
|
12
14
|
class << self
|
13
|
-
attr_accessor :login, :
|
15
|
+
attr_accessor :login, :api_key
|
16
|
+
attr_writer :use_ssl
|
17
|
+
alias :key :api_key
|
18
|
+
alias :key= :api_key=
|
19
|
+
|
20
|
+
def use_ssl
|
21
|
+
instance_variable_defined?(:@use_ssl) ? @use_ssl : true
|
22
|
+
end
|
23
|
+
|
24
|
+
def proxy=(addr)
|
25
|
+
RestClient.proxy = addr
|
26
|
+
end
|
27
|
+
|
28
|
+
def proxy
|
29
|
+
RestClient.proxy
|
30
|
+
end
|
31
|
+
|
32
|
+
def config
|
33
|
+
yield self
|
34
|
+
end
|
14
35
|
|
15
36
|
# Old API:
|
16
37
|
#
|
@@ -42,7 +63,7 @@ class Bitly < OpenStruct
|
|
42
63
|
params[:domain] = options[:domain]
|
43
64
|
end
|
44
65
|
|
45
|
-
response = JSON.parse RestClient.post(
|
66
|
+
response = JSON.parse RestClient.post(rest_api_url + ACTION_PATH[:shorten], params)
|
46
67
|
response.delete("data") if response["data"].empty?
|
47
68
|
|
48
69
|
bitly = new response["data"]
|
@@ -77,7 +98,7 @@ class Bitly < OpenStruct
|
|
77
98
|
options = {}
|
78
99
|
end
|
79
100
|
|
80
|
-
response = JSON.parse RestClient.post(
|
101
|
+
response = JSON.parse RestClient.post(rest_api_url + ACTION_PATH[:expand], { :shortURL => short_url, :login => login, :apiKey => key })
|
81
102
|
|
82
103
|
bitly = new(response["data"]["expand"].first)
|
83
104
|
bitly.status_code = response["status_code"]
|
@@ -110,7 +131,7 @@ class Bitly < OpenStruct
|
|
110
131
|
options = {}
|
111
132
|
end
|
112
133
|
|
113
|
-
response = JSON.parse RestClient.get("#{
|
134
|
+
response = JSON.parse RestClient.get("#{rest_api_url}#{ACTION_PATH[:clicks]}?login=#{login}&apiKey=#{key}&shortUrl=#{short_url}")
|
114
135
|
|
115
136
|
bitly = new(response["data"]["clicks"].first)
|
116
137
|
bitly.status_code = response["status_code"]
|
@@ -118,5 +139,11 @@ class Bitly < OpenStruct
|
|
118
139
|
|
119
140
|
bitly
|
120
141
|
end
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
def rest_api_url
|
146
|
+
use_ssl ? REST_API_URL_SSL : REST_API_URL
|
147
|
+
end
|
121
148
|
end
|
122
149
|
end
|
data/lib/ruby-bitly/version.rb
CHANGED
data/ruby-bitly.gemspec
CHANGED
@@ -3,17 +3,19 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
require "ruby-bitly/version"
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
|
-
s.name
|
7
|
-
s.version
|
8
|
-
s.authors
|
9
|
-
s.email
|
10
|
-
s.homepage
|
11
|
-
s.summary
|
12
|
-
s.description
|
13
|
-
s.files
|
14
|
-
s.test_files
|
15
|
-
s.executables
|
16
|
-
s.require_paths
|
6
|
+
s.name = "ruby-bitly"
|
7
|
+
s.version = BitlyGem::VERSION
|
8
|
+
s.authors = ["dx7"]
|
9
|
+
s.email = ["dx7@protonmail.ch"]
|
10
|
+
s.homepage = "http://github.com/dx7/ruby-bitly"
|
11
|
+
s.summary = %q{A simple bit.ly ruby client to shorten URLs, expand or get number of clicks on a bitlink}
|
12
|
+
s.description = %q{A simple bit.ly ruby client to shorten URLs, expand or get number of clicks on a bitlink.}
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.licenses = ['MIT']
|
18
|
+
s.required_ruby_version = '>= 2.0.0'
|
17
19
|
s.add_dependency('rest-client', '~> 2.0')
|
18
20
|
s.add_development_dependency('byebug')
|
19
21
|
s.add_development_dependency('rake')
|
data/spec/ruby-bitly_spec.rb
CHANGED
@@ -1,122 +1,294 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
-
require 'byebug'
|
4
3
|
|
5
|
-
describe
|
4
|
+
describe 'RubyBitly' do
|
6
5
|
|
7
|
-
let(:login) {
|
8
|
-
let(:key) {
|
6
|
+
let(:login) { RSpec::RUBY_BITLY_LOGIN }
|
7
|
+
let(:key) { RSpec::RUBY_BITLY_APIKEY }
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
Bitly.
|
9
|
+
describe 'config' do
|
10
|
+
it 'set variable login' do
|
11
|
+
Bitly.config { |c| c.login = 'login-custom' }
|
12
|
+
expect(Bitly.login).to eq('login-custom')
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
expect(response.global_hash).not_to be_empty
|
19
|
-
expect(response.hash_path.length).not_to eq(0)
|
20
|
-
expect(response.url).to match(/^http:\/\/bit\.ly\/[A-Za-z0-9]*/)
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'Shorten long url using options API' do
|
24
|
-
response = VCR.use_cassette('shorten_long_url') do
|
25
|
-
Bitly.shorten(:long_url => "http://google.com", :login => login, :api_key => key)
|
15
|
+
it 'set variables key' do
|
16
|
+
Bitly.config { |c| c.api_key = 'key-custom' }
|
17
|
+
expect(Bitly.api_key).to eq('key-custom')
|
26
18
|
end
|
27
19
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
expect(response.url).to match(/^http:\/\/bit\.ly\/[A-Za-z0-9]*/)
|
20
|
+
it 'set variables use_ssl' do
|
21
|
+
Bitly.config { |c| c.use_ssl = false }
|
22
|
+
expect(Bitly.use_ssl).to be false
|
23
|
+
Bitly.config { |c| c.use_ssl = true } # reset to default value
|
24
|
+
end
|
34
25
|
end
|
35
26
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
27
|
+
describe 'shorten' do
|
28
|
+
context 'using local config' do
|
29
|
+
before do
|
30
|
+
Bitly.config { |c| c.login = 'invalid login'; c.api_key = 'invalid key' }
|
31
|
+
end
|
41
32
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
expect(response.hash_path.length).not_to eq(0)
|
47
|
-
expect(response.url).to match(/^http:\/\/j\.mp\/[A-Za-z0-9]*/)
|
48
|
-
end
|
33
|
+
it 'shorten long url using old API' do
|
34
|
+
response = VCR.use_cassette('shorten_long_url') do
|
35
|
+
Bitly.shorten('http://google.com', login, key)
|
36
|
+
end
|
49
37
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
38
|
+
expect(response.status_txt).to eq('OK')
|
39
|
+
expect(response.status_code).to eq(200)
|
40
|
+
expect(response.new_hash).to eq(0)
|
41
|
+
expect(response.global_hash).not_to be_empty
|
42
|
+
expect(response.hash_path.length).not_to eq(0)
|
43
|
+
expect(response.url).to match(/^http:\/\/bit\.ly\/[A-Za-z0-9]*/)
|
44
|
+
end
|
54
45
|
|
55
|
-
|
56
|
-
|
57
|
-
|
46
|
+
it 'shorten long url using new API' do
|
47
|
+
response = VCR.use_cassette('shorten_long_url') do
|
48
|
+
Bitly.shorten(:long_url => 'http://google.com', :login => login, :api_key => key)
|
49
|
+
end
|
50
|
+
|
51
|
+
expect(response.status_txt).to eq('OK')
|
52
|
+
expect(response.status_code).to eq(200)
|
53
|
+
expect(response.new_hash).to eq(0)
|
54
|
+
expect(response.global_hash).not_to be_empty
|
55
|
+
expect(response.hash_path.length).not_to eq(0)
|
56
|
+
expect(response.url).to match(/^http:\/\/bit\.ly\/[A-Za-z0-9]*/)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'shorten long url with a preferred domain' do
|
60
|
+
response = VCR.use_cassette('shorten_long_url_with_preferred_domain') do
|
61
|
+
Bitly.shorten(:long_url => 'http://google.com', :domain => 'j.mp', :login => login, :api_key => key)
|
62
|
+
end
|
63
|
+
|
64
|
+
expect(response.status_txt).to eq('OK')
|
65
|
+
expect(response.status_code).to eq(200)
|
66
|
+
expect(response.new_hash).to eq(0)
|
67
|
+
expect(response.global_hash).not_to be_empty
|
68
|
+
expect(response.hash_path.length).not_to eq(0)
|
69
|
+
expect(response.url).to match(/^http:\/\/j\.mp\/[A-Za-z0-9]*/)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'shorten bitly url' do
|
73
|
+
response = VCR.use_cassette('shorten_bitly_url') do
|
74
|
+
Bitly.shorten('http://bit.ly/bcvNe5', login, key)
|
75
|
+
end
|
58
76
|
|
59
|
-
|
60
|
-
|
61
|
-
|
77
|
+
expect(response.status_txt).to eq('ALREADY_A_BITLY_LINK')
|
78
|
+
expect(response.status_code).to eq(500)
|
79
|
+
end
|
62
80
|
end
|
63
81
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
82
|
+
context 'using global config' do
|
83
|
+
before do
|
84
|
+
Bitly.config { |c| c.login = login; c.api_key = key }
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'shorten long url using old API' do
|
88
|
+
response = VCR.use_cassette('shorten_long_url') do
|
89
|
+
Bitly.shorten('http://google.com')
|
90
|
+
end
|
91
|
+
|
92
|
+
expect(response.status_txt).to eq('OK')
|
93
|
+
expect(response.status_code).to eq(200)
|
94
|
+
expect(response.new_hash).to eq(0)
|
95
|
+
expect(response.global_hash).not_to be_empty
|
96
|
+
expect(response.hash_path.length).not_to eq(0)
|
97
|
+
expect(response.url).to match(/^http:\/\/bit\.ly\/[A-Za-z0-9]*/)
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'shorten long url using global config' do
|
101
|
+
response = VCR.use_cassette('shorten_long_url') do
|
102
|
+
Bitly.shorten(:long_url => 'http://google.com')
|
103
|
+
end
|
104
|
+
|
105
|
+
expect(response.status_txt).to eq('OK')
|
106
|
+
expect(response.status_code).to eq(200)
|
107
|
+
expect(response.new_hash).to eq(0)
|
108
|
+
expect(response.global_hash).not_to be_empty
|
109
|
+
expect(response.hash_path.length).not_to eq(0)
|
110
|
+
expect(response.url).to match(/^http:\/\/bit\.ly\/[A-Za-z0-9]*/)
|
111
|
+
end
|
112
|
+
end
|
69
113
|
end
|
70
114
|
|
71
|
-
|
72
|
-
|
73
|
-
|
115
|
+
describe 'expand' do
|
116
|
+
context 'using local config' do
|
117
|
+
before do
|
118
|
+
Bitly.config { |c| c.login = 'invalid login'; c.api_key = 'invalid key' }
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'expand a short url to its long url old API' do
|
122
|
+
response = VCR.use_cassette('expand_a_short_url_to_it_long_url') do
|
123
|
+
Bitly.expand('http://bit.ly/bcvNe5', login, key)
|
124
|
+
end
|
125
|
+
|
126
|
+
expect(response.status_txt).to eq('OK')
|
127
|
+
expect(response.status_code).to eq(200)
|
128
|
+
expect(response.long_url).to eq('http://google.com')
|
129
|
+
expect(response.short_url).to eq('http://bit.ly/bcvNe5')
|
130
|
+
expect(response.user_hash).to eq('bcvNe5')
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'expand a short url to its long url' do
|
134
|
+
response = VCR.use_cassette('expand_a_short_url_to_it_long_url') do
|
135
|
+
Bitly.expand({ :short_url => 'http://bit.ly/bcvNe5', :login => login, :api_key => key })
|
136
|
+
end
|
137
|
+
|
138
|
+
expect(response.status_txt).to eq('OK')
|
139
|
+
expect(response.status_code).to eq(200)
|
140
|
+
expect(response.long_url).to eq('http://google.com')
|
141
|
+
expect(response.short_url).to eq('http://bit.ly/bcvNe5')
|
142
|
+
expect(response.user_hash).to eq('bcvNe5')
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'expand a long url should result an error' do
|
146
|
+
response = VCR.use_cassette('expand_a_long_url_should_result_an_error') do
|
147
|
+
Bitly.expand('http://google.com', login, key)
|
148
|
+
end
|
149
|
+
|
150
|
+
expect(response.status_txt).to eq('OK')
|
151
|
+
expect(response.status_code).to eq(200)
|
152
|
+
expect(response.long_url).to eq('NOT_FOUND')
|
153
|
+
expect(response.short_url).to eq('http://google.com')
|
154
|
+
end
|
74
155
|
end
|
75
156
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
157
|
+
context 'using global config' do
|
158
|
+
before do
|
159
|
+
Bitly.config { |c| c.login = login; c.api_key = key }
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'expand a short url to its long url old API' do
|
163
|
+
response = VCR.use_cassette('expand_a_short_url_to_it_long_url') do
|
164
|
+
Bitly.expand('http://bit.ly/bcvNe5')
|
165
|
+
end
|
166
|
+
|
167
|
+
expect(response.status_txt).to eq('OK')
|
168
|
+
expect(response.status_code).to eq(200)
|
169
|
+
expect(response.long_url).to eq('http://google.com')
|
170
|
+
expect(response.short_url).to eq('http://bit.ly/bcvNe5')
|
171
|
+
expect(response.user_hash).to eq('bcvNe5')
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'expand a short url to its long url using global config' do
|
175
|
+
response = VCR.use_cassette('expand_a_short_url_to_it_long_url') do
|
176
|
+
Bitly.expand({ :short_url => 'http://bit.ly/bcvNe5' })
|
177
|
+
end
|
178
|
+
|
179
|
+
expect(response.status_txt).to eq('OK')
|
180
|
+
expect(response.status_code).to eq(200)
|
181
|
+
expect(response.long_url).to eq('http://google.com')
|
182
|
+
expect(response.short_url).to eq('http://bit.ly/bcvNe5')
|
183
|
+
expect(response.user_hash).to eq('bcvNe5')
|
184
|
+
end
|
185
|
+
end
|
81
186
|
end
|
82
187
|
|
83
|
-
|
84
|
-
|
85
|
-
|
188
|
+
|
189
|
+
describe 'get clicks' do
|
190
|
+
context 'using local config' do
|
191
|
+
before do
|
192
|
+
Bitly.config { |c| c.login = 'invalid login'; c.api_key = 'invalid key' }
|
193
|
+
end
|
194
|
+
|
195
|
+
it 'get clicks by short_url using old API' do
|
196
|
+
bitly = VCR.use_cassette('get_clicks_by_short_url') do
|
197
|
+
Bitly.get_clicks('http://bit.ly/xlii42', login, key)
|
198
|
+
end
|
199
|
+
|
200
|
+
expect(bitly.status_txt).to eq('OK')
|
201
|
+
expect(bitly.status_code).to eq(200)
|
202
|
+
expect(bitly.global_clicks).to be_an_instance_of Fixnum
|
203
|
+
expect(bitly.user_clicks).to be_an_instance_of Fixnum
|
204
|
+
expect(bitly.short_url).to eq('http://bit.ly/xlii42')
|
205
|
+
expect(bitly.global_hash).to eq('cunZEP')
|
206
|
+
expect(bitly.user_hash).to eq('cT1Izu')
|
207
|
+
end
|
208
|
+
|
209
|
+
it 'get clicks by short_url using options API' do
|
210
|
+
bitly = VCR.use_cassette('get_clicks_by_short_url') do
|
211
|
+
Bitly.get_clicks(:short_url => 'http://bit.ly/xlii42', :login => login, :api_key => key)
|
212
|
+
end
|
213
|
+
|
214
|
+
expect(bitly.status_txt).to eq('OK')
|
215
|
+
expect(bitly.status_code).to eq(200)
|
216
|
+
expect(bitly.global_clicks).to be_an_instance_of Fixnum
|
217
|
+
expect(bitly.user_clicks).to be_an_instance_of Fixnum
|
218
|
+
expect(bitly.short_url).to eq('http://bit.ly/xlii42')
|
219
|
+
expect(bitly.global_hash).to eq('cunZEP')
|
220
|
+
expect(bitly.user_hash).to eq('cT1Izu')
|
221
|
+
end
|
86
222
|
end
|
87
223
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
224
|
+
context 'using global config' do
|
225
|
+
before do
|
226
|
+
Bitly.config { |c| c.login = login; c.api_key = key }
|
227
|
+
end
|
228
|
+
|
229
|
+
it 'get clicks by short_url using old API' do
|
230
|
+
bitly = VCR.use_cassette('get_clicks_by_short_url') do
|
231
|
+
Bitly.get_clicks('http://bit.ly/xlii42')
|
232
|
+
end
|
233
|
+
|
234
|
+
expect(bitly.status_txt).to eq('OK')
|
235
|
+
expect(bitly.status_code).to eq(200)
|
236
|
+
expect(bitly.global_clicks).to be_an_instance_of Fixnum
|
237
|
+
expect(bitly.user_clicks).to be_an_instance_of Fixnum
|
238
|
+
expect(bitly.short_url).to eq('http://bit.ly/xlii42')
|
239
|
+
expect(bitly.global_hash).to eq('cunZEP')
|
240
|
+
expect(bitly.user_hash).to eq('cT1Izu')
|
241
|
+
end
|
242
|
+
|
243
|
+
it 'get clicks by short_url using global config' do
|
244
|
+
bitly = VCR.use_cassette('get_clicks_by_short_url') do
|
245
|
+
Bitly.get_clicks(:short_url => 'http://bit.ly/xlii42')
|
246
|
+
end
|
247
|
+
|
248
|
+
expect(bitly.status_txt).to eq('OK')
|
249
|
+
expect(bitly.status_code).to eq(200)
|
250
|
+
expect(bitly.global_clicks).to be_an_instance_of Fixnum
|
251
|
+
expect(bitly.user_clicks).to be_an_instance_of Fixnum
|
252
|
+
expect(bitly.short_url).to eq('http://bit.ly/xlii42')
|
253
|
+
expect(bitly.global_hash).to eq('cunZEP')
|
254
|
+
expect(bitly.user_hash).to eq('cT1Izu')
|
255
|
+
end
|
256
|
+
end
|
92
257
|
end
|
93
258
|
|
259
|
+
describe 'proxy' do
|
260
|
+
it 'uses http_proxy as default' do
|
261
|
+
expect(RestClient.proxy).not_to eq('http://http-proxy.host.com:1234')
|
262
|
+
|
263
|
+
ENV['http_proxy'] = 'http://http-proxy.host.com:1234'
|
264
|
+
Object.send :remove_const, :Bitly
|
265
|
+
load './lib/ruby-bitly.rb'
|
94
266
|
|
95
|
-
|
96
|
-
bitly = VCR.use_cassette('get_clicks_by_short_url') do
|
97
|
-
Bitly.get_clicks("http://bit.ly/xlii42", login, key)
|
267
|
+
expect(RestClient.proxy).to eq('http://http-proxy.host.com:1234')
|
98
268
|
end
|
99
269
|
|
100
|
-
|
101
|
-
|
102
|
-
expect(bitly.global_clicks).to be_an_instance_of Fixnum
|
103
|
-
expect(bitly.user_clicks).to be_an_instance_of Fixnum
|
104
|
-
expect(bitly.short_url).to eq("http://bit.ly/xlii42")
|
105
|
-
expect(bitly.global_hash).to eq("cunZEP")
|
106
|
-
expect(bitly.user_hash).to eq("cT1Izu")
|
107
|
-
end
|
270
|
+
it 'uses set proxy on configuration' do
|
271
|
+
expect(RestClient.proxy).not_to eq('http://config-proxy.host.com:1234')
|
108
272
|
|
109
|
-
|
110
|
-
|
111
|
-
|
273
|
+
ENV['http_proxy'] = 'http://http-proxy.host.com:1234'
|
274
|
+
Object.send :remove_const, :Bitly
|
275
|
+
load './lib/ruby-bitly.rb'
|
276
|
+
Bitly.proxy = 'http://config-proxy.host.com:1234'
|
277
|
+
|
278
|
+
expect(RestClient.proxy).to eq('http://config-proxy.host.com:1234')
|
112
279
|
end
|
280
|
+
end
|
281
|
+
|
282
|
+
describe 'ssl' do
|
283
|
+
it 'do not use ssl if it is disabled' do
|
284
|
+
Bitly.use_ssl = false
|
113
285
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
286
|
+
bitly = VCR.use_cassette('get_clicks_by_short_url_without_ssl') do
|
287
|
+
Bitly.get_clicks(:short_url => 'http://bit.ly/xlii42', :login => login, :api_key => key)
|
288
|
+
end
|
289
|
+
|
290
|
+
expect(bitly.status_txt).to eq('OK')
|
291
|
+
expect(bitly.status_code).to eq(200)
|
292
|
+
end
|
121
293
|
end
|
122
294
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,13 +1,19 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
3
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
require 'byebug'
|
4
5
|
require 'ruby-bitly'
|
5
6
|
require 'rspec'
|
6
7
|
require 'vcr'
|
7
8
|
|
9
|
+
RSpec::RUBY_BITLY_LOGIN = ENV['RUBY_BITLY_LOGIN'] || 'my-login'
|
10
|
+
RSpec::RUBY_BITLY_APIKEY = ENV['RUBY_BITLY_APIKEY'] || 'my-api-key'
|
11
|
+
|
8
12
|
VCR.configure do |c|
|
9
13
|
c.cassette_library_dir = 'fixtures/vcr_cassettes'
|
10
14
|
c.ignore_localhost = true
|
11
15
|
c.hook_into :webmock
|
12
16
|
c.default_cassette_options = { :record => :once, :match_requests_on => [:method, :uri, :host, :path, :body, :query] }
|
17
|
+
c.filter_sensitive_data('my-login') { RSpec::RUBY_BITLY_LOGIN }
|
18
|
+
c.filter_sensitive_data('my-api-key') { RSpec::RUBY_BITLY_APIKEY }
|
13
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-bitly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dx7
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -94,7 +94,8 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '2.1'
|
97
|
-
description: A simple bit.ly ruby client
|
97
|
+
description: A simple bit.ly ruby client to shorten URLs, expand or get number of
|
98
|
+
clicks on a bitlink.
|
98
99
|
email:
|
99
100
|
- dx7@protonmail.ch
|
100
101
|
executables:
|
@@ -104,6 +105,7 @@ extra_rdoc_files: []
|
|
104
105
|
files:
|
105
106
|
- ".gitignore"
|
106
107
|
- ".rspec"
|
108
|
+
- ".travis.yml"
|
107
109
|
- CHANGELOG
|
108
110
|
- Gemfile
|
109
111
|
- Gemfile.lock
|
@@ -114,6 +116,7 @@ files:
|
|
114
116
|
- fixtures/vcr_cassettes/expand_a_long_url_should_result_an_error.yml
|
115
117
|
- fixtures/vcr_cassettes/expand_a_short_url_to_it_long_url.yml
|
116
118
|
- fixtures/vcr_cassettes/get_clicks_by_short_url.yml
|
119
|
+
- fixtures/vcr_cassettes/get_clicks_by_short_url_without_ssl.yml
|
117
120
|
- fixtures/vcr_cassettes/shorten_bitly_url.yml
|
118
121
|
- fixtures/vcr_cassettes/shorten_long_url.yml
|
119
122
|
- fixtures/vcr_cassettes/shorten_long_url_with_preferred_domain.yml
|
@@ -123,7 +126,8 @@ files:
|
|
123
126
|
- spec/ruby-bitly_spec.rb
|
124
127
|
- spec/spec_helper.rb
|
125
128
|
homepage: http://github.com/dx7/ruby-bitly
|
126
|
-
licenses:
|
129
|
+
licenses:
|
130
|
+
- MIT
|
127
131
|
metadata: {}
|
128
132
|
post_install_message:
|
129
133
|
rdoc_options: []
|
@@ -133,7 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
133
137
|
requirements:
|
134
138
|
- - ">="
|
135
139
|
- !ruby/object:Gem::Version
|
136
|
-
version:
|
140
|
+
version: 2.0.0
|
137
141
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
142
|
requirements:
|
139
143
|
- - ">="
|
@@ -144,7 +148,8 @@ rubyforge_project:
|
|
144
148
|
rubygems_version: 2.5.1
|
145
149
|
signing_key:
|
146
150
|
specification_version: 4
|
147
|
-
summary: A simple bit.ly ruby client
|
151
|
+
summary: A simple bit.ly ruby client to shorten URLs, expand or get number of clicks
|
152
|
+
on a bitlink
|
148
153
|
test_files:
|
149
154
|
- spec/ruby-bitly_spec.rb
|
150
155
|
- spec/spec_helper.rb
|