link_shrink 0.0.6 → 0.0.7
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.
- data/.rspec +1 -1
- data/Changelog.md +22 -1
- data/README.md +22 -26
- data/lib/link_shrink.rb +7 -22
- data/lib/link_shrink/cli.rb +42 -46
- data/lib/link_shrink/config.rb +5 -5
- data/lib/link_shrink/request.rb +13 -29
- data/lib/link_shrink/shrinkers/base.rb +52 -11
- data/lib/link_shrink/shrinkers/google.rb +0 -10
- data/lib/link_shrink/shrinkers/owly.rb +1 -0
- data/lib/link_shrink/util.rb +13 -0
- data/lib/link_shrink/version.rb +2 -2
- data/link_shrink.gemspec +3 -3
- data/spec/link_shrink/link_shrink_spec.rb +7 -83
- data/spec/link_shrink/request_spec.rb +1 -43
- data/spec/link_shrink/{google_spec.rb → shrinkers/google_spec.rb} +0 -16
- data/spec/link_shrink/{isgd_spec.rb → shrinkers/isgd_spec.rb} +0 -0
- data/spec/link_shrink/{owly_spec.rb → shrinkers/owly_spec.rb} +1 -1
- data/spec/link_shrink/{shrinker_base_spec.rb → shrinkers/shrinker_base_spec.rb} +7 -0
- data/spec/link_shrink/{tiny_url_spec.rb → shrinkers/tiny_url_spec.rb} +0 -0
- data/spec/shared_examples.rb +0 -4
- metadata +20 -19
data/.rspec
CHANGED
data/Changelog.md
CHANGED
@@ -1,5 +1,26 @@
|
|
1
1
|
# LinkShrink Changelog
|
2
2
|
|
3
|
+
## 0.0.7
|
4
|
+
|
5
|
+
Released January 19, 2014 ([0.0.7](https://github.com/jonahoffline/link_shrink/tree/v0.0.7)).
|
6
|
+
|
7
|
+
* Remove old features documentation and examples from README
|
8
|
+
* Refactor LinkShrink, Request modules
|
9
|
+
* Refactor LinkShrink::CLI class and update documentation
|
10
|
+
* Remove json and qr_code options from CLI class
|
11
|
+
* Remove charts, qr_codes and json options tests
|
12
|
+
* Add error parameter to :response_options block in Owly Shrinker
|
13
|
+
* Fix indentation in Owly spec
|
14
|
+
* Add error_key and error for internal DSL in Shrinkers::Base
|
15
|
+
* Remove rescue method from :shrink_url
|
16
|
+
* Add Util module for helper methods
|
17
|
+
* Update documentation for ::Shrinkers::Base class
|
18
|
+
* Update documentation in Config module
|
19
|
+
* Move shrinkers specs into its own shrinkers folder to mirror lib structure
|
20
|
+
* Add a Bitdeli badge to README
|
21
|
+
* Update development dependencies for `webmock`, `vcr` and `simplecov`
|
22
|
+
* Update version to 0.0.7
|
23
|
+
|
3
24
|
## 0.0.6
|
4
25
|
|
5
26
|
Released September 8, 2013 ([0.0.6](https://github.com/jonahoffline/link_shrink/tree/v0.0.6)).
|
@@ -64,4 +85,4 @@ Released July 16, 2013 ([0.0.2](https://github.com/jonahoffline/link_shrink/tree
|
|
64
85
|
|
65
86
|
Released June 24, 2013 ([0.0.1](https://github.com/jonahoffline/link_shrink/tree/v0.0.1)).
|
66
87
|
|
67
|
-
* Initial Release.
|
88
|
+
* Initial Release.
|
data/README.md
CHANGED
@@ -11,14 +11,16 @@ A Ruby Gem and Command-Line Application for shrinking those long and nasty links
|
|
11
11
|
* Owly
|
12
12
|
* **More to come...**
|
13
13
|
|
14
|
-
Installation
|
15
|
-
|
14
|
+
## Installation
|
15
|
+
|
16
16
|
|
17
17
|
$ gem install link_shrink
|
18
18
|
|
19
19
|
|
20
|
-
Setup
|
21
|
-
|
20
|
+
## Setup
|
21
|
+
|
22
|
+
You can use any shrinker listed without worrying about the api key except for 'Owly'.
|
23
|
+
Google let's you use its api without an api key, but providing one will generate metrics in their dashboard.
|
22
24
|
|
23
25
|
## Google
|
24
26
|
Works best with a Google URL API key. You can sign-up for a free one at
|
@@ -39,14 +41,13 @@ Sign-up for a free API key at [Owly](http://ow.ly/).
|
|
39
41
|
Set your apikey as an environment variable:
|
40
42
|
|
41
43
|
$ export GOOGLE_URL_KEY='your_api_key_here'
|
42
|
-
|
43
44
|
|
44
45
|
$ export OWLY_URL_KEY='your_api_key_here'
|
45
46
|
|
46
|
-
You could also save it in your
|
47
|
+
You could also save it in your `~/.bash_profile` or `~/.zshrc`
|
47
48
|
|
48
49
|
|
49
|
-
## Usage
|
50
|
+
## Usage
|
50
51
|
|
51
52
|
Ruby:
|
52
53
|
|
@@ -56,18 +57,6 @@ require 'link_shrink'
|
|
56
57
|
LinkShrink.shrink_url("http://www.ruby-lang.org")
|
57
58
|
=> "http://goo.gl/QuXj"
|
58
59
|
|
59
|
-
LinkShrink.shrink_url("http://www.ruby-lang.org", { :json => true })
|
60
|
-
=> "{\"kind\":\"urlshortener#url\",\"id\":\"http://goo.gl/MprR\",\"longUrl\":\"http://www.ruby-lang.org/\"}"
|
61
|
-
|
62
|
-
LinkShrink.shrink_url("http://www.ruby-lang.org", { :qr_code => true })
|
63
|
-
=> "http://goo.gl/QuXj.qr"
|
64
|
-
|
65
|
-
LinkShrink.shrink_url("http://www.ruby-lang.org", { :qr_code => true, image_size: '200x200' })
|
66
|
-
=> "http://chart.googleapis.com/chart?cht=qr&chs=200x200&choe=UTF-8&chld=H&chl=http://goo.gl/MprR"
|
67
|
-
|
68
|
-
LinkShrink.shrink_url("http://www.ruby-lang.org", { json: true, qr_code: true, image_size: '300x300' })
|
69
|
-
=> "{\"kind\":\"urlshortener#url\",\"id\":\"http://goo.gl/MprR\",\"longUrl\":\"http://www.ruby-lang.org/\",\"qr_code\":\"http://chart.googleapis.com/chart?cht=qr&chs=300x300&choe=UTF-8&chld=H&chl=http://goo.gl/MprR\"}"
|
70
|
-
|
71
60
|
```
|
72
61
|
|
73
62
|
To change the default shrinker (Google API):
|
@@ -75,12 +64,12 @@ To change the default shrinker (Google API):
|
|
75
64
|
|
76
65
|
```ruby
|
77
66
|
LinkShrink.configure do |c|
|
78
|
-
c.api = '
|
67
|
+
c.api = 'IsGd'
|
79
68
|
end
|
80
69
|
|
81
70
|
# or
|
82
71
|
|
83
|
-
LinkShrink::Config.api = '
|
72
|
+
LinkShrink::Config.api = 'TinyUrl'
|
84
73
|
|
85
74
|
LinkShrink.shrink_url('http://www.google.com')
|
86
75
|
=> "http://tinyurl.com/1c2"
|
@@ -102,7 +91,7 @@ In your terminal:
|
|
102
91
|
$ linkshrink --owly http://www.rubyrogues.com
|
103
92
|
http://ow.ly/22rwSe
|
104
93
|
|
105
|
-
### Command-Line Options
|
94
|
+
### Command-Line Options
|
106
95
|
|
107
96
|
Shrinkers:
|
108
97
|
|
@@ -111,12 +100,16 @@ Shrinkers:
|
|
111
100
|
* -om --owly - use Owly API
|
112
101
|
* -g, --google - use Google API (Default)
|
113
102
|
|
114
|
-
|
103
|
+
Additional options:
|
115
104
|
|
116
|
-
* -j, --json - return JSON response
|
117
|
-
* -q, --qrcode - return QR Code
|
118
105
|
* -h, --help - show help message
|
119
106
|
|
107
|
+
## Todo
|
108
|
+
* Batch list (with selected api or specified for operation)
|
109
|
+
* Save to file (html, text)
|
110
|
+
* Bitly Shrinker API
|
111
|
+
* Parallel HTTP Requests
|
112
|
+
|
120
113
|
## Author
|
121
114
|
* [Jonah Ruiz](http://www.pixelhipsters.com)
|
122
115
|
|
@@ -126,4 +119,7 @@ Format and additional options:
|
|
126
119
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
127
120
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
128
121
|
4. Push to the branch (`git push origin my-new-feature`)
|
129
|
-
5. Create new Pull Request
|
122
|
+
5. Create new Pull Request
|
123
|
+
|
124
|
+
[](https://bitdeli.com/free "Bitdeli Badge")
|
125
|
+
|
data/lib/link_shrink.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'link_shrink/version'
|
2
2
|
require 'link_shrink/options'
|
3
|
+
require 'link_shrink/util'
|
3
4
|
require 'link_shrink/request'
|
4
5
|
require 'link_shrink/json_parser'
|
5
6
|
require 'link_shrink/shrinkers/base'
|
@@ -10,33 +11,17 @@ require 'link_shrink/shrinkers/owly'
|
|
10
11
|
require 'link_shrink/config'
|
11
12
|
|
12
13
|
# @author Jonah Ruiz <jonah@pixelhipsters.com>
|
13
|
-
# Creates a short
|
14
|
+
# Creates a short URLs
|
14
15
|
module LinkShrink
|
15
16
|
extend self
|
16
17
|
include LinkShrink::Request
|
17
|
-
# Returns a short URL
|
18
|
-
# example: shrink_url('http://www.wtf.com'
|
19
|
-
# example: shrink_url('http://www.wtf.com', { qr_code: true })
|
18
|
+
# Returns a short URL
|
19
|
+
# example: shrink_url('http://www.wtf.com')
|
20
20
|
#
|
21
21
|
# @param url [String] long URL to be shortened
|
22
|
-
# @
|
23
|
-
|
24
|
-
|
25
|
-
process_request(url, options)
|
26
|
-
rescue
|
27
|
-
'Problem generating short URL. Try again.'
|
28
|
-
end
|
29
|
-
|
30
|
-
# Returns a QR code URL
|
31
|
-
# example: generate_qr_code('http://www.wtf.com', { image_size: '300x300' })
|
32
|
-
#
|
33
|
-
# @param url [String] long URL to be shortened
|
34
|
-
# @param options [Hash] image_size: '300x300' for a custom size
|
35
|
-
# @return [String] QR code URL using default or custom size
|
36
|
-
def generate_qr_code(url, options = {})
|
37
|
-
new_url = process_request(url, {})
|
38
|
-
image_size = options.fetch(:image_size, {})
|
39
|
-
Config.api.generate_chart_url(new_url, image_size)
|
22
|
+
# @return [String] generated short URL
|
23
|
+
def shrink_url(url)
|
24
|
+
process_request(url)
|
40
25
|
end
|
41
26
|
|
42
27
|
# Yield's to Config for options
|
data/lib/link_shrink/cli.rb
CHANGED
@@ -1,60 +1,60 @@
|
|
1
1
|
require 'optparse'
|
2
|
+
require 'ostruct'
|
2
3
|
|
3
4
|
module LinkShrink
|
4
5
|
# @author Jonah Ruiz <jonah@pixelhipsters.com>
|
5
6
|
# A Simple class for the executable version of the gem
|
6
7
|
class CLI
|
8
|
+
include LinkShrink::Util
|
7
9
|
|
10
|
+
# @!attribute [r] options
|
11
|
+
# @return [String] options for api settings
|
12
|
+
attr_reader :options
|
8
13
|
# @param args [Array<String>] The command-line arguments
|
9
14
|
def initialize(args)
|
10
|
-
@args = args
|
15
|
+
@args, @options = args, OpenStruct.new(default_options)
|
11
16
|
end
|
12
17
|
|
13
|
-
|
14
|
-
|
15
|
-
def set_options(opts)
|
16
|
-
%w(@json @qr_code @tiny_url)
|
17
|
-
@json = false
|
18
|
-
@qr_code = false
|
19
|
-
@tiny_url = false
|
20
|
-
@google = false
|
21
|
-
@is_gd = false
|
22
|
-
@owly = false
|
23
|
-
opts.version = LinkShrink::VERSION
|
24
|
-
opts.banner = <<MSG
|
18
|
+
def banner
|
19
|
+
<<MSG
|
25
20
|
Usage: link_shrink [OPTION] [URL]
|
26
21
|
Description:
|
27
22
|
LinkShrink, Turn long and nasty links into short urls.
|
28
23
|
|
29
24
|
Options:
|
30
25
|
MSG
|
31
|
-
|
32
|
-
opts.on_head('-t', '--tinyurl', 'use TinyURL') do
|
33
|
-
@tiny_url = :true
|
34
|
-
end
|
26
|
+
end
|
35
27
|
|
36
|
-
|
37
|
-
|
28
|
+
def default_options
|
29
|
+
apis = (available_shrinkers).reduce({}) do |opts, key|
|
30
|
+
opts[key] = false
|
31
|
+
opts
|
38
32
|
end
|
39
33
|
|
40
|
-
|
41
|
-
|
42
|
-
|
34
|
+
{}.merge({
|
35
|
+
api: apis.merge(Google: true),
|
36
|
+
banner: banner,
|
37
|
+
version: LinkShrink::VERSION
|
38
|
+
})
|
39
|
+
end
|
43
40
|
|
44
|
-
|
45
|
-
|
46
|
-
|
41
|
+
# Configures the arguments for the command
|
42
|
+
# @param opts [OptionParser]
|
43
|
+
def set_options(opts)
|
44
|
+
opts.version, opts.banner = options.version, options.banner
|
45
|
+
opts.set_program_name 'LinkShrink'
|
47
46
|
|
48
|
-
|
49
|
-
|
50
|
-
end
|
47
|
+
options.api.map do |k, v|
|
48
|
+
arg = k.to_s.downcase
|
51
49
|
|
52
|
-
|
53
|
-
|
50
|
+
opts.on_head("-#{arg[0]}", "--#{arg}", argument_text_for(k)) do
|
51
|
+
options.api[k] = true
|
52
|
+
end
|
54
53
|
end
|
55
54
|
|
56
|
-
opts.on_tail('-v', '--version',
|
57
|
-
|
55
|
+
opts.on_tail('-v', '--version',
|
56
|
+
'display the version of LinkShrink and exit') do
|
57
|
+
puts opts.ver
|
58
58
|
exit
|
59
59
|
end
|
60
60
|
|
@@ -64,6 +64,10 @@ MSG
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
+
def argument_text_for(option)
|
68
|
+
"use #{option}".concat option.eql?(:Google) ? ' (Default)' : ''
|
69
|
+
end
|
70
|
+
|
67
71
|
# Parses the command-line arguments and runs the executable
|
68
72
|
# @return [String] The short url or argument passed
|
69
73
|
def parse
|
@@ -74,26 +78,18 @@ MSG
|
|
74
78
|
end
|
75
79
|
|
76
80
|
def process_url
|
77
|
-
api = select_api
|
78
|
-
|
79
|
-
LinkShrink.shrink_url(@args.last
|
81
|
+
LinkShrink.configure { |config| config.api = select_api }
|
82
|
+
|
83
|
+
LinkShrink.shrink_url(@args.last)
|
80
84
|
end
|
81
85
|
|
82
86
|
def url_present?
|
83
|
-
|
87
|
+
regexp = /^(http?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
|
88
|
+
!!(@args.last =~ regexp)
|
84
89
|
end
|
85
90
|
|
86
91
|
def select_api
|
87
|
-
|
88
|
-
when @tiny_url
|
89
|
-
'TinyUrl'
|
90
|
-
when @is_gd
|
91
|
-
'IsGd'
|
92
|
-
when @owly
|
93
|
-
'Owly'
|
94
|
-
else
|
95
|
-
'Google'
|
96
|
-
end
|
92
|
+
options.api.select { |_, v| v }.keys.last.to_s
|
97
93
|
end
|
98
94
|
end
|
99
95
|
end
|
data/lib/link_shrink/config.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
module LinkShrink
|
2
|
+
# @author Jonah Ruiz <jonah@pixelhipsters.com>
|
3
|
+
# Configurable module for Shrinkers-related settings
|
2
4
|
module Config
|
3
|
-
# @author Jonah Ruiz <jonah@pixelhipsters.com>
|
4
|
-
# Configurable module for integrating with other URL APIs
|
5
5
|
extend self
|
6
6
|
|
7
|
-
#
|
8
|
-
# @
|
7
|
+
# @!attribute api_key
|
8
|
+
# @return [String] API key to be used
|
9
9
|
attr_accessor :api_key
|
10
10
|
|
11
11
|
# Sets API to be used
|
@@ -15,7 +15,7 @@ module LinkShrink
|
|
15
15
|
end
|
16
16
|
|
17
17
|
# Returns API used
|
18
|
-
#
|
18
|
+
# Uses Google by default
|
19
19
|
# @return [LinkShrink::Shrinkers::Google] instance
|
20
20
|
def api
|
21
21
|
@api || LinkShrink::Shrinkers::Google.new
|
data/lib/link_shrink/request.rb
CHANGED
@@ -10,51 +10,35 @@ module LinkShrink
|
|
10
10
|
|
11
11
|
module_function
|
12
12
|
|
13
|
-
def process_request(url,
|
14
|
-
parse(request(url, shrinker).body,
|
13
|
+
def process_request(url, shrinker = LinkShrink::Config.api)
|
14
|
+
parse(request(url, shrinker).body, shrinker)
|
15
15
|
end
|
16
16
|
|
17
|
-
def parse(response,
|
17
|
+
def parse(response, shrinker)
|
18
18
|
if shrinker.content_type.eql?('text/plain')
|
19
19
|
response
|
20
20
|
else
|
21
|
-
process_response(response,
|
21
|
+
process_response(response, shrinker)
|
22
22
|
end
|
23
|
+
rescue
|
24
|
+
"Error #{response}"
|
23
25
|
end
|
24
26
|
|
25
27
|
def process_parse_options(parsed_json, shrinker)
|
26
28
|
if shrinker.collection_key && shrinker.url_key
|
27
|
-
parsed_json
|
29
|
+
parsed_json
|
30
|
+
.fetch(shrinker.collection_key)
|
31
|
+
.fetch(shrinker.url_key)
|
28
32
|
else
|
29
33
|
parsed_json.fetch(shrinker.url_key)
|
30
34
|
end
|
35
|
+
rescue
|
36
|
+
parsed_json.fetch(shrinker.error_key) { 'Error parsing the request!'}
|
31
37
|
end
|
32
38
|
|
33
|
-
def process_response(response,
|
34
|
-
option = Options.new(options)
|
39
|
+
def process_response(response, shrinker, json = JSONParser)
|
35
40
|
parsed_json = json.parse_json(response)
|
36
|
-
|
37
|
-
|
38
|
-
if option.json? && option.qr_code?
|
39
|
-
if option.image_size?
|
40
|
-
return parsed_json.merge(qr_code: shrinker.generate_chart_url(plain, options.fetch(:image_size))).to_json
|
41
|
-
end
|
42
|
-
return parsed_json.merge(qr_code: shrinker.generate_chart_url(plain)).to_json
|
43
|
-
end
|
44
|
-
|
45
|
-
case
|
46
|
-
when option.json?
|
47
|
-
json.cleanup_json(response)
|
48
|
-
when option.qr_code?
|
49
|
-
if option.image_size?
|
50
|
-
return shrinker.generate_chart_url(plain, options.fetch(:image_size))
|
51
|
-
end
|
52
|
-
shrinker.generate_chart_url(plain)
|
53
|
-
when option.image_size?
|
54
|
-
shrinker.generate_chart_url(plain, options.fetch(:image_size))
|
55
|
-
else
|
56
|
-
plain
|
57
|
-
end
|
41
|
+
process_parse_options(parsed_json, shrinker.class)
|
58
42
|
end
|
59
43
|
|
60
44
|
# Calls URL API
|
@@ -3,10 +3,31 @@ require 'uri'
|
|
3
3
|
module LinkShrink
|
4
4
|
module Shrinkers
|
5
5
|
# @author Jonah Ruiz <jonah@pixelhipsters.com>
|
6
|
-
#
|
6
|
+
# Base class for implementing other URL APIs
|
7
|
+
#
|
8
|
+
# @abstract Subclass and override methods as needed
|
9
|
+
# @example Implement a Shrinker class
|
10
|
+
# class Shorty < Base
|
11
|
+
#
|
12
|
+
# def base_url
|
13
|
+
# 'http://shorty.com/api/2.0/shorten'
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# def api_query_parameter
|
17
|
+
# "?url=#{url}"
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# def api_url
|
21
|
+
# base_url.concat api_query_parameter
|
22
|
+
# end
|
23
|
+
# end
|
7
24
|
class Base
|
25
|
+
|
26
|
+
# @!attribute url
|
27
|
+
# @return [String] long url to shrink
|
8
28
|
attr_reader :url
|
9
|
-
|
29
|
+
|
30
|
+
# Callback method to define a sub_klass method for reference
|
10
31
|
# @return [String] inherited class name
|
11
32
|
def self.inherited(sub_klass)
|
12
33
|
sub_klass.class_eval do
|
@@ -16,12 +37,18 @@ module LinkShrink
|
|
16
37
|
end
|
17
38
|
end
|
18
39
|
|
19
|
-
#
|
40
|
+
# @overload base_url
|
41
|
+
# URL base for API requests
|
42
|
+
# @raise [String] unless implemented on inheriting class
|
43
|
+
# @return [String] api base URL
|
20
44
|
def base_url
|
21
45
|
fail "#{__method__} not implemented"
|
22
46
|
end
|
23
47
|
|
24
|
-
#
|
48
|
+
# @overload api_query_parameter
|
49
|
+
# URL query parameters
|
50
|
+
# @raise [String] unless implemented on inheriting class
|
51
|
+
# @return [String] query parameters to be used in request
|
25
52
|
def api_query_parameter
|
26
53
|
fail "#{__method__} not implemented"
|
27
54
|
end
|
@@ -54,16 +81,21 @@ module LinkShrink
|
|
54
81
|
# @param new_url [String] url to be parsed
|
55
82
|
# @return [String] parsed URL
|
56
83
|
def sanitize_url(new_url)
|
57
|
-
URI.encode(
|
84
|
+
URI.encode(
|
85
|
+
!(new_url =~ /^(http?:\/\/)?/) ? "http://#{new_url}" : new_url
|
86
|
+
)
|
58
87
|
end
|
59
88
|
|
60
|
-
#
|
89
|
+
# @overload http_method
|
90
|
+
# Returns HTTP method to be used in request
|
91
|
+
# override +:get+ with +:post+
|
61
92
|
# @return [Symbol] http method
|
62
93
|
def http_method
|
63
94
|
:get
|
64
95
|
end
|
65
96
|
|
66
|
-
#
|
97
|
+
# @overload content_type
|
98
|
+
# Returns Content-Type to be used in Request headers
|
67
99
|
# @return [String] content-type
|
68
100
|
def content_type
|
69
101
|
'application/json'
|
@@ -86,8 +118,9 @@ module LinkShrink
|
|
86
118
|
end
|
87
119
|
|
88
120
|
# Handles DSL definition of response structure to be parsed (JSON)
|
121
|
+
# @!group ShrinkerDSL
|
89
122
|
class << self
|
90
|
-
attr_accessor :collection_key, :url_key
|
123
|
+
attr_accessor :collection_key, :url_key, :error_key
|
91
124
|
|
92
125
|
# Helper method that yields into the other response structure methods
|
93
126
|
def response_options(&block)
|
@@ -101,11 +134,19 @@ module LinkShrink
|
|
101
134
|
end
|
102
135
|
|
103
136
|
# Defines url_key in response
|
104
|
-
# @param
|
105
|
-
def short_url(
|
106
|
-
self.url_key =
|
137
|
+
# @param short_url_key [String] url_key
|
138
|
+
def short_url(short_url_key)
|
139
|
+
self.url_key = short_url_key
|
140
|
+
end
|
141
|
+
|
142
|
+
# Defines error_key in response
|
143
|
+
# @param new_error_key [String] error_key
|
144
|
+
def error(new_error_key = 'error')
|
145
|
+
self.error_key = new_error_key
|
107
146
|
end
|
108
147
|
end
|
148
|
+
# @!endgroup
|
149
|
+
|
109
150
|
end
|
110
151
|
end
|
111
152
|
end
|
@@ -28,16 +28,6 @@ module LinkShrink
|
|
28
28
|
{ 'longUrl' => url }.to_json
|
29
29
|
end
|
30
30
|
|
31
|
-
# Generates QR code URL
|
32
|
-
# @return [String] QR code url with '150x150' or custom image size
|
33
|
-
def generate_chart_url(new_url, image_size = {})
|
34
|
-
return "#{new_url}.qr" if image_size.empty?
|
35
|
-
|
36
|
-
chart_url = 'http://chart.googleapis.com/chart'
|
37
|
-
params = "?cht=qr&chs=#{image_size}&choe=UTF-8&chld=H&chl=#{new_url}"
|
38
|
-
chart_url.concat(params)
|
39
|
-
end
|
40
|
-
|
41
31
|
# Returns HTTP method to be used in request
|
42
32
|
# @return [Symbol] post http method
|
43
33
|
def http_method
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module LinkShrink
|
2
|
+
# @author Jonah Ruiz <jonah@pixelhipsters.com>
|
3
|
+
# Provides utility methods
|
4
|
+
module Util
|
5
|
+
module_function
|
6
|
+
|
7
|
+
# Returns list of Url Shrinkers
|
8
|
+
# @return [Array<String>] Url shrinker classes
|
9
|
+
def available_shrinkers
|
10
|
+
LinkShrink::Shrinkers.constants - [:Base]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/link_shrink/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module LinkShrink
|
2
|
-
VERSION = '0.0.
|
3
|
-
end
|
2
|
+
VERSION = '0.0.7'
|
3
|
+
end
|
data/link_shrink.gemspec
CHANGED
@@ -24,9 +24,9 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_development_dependency 'bundler', '~> 1.3'
|
25
25
|
spec.add_development_dependency 'rspec', '~> 2.14.1'
|
26
26
|
spec.add_development_dependency 'rake'
|
27
|
-
spec.add_development_dependency 'simplecov', '~> 0.
|
27
|
+
spec.add_development_dependency 'simplecov', '~> 0.8.2'
|
28
28
|
spec.add_development_dependency 'simplecov-gem-adapter', '~> 1.0.1'
|
29
29
|
spec.add_development_dependency 'coveralls', '~> 0.6.7'
|
30
|
-
spec.add_development_dependency 'webmock', '~> 1.
|
31
|
-
spec.add_development_dependency 'vcr', '~> 2.
|
30
|
+
spec.add_development_dependency 'webmock', '~> 1.17.1'
|
31
|
+
spec.add_development_dependency 'vcr', '~> 2.8.0'
|
32
32
|
end
|
@@ -3,92 +3,14 @@ require 'spec_helper'
|
|
3
3
|
describe LinkShrink do
|
4
4
|
include_examples 'shared_examples'
|
5
5
|
|
6
|
+
before :all do
|
7
|
+
LinkShrink.configure { |c| c.api = 'Google' }
|
8
|
+
end
|
9
|
+
|
6
10
|
describe '.shrink_url', :vcr => 'google_url' do
|
7
11
|
it 'creates a short url' do
|
8
12
|
expect(link_shrink.shrink_url(url)).to eq(short_url)
|
9
13
|
end
|
10
|
-
|
11
|
-
context 'when called with additional options' do
|
12
|
-
context 'json option' do
|
13
|
-
it 'returns JSON when set to true' do
|
14
|
-
expect(link_shrink.shrink_url(url, { :json => true }))
|
15
|
-
.to eq(json_response)
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'returns default response when set to false' do
|
19
|
-
expect(link_shrink.shrink_url(url, { json: false }))
|
20
|
-
.to eq(short_url)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
context 'qr_code option' do
|
25
|
-
it 'returns QR code when set to true' do
|
26
|
-
expect(link_shrink.shrink_url(url, { qr_code: true }))
|
27
|
-
.to eq(qrcode_url)
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'returns default response when set to false' do
|
31
|
-
expect(link_shrink.shrink_url(url, { qr_code: false }))
|
32
|
-
.to eq(short_url)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'returns default response when called with empty hash' do
|
37
|
-
expect(link_shrink.shrink_url(url, {})).to eq(short_url)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
context 'when called with json and qr_code options' do
|
42
|
-
it 'returns qr_code in JSON' do
|
43
|
-
expect(link_shrink.shrink_url(url, { :json => true, :qr_code => true }))
|
44
|
-
.to eq(json_qr_code)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
context 'when called with image_size: 200x200' do
|
49
|
-
it 'returns QR code link with custom size' do
|
50
|
-
expect(link_shrink.shrink_url(url, {:qr_code => true, :image_size => "200x200"}))
|
51
|
-
.to eq(chart_url)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
context 'when called with no qr_code and image_size: 200x200' do
|
56
|
-
it 'returns QR code link with custom size' do
|
57
|
-
expect(link_shrink.shrink_url(url, {:image_size => "200x200"}))
|
58
|
-
.to eq(chart_url)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context 'when called with json, qr_code and image_size: 300x300' do
|
63
|
-
let(:options) { { json: true, qr_code: true, image_size: "300x300" } }
|
64
|
-
|
65
|
-
it 'returns QR code in JSON with custom size' do
|
66
|
-
expect(link_shrink.shrink_url(url, options))
|
67
|
-
.to eq(json_qr_code_custom)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
context 'when API is down' do
|
72
|
-
it 'raises rescue exception' do
|
73
|
-
link_shrink.stub(:request).and_return(nil)
|
74
|
-
|
75
|
-
expect(link_shrink.shrink_url('www.google.com'))
|
76
|
-
.to eq('Problem generating short URL. Try again.')
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
describe '.generate_qr_code' do
|
82
|
-
it 'creates a QR code' do
|
83
|
-
expect(link_shrink.generate_qr_code(url)).to eq(qrcode_url)
|
84
|
-
end
|
85
|
-
|
86
|
-
context 'when called with image_size: 200x200' do
|
87
|
-
it 'returns QR code with custom size' do
|
88
|
-
expect(link_shrink.generate_qr_code(url, { image_size: "200x200" }))
|
89
|
-
.to eq(chart_url)
|
90
|
-
end
|
91
|
-
end
|
92
14
|
end
|
93
15
|
|
94
16
|
describe '.configure' do
|
@@ -98,7 +20,9 @@ describe LinkShrink do
|
|
98
20
|
end
|
99
21
|
|
100
22
|
it 'sets API Shrinker' do
|
101
|
-
link_shrink.should_receive(:configure)
|
23
|
+
link_shrink.should_receive(:configure)
|
24
|
+
.and_yield(LinkShrink::Config)
|
25
|
+
|
102
26
|
link_shrink.configure { |config| api = 'TinyURL' }
|
103
27
|
end
|
104
28
|
end
|
@@ -7,48 +7,6 @@ describe LinkShrink::Request do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
let(:shrinker) { LinkShrink::Config.api }
|
10
|
-
let(:json_default) {{ :json => false }}
|
11
|
-
|
12
|
-
describe '.process_request' do
|
13
|
-
it 'calls request and returns short link' do
|
14
|
-
expect(link_shrink.process_request(url, json_default, shrinker))
|
15
|
-
.to eq(short_url)
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'when called with qr_code true, image_size 300x300' do
|
19
|
-
it 'returns QR code with custom size' do
|
20
|
-
options = { json: true, qr_code: true, image_size: '300x300' }
|
21
|
-
expect(link_shrink.process_request(url, options))
|
22
|
-
.to eq(json_qr_code_custom)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe '.process_response' do
|
28
|
-
context 'when called with json true' do
|
29
|
-
it 'returns json' do
|
30
|
-
response = link_shrink.request(url, shrinker).body
|
31
|
-
expect(link_shrink.process_response(response, {json: true}, shrinker))
|
32
|
-
.to eq(json_response)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context 'when called with json false or no options' do
|
37
|
-
it 'returns link' do
|
38
|
-
response = link_shrink.request(url, shrinker).body
|
39
|
-
expect(link_shrink.process_response(response, json_default, shrinker))
|
40
|
-
.to eq(short_url)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'when called with qr_code true' do
|
45
|
-
it 'returns QR code' do
|
46
|
-
response = link_shrink.request(url, shrinker).body
|
47
|
-
expect(link_shrink.process_response(response, { qr_code: true }, shrinker))
|
48
|
-
.to eq(qrcode_url)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
10
|
|
53
11
|
describe '.request' do
|
54
12
|
it 'is a Typhoeus::Request instance' do
|
@@ -63,7 +21,7 @@ describe LinkShrink::Request do
|
|
63
21
|
LinkShrink.configure { |c| c.api = 'TinyUrl' }
|
64
22
|
response = link_shrink.request(url, shrinker).body
|
65
23
|
|
66
|
-
expect(link_shrink.parse(response,
|
24
|
+
expect(link_shrink.parse(response, shrinker))
|
67
25
|
.to eq('http://tinyurl.com/1c2')
|
68
26
|
LinkShrink.configure { |c| c.api = 'Google' }
|
69
27
|
end
|
@@ -27,22 +27,6 @@ describe LinkShrink::Shrinkers::Google do
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
describe '#generate_chart_url' do
|
31
|
-
let(:chart_url) { "http://chart.googleapis.com/chart?cht=qr&chs=300x300&choe=UTF-8&chld=H&chl=http://goo.gl/fbsS" }
|
32
|
-
|
33
|
-
it 'returns a QR code' do
|
34
|
-
expect(link_shrink.generate_chart_url(short_url))
|
35
|
-
.to eq(qrcode_url)
|
36
|
-
end
|
37
|
-
|
38
|
-
context 'when called with optional image_size argument' do
|
39
|
-
it 'returns a QR code with custom image size' do
|
40
|
-
expect(link_shrink.generate_chart_url(short_url, '300x300'))
|
41
|
-
.to eq(chart_url)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
30
|
describe '#body_parameters' do
|
47
31
|
it 'returns body parameters for API request' do
|
48
32
|
link_shrink.stub(:url).and_return('http://www.google.com')
|
File without changes
|
@@ -65,7 +65,7 @@ describe LinkShrink::Shrinkers::Owly do
|
|
65
65
|
it 'returns correct shortUrl' do
|
66
66
|
LinkShrink.configure { |config| config.api = 'Owly' }
|
67
67
|
expect(LinkShrink.shrink_url('www.google.com'))
|
68
|
-
|
68
|
+
.to eq('http://ow.ly/22rqvf')
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
@@ -114,9 +114,16 @@ describe LinkShrink::Shrinkers::Base do
|
|
114
114
|
expect(link_shrink.class.collection_key).to eq(nil)
|
115
115
|
end
|
116
116
|
end
|
117
|
+
|
117
118
|
describe '#url_key' do
|
118
119
|
it 'returns key used to extract data from response' do
|
119
120
|
expect(link_shrink.class.url_key).to eq(nil)
|
120
121
|
end
|
121
122
|
end
|
123
|
+
|
124
|
+
describe '#error_key' do
|
125
|
+
it 'returns key used to extract data from response' do
|
126
|
+
expect(link_shrink.class.error_key).to eq(nil)
|
127
|
+
end
|
128
|
+
end
|
122
129
|
end
|
File without changes
|
data/spec/shared_examples.rb
CHANGED
@@ -12,11 +12,7 @@ shared_context 'shared_examples' do
|
|
12
12
|
let(:link_shrink) { described_class }
|
13
13
|
let(:url) { 'http://www.google.com' }
|
14
14
|
let(:short_url) { "http://goo.gl/fbsS" }
|
15
|
-
let(:qrcode_url) { 'http://goo.gl/fbsS.qr' }
|
16
15
|
|
17
16
|
let(:json_response) { fixture('response.json') }
|
18
|
-
let(:json_qr_code) { fixture('response_qr_code.json') }
|
19
|
-
let(:json_qr_code_custom) { fixture('response_qr_code_custom.json') }
|
20
17
|
let(:parsed_json) { { "kind" => "urlshortener#url", "id" => "http://goo.gl/fbsS", "longUrl" => "http://www.google.com/" } }
|
21
|
-
let(:chart_url) { "http://chart.googleapis.com/chart?cht=qr&chs=200x200&choe=UTF-8&chld=H&chl=http://goo.gl/fbsS" }
|
22
18
|
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.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-01-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: typhoeus
|
@@ -82,7 +82,7 @@ dependencies:
|
|
82
82
|
requirements:
|
83
83
|
- - ~>
|
84
84
|
- !ruby/object:Gem::Version
|
85
|
-
version: 0.
|
85
|
+
version: 0.8.2
|
86
86
|
type: :development
|
87
87
|
prerelease: false
|
88
88
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -90,7 +90,7 @@ dependencies:
|
|
90
90
|
requirements:
|
91
91
|
- - ~>
|
92
92
|
- !ruby/object:Gem::Version
|
93
|
-
version: 0.
|
93
|
+
version: 0.8.2
|
94
94
|
- !ruby/object:Gem::Dependency
|
95
95
|
name: simplecov-gem-adapter
|
96
96
|
requirement: !ruby/object:Gem::Requirement
|
@@ -130,7 +130,7 @@ dependencies:
|
|
130
130
|
requirements:
|
131
131
|
- - ~>
|
132
132
|
- !ruby/object:Gem::Version
|
133
|
-
version: 1.
|
133
|
+
version: 1.17.1
|
134
134
|
type: :development
|
135
135
|
prerelease: false
|
136
136
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -138,7 +138,7 @@ dependencies:
|
|
138
138
|
requirements:
|
139
139
|
- - ~>
|
140
140
|
- !ruby/object:Gem::Version
|
141
|
-
version: 1.
|
141
|
+
version: 1.17.1
|
142
142
|
- !ruby/object:Gem::Dependency
|
143
143
|
name: vcr
|
144
144
|
requirement: !ruby/object:Gem::Requirement
|
@@ -146,7 +146,7 @@ dependencies:
|
|
146
146
|
requirements:
|
147
147
|
- - ~>
|
148
148
|
- !ruby/object:Gem::Version
|
149
|
-
version: 2.
|
149
|
+
version: 2.8.0
|
150
150
|
type: :development
|
151
151
|
prerelease: false
|
152
152
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -154,7 +154,7 @@ dependencies:
|
|
154
154
|
requirements:
|
155
155
|
- - ~>
|
156
156
|
- !ruby/object:Gem::Version
|
157
|
-
version: 2.
|
157
|
+
version: 2.8.0
|
158
158
|
description: Shrink those long and nasty links into a shorter URL
|
159
159
|
email:
|
160
160
|
- jonah@pixelhipsters.com
|
@@ -186,21 +186,22 @@ files:
|
|
186
186
|
- lib/link_shrink/shrinkers/isgd.rb
|
187
187
|
- lib/link_shrink/shrinkers/owly.rb
|
188
188
|
- lib/link_shrink/shrinkers/tinyurl.rb
|
189
|
+
- lib/link_shrink/util.rb
|
189
190
|
- lib/link_shrink/version.rb
|
190
191
|
- link_shrink.gemspec
|
191
192
|
- spec/fixtures/response.json
|
192
193
|
- spec/fixtures/response_qr_code.json
|
193
194
|
- spec/fixtures/response_qr_code_custom.json
|
194
195
|
- spec/link_shrink/config_spec.rb
|
195
|
-
- spec/link_shrink/google_spec.rb
|
196
|
-
- spec/link_shrink/isgd_spec.rb
|
197
196
|
- spec/link_shrink/json_parser_spec.rb
|
198
197
|
- spec/link_shrink/link_shrink_spec.rb
|
199
198
|
- spec/link_shrink/options_spec.rb
|
200
|
-
- spec/link_shrink/owly_spec.rb
|
201
199
|
- spec/link_shrink/request_spec.rb
|
202
|
-
- spec/link_shrink/
|
203
|
-
- spec/link_shrink/
|
200
|
+
- spec/link_shrink/shrinkers/google_spec.rb
|
201
|
+
- spec/link_shrink/shrinkers/isgd_spec.rb
|
202
|
+
- spec/link_shrink/shrinkers/owly_spec.rb
|
203
|
+
- spec/link_shrink/shrinkers/shrinker_base_spec.rb
|
204
|
+
- spec/link_shrink/shrinkers/tiny_url_spec.rb
|
204
205
|
- spec/shared_examples.rb
|
205
206
|
- spec/spec_helper.rb
|
206
207
|
- spec/vcr_cassettes/LinkShrink/_shrink_url/creates_a_short_url.yml
|
@@ -234,7 +235,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
234
235
|
version: '0'
|
235
236
|
segments:
|
236
237
|
- 0
|
237
|
-
hash: -
|
238
|
+
hash: -4473325036172331892
|
238
239
|
requirements: []
|
239
240
|
rubyforge_project:
|
240
241
|
rubygems_version: 1.8.25
|
@@ -246,15 +247,15 @@ test_files:
|
|
246
247
|
- spec/fixtures/response_qr_code.json
|
247
248
|
- spec/fixtures/response_qr_code_custom.json
|
248
249
|
- spec/link_shrink/config_spec.rb
|
249
|
-
- spec/link_shrink/google_spec.rb
|
250
|
-
- spec/link_shrink/isgd_spec.rb
|
251
250
|
- spec/link_shrink/json_parser_spec.rb
|
252
251
|
- spec/link_shrink/link_shrink_spec.rb
|
253
252
|
- spec/link_shrink/options_spec.rb
|
254
|
-
- spec/link_shrink/owly_spec.rb
|
255
253
|
- spec/link_shrink/request_spec.rb
|
256
|
-
- spec/link_shrink/
|
257
|
-
- spec/link_shrink/
|
254
|
+
- spec/link_shrink/shrinkers/google_spec.rb
|
255
|
+
- spec/link_shrink/shrinkers/isgd_spec.rb
|
256
|
+
- spec/link_shrink/shrinkers/owly_spec.rb
|
257
|
+
- spec/link_shrink/shrinkers/shrinker_base_spec.rb
|
258
|
+
- spec/link_shrink/shrinkers/tiny_url_spec.rb
|
258
259
|
- spec/shared_examples.rb
|
259
260
|
- spec/spec_helper.rb
|
260
261
|
- spec/vcr_cassettes/LinkShrink/_shrink_url/creates_a_short_url.yml
|