link_shrink 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +2 -1
- data/Changelog.md +11 -0
- data/README.md +17 -2
- data/lib/link_shrink/cli.rb +7 -0
- data/lib/link_shrink/request.rb +9 -1
- data/lib/link_shrink/shrinkers/base.rb +22 -0
- data/lib/link_shrink/shrinkers/google.rb +5 -0
- data/lib/link_shrink/shrinkers/owly.rb +33 -0
- data/lib/link_shrink/version.rb +1 -1
- data/lib/link_shrink.rb +1 -0
- data/spec/link_shrink/google_spec.rb +13 -1
- data/spec/link_shrink/owly_spec.rb +71 -0
- data/spec/link_shrink/shrinker_base_spec.rb +12 -1
- metadata +5 -2
data/.travis.yml
CHANGED
@@ -14,4 +14,5 @@ notifications:
|
|
14
14
|
- irc.freenode.org#rubyonadhd
|
15
15
|
env:
|
16
16
|
global:
|
17
|
-
|
17
|
+
- secure: bPqqN6twAJeRIpUEGozFIV3+dW2TSRxYQRrQhpzJ6Bx2ZXmk8UWl1O57Scf9/ZrCjo23MSvWKY1Rt1eyncaUVfzr4PjR6GjUwwdE7SPHQFKvGeNLIlJElzcz1ywcX6RU8+6JPYnx3NxR2O5VM8rf7wLwma00nuaDunM5NgKMMdQ=
|
18
|
+
- secure: JfAnh3FV1Q5Sz+QydZ7hFCCvCTiJoYqM4h0ztzZU/fqDNKo6d1/h8RWGZL++d7Ofl3Z7Jrfbj/1R4f8UiwNJVdqRMPgSrbbif8LfVBmsTtZm/gFcL5WFhm/kzIi7Jp7FmzNQc6Hx+EiI3T2mtLw51fi7lNNonVhevyEetUSvG2Q=
|
data/Changelog.md
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
# LinkShrink Changelog
|
2
2
|
|
3
|
+
## 0.0.6
|
4
|
+
|
5
|
+
Released September 8, 2013 ([0.0.6](https://github.com/jonahoffline/link_shrink/tree/v0.0.6)).
|
6
|
+
|
7
|
+
* Add Owly Shrinker
|
8
|
+
* Add option for selecting Owly Shrinker in command-line application.
|
9
|
+
* Add new mini DSL to ::Shrinkers::Base for defining the response structure to be parsed.
|
10
|
+
* Update Google class to use the response_options configuration.
|
11
|
+
* Update README with a new options and configuration.
|
12
|
+
* Update version to 0.0.6
|
13
|
+
|
3
14
|
## 0.0.5
|
4
15
|
|
5
16
|
Released September 7, 2013 ([0.0.5](https://github.com/jonahoffline/link_shrink/tree/v0.0.5)).
|
data/README.md
CHANGED
@@ -8,6 +8,7 @@ A Ruby Gem and Command-Line Application for shrinking those long and nasty links
|
|
8
8
|
* Google
|
9
9
|
* TinyUrl
|
10
10
|
* IsGd
|
11
|
+
* Owly
|
11
12
|
* **More to come...**
|
12
13
|
|
13
14
|
Installation
|
@@ -19,6 +20,7 @@ Installation
|
|
19
20
|
Setup
|
20
21
|
-------
|
21
22
|
|
23
|
+
## Google
|
22
24
|
Works best with a Google URL API key. You can sign-up for a free one at
|
23
25
|
[Google URL API](https://code.google.com/apis/console)
|
24
26
|
|
@@ -28,14 +30,22 @@ if the Terms of Service appear, read and accept them.
|
|
28
30
|
2. Go to the [API Access pane](https://code.google.com/apis/console#access). The API key is near the bottom of that pane,
|
29
31
|
in the section titled "Simple API Access."
|
30
32
|
|
31
|
-
|
33
|
+
## Owly
|
34
|
+
Sign-up for a free API key at [Owly](http://ow.ly/).
|
32
35
|
|
33
|
-
|
36
|
+
|
37
|
+
#### Configuration
|
38
|
+
|
39
|
+
Set your apikey as an environment variable:
|
34
40
|
|
35
41
|
$ export GOOGLE_URL_KEY='your_api_key_here'
|
42
|
+
|
43
|
+
|
44
|
+
$ export OWLY_URL_KEY='your_api_key_here'
|
36
45
|
|
37
46
|
You could also save it in your ~/.bash_profile
|
38
47
|
|
48
|
+
|
39
49
|
## Usage ##
|
40
50
|
|
41
51
|
Ruby:
|
@@ -88,12 +98,17 @@ In your terminal:
|
|
88
98
|
|
89
99
|
$ linkshrink --isgd http://www.rubyrogues.com
|
90
100
|
http://is.gd/6ZNRWe
|
101
|
+
|
102
|
+
$ linkshrink --owly http://www.rubyrogues.com
|
103
|
+
http://ow.ly/22rwSe
|
104
|
+
|
91
105
|
### Command-Line Options ###
|
92
106
|
|
93
107
|
Shrinkers:
|
94
108
|
|
95
109
|
* -t, --tinyurl - use TinyUrl API
|
96
110
|
* -i, --isgd - use Is.gd API
|
111
|
+
* -om --owly - use Owly API
|
97
112
|
* -g, --google - use Google API (Default)
|
98
113
|
|
99
114
|
Format and additional options:
|
data/lib/link_shrink/cli.rb
CHANGED
@@ -19,6 +19,7 @@ module LinkShrink
|
|
19
19
|
@tiny_url = false
|
20
20
|
@google = false
|
21
21
|
@is_gd = false
|
22
|
+
@owly = false
|
22
23
|
opts.version = LinkShrink::VERSION
|
23
24
|
opts.banner = <<MSG
|
24
25
|
Usage: link_shrink [OPTION] [URL]
|
@@ -36,6 +37,10 @@ MSG
|
|
36
37
|
@is_gd = :true
|
37
38
|
end
|
38
39
|
|
40
|
+
opts.on_head('-o', '--owly', 'use Owly') do
|
41
|
+
@owly = :true
|
42
|
+
end
|
43
|
+
|
39
44
|
opts.on_head('-g', '--google', 'use Google (Default)') do
|
40
45
|
@google = :true
|
41
46
|
end
|
@@ -84,6 +89,8 @@ MSG
|
|
84
89
|
'TinyUrl'
|
85
90
|
when @is_gd
|
86
91
|
'IsGd'
|
92
|
+
when @owly
|
93
|
+
'Owly'
|
87
94
|
else
|
88
95
|
'Google'
|
89
96
|
end
|
data/lib/link_shrink/request.rb
CHANGED
@@ -22,10 +22,18 @@ module LinkShrink
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
def process_parse_options(parsed_json, shrinker)
|
26
|
+
if shrinker.collection_key && shrinker.url_key
|
27
|
+
parsed_json.fetch(shrinker.collection_key).fetch(shrinker.url_key)
|
28
|
+
else
|
29
|
+
parsed_json.fetch(shrinker.url_key)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
25
33
|
def process_response(response, options, shrinker, json = JSONParser)
|
26
34
|
option = Options.new(options)
|
27
35
|
parsed_json = json.parse_json(response)
|
28
|
-
plain = parsed_json
|
36
|
+
plain = process_parse_options(parsed_json, shrinker.class)
|
29
37
|
|
30
38
|
if option.json? && option.qr_code?
|
31
39
|
if option.image_size?
|
@@ -84,6 +84,28 @@ module LinkShrink
|
|
84
84
|
def generate_chart_url(new_url, image_size = {})
|
85
85
|
fail "#{__method__} not implemented"
|
86
86
|
end
|
87
|
+
|
88
|
+
# Handles DSL definition of response structure to be parsed (JSON)
|
89
|
+
class << self
|
90
|
+
attr_accessor :collection_key, :url_key
|
91
|
+
|
92
|
+
# Helper method that yields into the other response structure methods
|
93
|
+
def response_options(&block)
|
94
|
+
yield
|
95
|
+
end
|
96
|
+
|
97
|
+
# Defines collection_key in response
|
98
|
+
# @param new_collection [String] collection_key
|
99
|
+
def collection(new_collection)
|
100
|
+
self.collection_key = new_collection
|
101
|
+
end
|
102
|
+
|
103
|
+
# Defines url_key in response
|
104
|
+
# @param new_url_key [String] url_key
|
105
|
+
def short_url(new_url_key)
|
106
|
+
self.url_key = new_url_key
|
107
|
+
end
|
108
|
+
end
|
87
109
|
end
|
88
110
|
end
|
89
111
|
end
|
@@ -5,6 +5,11 @@ module LinkShrink
|
|
5
5
|
# @author Jonah Ruiz <jonah@pixelhipsters.com>
|
6
6
|
# Implements Google's URL Shortener API
|
7
7
|
class Google < Base
|
8
|
+
# Defines response structure to be parsed
|
9
|
+
response_options do
|
10
|
+
short_url 'id'
|
11
|
+
end
|
12
|
+
|
8
13
|
# Returns URL base for API
|
9
14
|
# @return [String] api base url
|
10
15
|
def base_url
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module LinkShrink
|
4
|
+
module Shrinkers
|
5
|
+
# @author Jonah Ruiz <jonah@pixelhipsters.com>
|
6
|
+
# Implements Owly's URL Shortener API
|
7
|
+
class Owly < Base
|
8
|
+
# Defines response structure to be parsed
|
9
|
+
response_options do
|
10
|
+
collection 'results'
|
11
|
+
short_url 'shortUrl'
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns URL base for API
|
15
|
+
# @return [String] api base url
|
16
|
+
def base_url
|
17
|
+
'http://ow.ly/api/1.1/url/shorten'
|
18
|
+
end
|
19
|
+
|
20
|
+
# Returns URL query parameters
|
21
|
+
# @return [String] query parameters to be used in request
|
22
|
+
def api_query_parameter
|
23
|
+
"?apiKey=#{api_key}&longUrl=#{url}"
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns full api url
|
27
|
+
# @return [String] full api url with query parameters
|
28
|
+
def api_url
|
29
|
+
base_url.concat api_query_parameter
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/link_shrink/version.rb
CHANGED
data/lib/link_shrink.rb
CHANGED
@@ -6,6 +6,7 @@ require 'link_shrink/shrinkers/base'
|
|
6
6
|
require 'link_shrink/shrinkers/google'
|
7
7
|
require 'link_shrink/shrinkers/tinyurl'
|
8
8
|
require 'link_shrink/shrinkers/isgd'
|
9
|
+
require 'link_shrink/shrinkers/owly'
|
9
10
|
require 'link_shrink/config'
|
10
11
|
|
11
12
|
# @author Jonah Ruiz <jonah@pixelhipsters.com>
|
@@ -50,4 +50,16 @@ describe LinkShrink::Shrinkers::Google do
|
|
50
50
|
.to eq("{\"longUrl\":\"http://www.google.com\"}")
|
51
51
|
end
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
|
+
describe '#collection_key' do
|
55
|
+
it 'returns nil' do
|
56
|
+
expect(link_shrink.class.collection_key).to eq(nil)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#url_key' do
|
61
|
+
it 'returns key used to extract data from response' do
|
62
|
+
expect(link_shrink.class.url_key).to eq('id')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LinkShrink::Shrinkers::Owly do
|
4
|
+
include_examples 'shared_examples'
|
5
|
+
|
6
|
+
let(:link_shrink) { described_class.new }
|
7
|
+
let(:owly) { 'http://ow.ly/api/1.1/url/shorten' }
|
8
|
+
let(:key) { ENV['OWLY_URL_KEY'] }
|
9
|
+
|
10
|
+
describe '#sub_klass' do
|
11
|
+
it 'returns the inherited subclass name' do
|
12
|
+
expect(link_shrink.sub_klass).to eq('Owly')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#base_url' do
|
17
|
+
it 'returns the base_url for the API' do
|
18
|
+
expect(link_shrink.base_url)
|
19
|
+
.to eq(owly)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#api_key' do
|
24
|
+
it 'returns the API key' do
|
25
|
+
expect(link_shrink.api_key)
|
26
|
+
.to eq(key)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#api_url' do
|
31
|
+
it 'returns full api url' do
|
32
|
+
link_shrink.stub(:url).and_return('http://www.google.com')
|
33
|
+
|
34
|
+
expect(link_shrink.api_url)
|
35
|
+
.to eq("http://ow.ly/api/1.1/url/shorten?apiKey=#{key}&longUrl=http://www.google.com")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#api_query_parameter' do
|
40
|
+
it 'returns query parameter' do
|
41
|
+
link_shrink.stub(:url).and_return('http://www.google.com')
|
42
|
+
expect(link_shrink.api_query_parameter).to eq("?apiKey=#{key}&longUrl=http://www.google.com")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#content_type' do
|
47
|
+
it 'returns text/plain' do
|
48
|
+
expect(link_shrink.content_type).to eq('application/json')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe '#collection_key' do
|
53
|
+
it 'returns key of collection' do
|
54
|
+
expect(link_shrink.class.collection_key).to eq('results')
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#url_key' do
|
59
|
+
it 'returns key used to extract data from response' do
|
60
|
+
expect(link_shrink.class.url_key).to eq('shortUrl')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'integration' do
|
65
|
+
it 'returns correct shortUrl' do
|
66
|
+
LinkShrink.configure { |config| config.api = 'Owly' }
|
67
|
+
expect(LinkShrink.shrink_url('www.google.com'))
|
68
|
+
.to eq('http://ow.ly/22rqvf')
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -108,4 +108,15 @@ describe LinkShrink::Shrinkers::Base do
|
|
108
108
|
end
|
109
109
|
end
|
110
110
|
end
|
111
|
-
|
111
|
+
|
112
|
+
describe '#collection_key' do
|
113
|
+
it 'returns nil' do
|
114
|
+
expect(link_shrink.class.collection_key).to eq(nil)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
describe '#url_key' do
|
118
|
+
it 'returns key used to extract data from response' do
|
119
|
+
expect(link_shrink.class.url_key).to eq(nil)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: link_shrink
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -184,6 +184,7 @@ files:
|
|
184
184
|
- lib/link_shrink/shrinkers/base.rb
|
185
185
|
- lib/link_shrink/shrinkers/google.rb
|
186
186
|
- lib/link_shrink/shrinkers/isgd.rb
|
187
|
+
- lib/link_shrink/shrinkers/owly.rb
|
187
188
|
- lib/link_shrink/shrinkers/tinyurl.rb
|
188
189
|
- lib/link_shrink/version.rb
|
189
190
|
- link_shrink.gemspec
|
@@ -196,6 +197,7 @@ files:
|
|
196
197
|
- spec/link_shrink/json_parser_spec.rb
|
197
198
|
- spec/link_shrink/link_shrink_spec.rb
|
198
199
|
- spec/link_shrink/options_spec.rb
|
200
|
+
- spec/link_shrink/owly_spec.rb
|
199
201
|
- spec/link_shrink/request_spec.rb
|
200
202
|
- spec/link_shrink/shrinker_base_spec.rb
|
201
203
|
- spec/link_shrink/tiny_url_spec.rb
|
@@ -232,7 +234,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
232
234
|
version: '0'
|
233
235
|
segments:
|
234
236
|
- 0
|
235
|
-
hash: -
|
237
|
+
hash: -2671621547158005072
|
236
238
|
requirements: []
|
237
239
|
rubyforge_project:
|
238
240
|
rubygems_version: 1.8.25
|
@@ -249,6 +251,7 @@ test_files:
|
|
249
251
|
- spec/link_shrink/json_parser_spec.rb
|
250
252
|
- spec/link_shrink/link_shrink_spec.rb
|
251
253
|
- spec/link_shrink/options_spec.rb
|
254
|
+
- spec/link_shrink/owly_spec.rb
|
252
255
|
- spec/link_shrink/request_spec.rb
|
253
256
|
- spec/link_shrink/shrinker_base_spec.rb
|
254
257
|
- spec/link_shrink/tiny_url_spec.rb
|