link_shrink 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
@@ -0,0 +1 @@
1
+ require 'autotest/fsevent'
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
2
  --format documentation
3
+ --format Nc
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-1.9.3-p392
1
+ ruby-1.9.3-p448
data/.travis.yml CHANGED
@@ -1,15 +1,15 @@
1
+ ---
1
2
  language: ruby
2
3
  rvm:
3
- - 1.9.3
4
- script: 'bundle exec rake test'
5
- before_install:
6
- - gem install bundler
7
- rvm:
8
- - 1.9.3
4
+ - 1.9.3
5
+ script: bundle exec rake test
9
6
  notifications:
10
7
  email: false
11
8
  irc:
12
9
  on_success: change
13
10
  on_failure: always
14
11
  channels:
15
- - "irc.freenode.org#rubyonadhd"
12
+ - irc.freenode.org#rubyonadhd
13
+ env:
14
+ global:
15
+ secure: bPqqN6twAJeRIpUEGozFIV3+dW2TSRxYQRrQhpzJ6Bx2ZXmk8UWl1O57Scf9/ZrCjo23MSvWKY1Rt1eyncaUVfzr4PjR6GjUwwdE7SPHQFKvGeNLIlJElzcz1ywcX6RU8+6JPYnx3NxR2O5VM8rf7wLwma00nuaDunM5NgKMMdQ=
data/Changelog.md ADDED
@@ -0,0 +1,27 @@
1
+ # LinkShrink Changelog
2
+
3
+ ## 0.0.2
4
+
5
+ Released July 16, 2013 ([0.0.2](https://github.com/jonahoffline/link_shrink/tree/v0.0.2)).
6
+
7
+ * Add :qr_code, and :image_size options to the shrink_url.
8
+ * Add new generate_qr_code method to LinkShrink.
9
+ * Add method for using API key.
10
+ * Refactor LinkShrink module.
11
+ * Move code from LinkShrink into new Request module.
12
+ * Move json-related code into new JSONParser module.
13
+ * Add new Options class.
14
+ * Add Shrinker::Base abstract class for defining other URL APIs.
15
+ * Move Google URL API related code into its own class implemeting new Base class.
16
+ * Add --qrcode option into CLI for command-line application.
17
+ * Update README.
18
+ * Add Changelog.
19
+ * Update gemspec development-dependencies (RSpec)
20
+ * Add new gemspec development-dependencies (autotest-test, rspec-nc, simplecov)
21
+ * Add BadgeFury, Gemnasium, CodeClimate and Travis-CI badges to README.
22
+
23
+ ## 0.0.1
24
+
25
+ Released June 24, 2013 ([0.0.1](https://github.com/jonahoffline/link_shrink/tree/v0.0.1)).
26
+
27
+ * Initial Release.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- LinkShrink [![Build Status](https://travis-ci.org/jonahoffline/link_shrink.png?branch=master)](https://travis-ci.org/jonahoffline/link_shrink) [![Gem Version](https://badge.fury.io/rb/link_shrink.png)](http://badge.fury.io/rb/link_shrink) [![Dependency Status](https://gemnasium.com/jonahoffline/link_shrink.png)](https://gemnasium.com/jonahoffline/link_shrink)
1
+ LinkShrink [![Build Status](https://travis-ci.org/jonahoffline/link_shrink.png?branch=master)](https://travis-ci.org/jonahoffline/link_shrink) [![Gem Version](https://badge.fury.io/rb/link_shrink.png)](http://badge.fury.io/rb/link_shrink) [![Dependency Status](https://gemnasium.com/jonahoffline/link_shrink.png)](https://gemnasium.com/jonahoffline/link_shrink) [![Code Climate](https://codeclimate.com/github/jonahoffline/link_shrink.png)](https://codeclimate.com/github/jonahoffline/link_shrink)
2
2
  =================
3
3
 
4
4
  A Ruby Gem and Command-Line Application for shrinking those long and nasty links into a shorter URL
@@ -20,6 +20,15 @@ LinkShrink.shrink_url("http://www.ruby-lang.org")
20
20
 
21
21
  LinkShrink.shrink_url("http://www.ruby-lang.org", { :json => true })
22
22
  => "{\"kind\":\"urlshortener#url\",\"id\":\"http://goo.gl/MprR\",\"longUrl\":\"http://www.ruby-lang.org/\"}"
23
+
24
+ LinkShrink.shrink_url("http://www.ruby-lang.org", { :qr_code => true })
25
+ => "http://goo.gl/QuXj.qr"
26
+
27
+ LinkShrink.shrink_url("http://www.ruby-lang.org", { :qr_code => true, image_size: '200x200' })
28
+ => "http://chart.googleapis.com/chart?cht=qr&chs=200x200&choe=UTF-8&chld=H&chl=http://goo.gl/MprR"
29
+
30
+ LinkShrink.shrink_url("http://www.ruby-lang.org", { json: true, qr_code: true, image_size: '300x300' })
31
+ => "{\"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\"}"
23
32
  ```
24
33
 
25
34
 
@@ -31,6 +40,7 @@ In your terminal:
31
40
  ### Command-Line Options ###
32
41
 
33
42
  * -j, --json - return JSON response
43
+ * -q, --qrcode - return QR Code
34
44
  * -h, --help - show help message
35
45
 
36
46
  ## Author
data/Rakefile CHANGED
@@ -4,4 +4,11 @@ require 'rspec/core/rake_task'
4
4
  RSpec::Core::RakeTask.new
5
5
 
6
6
  task :default => :spec
7
- task :test => :spec
7
+ task :test => :spec
8
+
9
+ desc 'Run All specs and generate simplecov report'
10
+ task :cov do |t|
11
+ ENV['COVERAGE'] = 'true'
12
+ Rake::Task['spec'].execute
13
+ `open coverage/index.html`
14
+ end
@@ -14,6 +14,7 @@ module LinkShrink
14
14
  # @param opts [OptionParser]
15
15
  def set_options(opts)
16
16
  @json = false
17
+ @qr_code = false
17
18
  opts.version = LinkShrink::VERSION
18
19
  opts.banner = <<MSG
19
20
  Usage: link_shrink [OPTION] [URL]
@@ -27,6 +28,10 @@ MSG
27
28
  @json = :true
28
29
  end
29
30
 
31
+ opts.on_head('-q', '--qrcode', 'return QR Code') do
32
+ @qr_code = :true
33
+ end
34
+
30
35
  opts.on_tail('-v', '--version', 'display the version of LinkShrink and exit') do
31
36
  puts opts.version
32
37
  exit
@@ -48,7 +53,7 @@ MSG
48
53
  end
49
54
 
50
55
  def process_url
51
- LinkShrink.shrink_url(@args.last, { json: @json })
56
+ LinkShrink.shrink_url(@args.last, { json: @json, qr_code: @qr_code })
52
57
  end
53
58
 
54
59
  def url_present?
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+
3
+ module LinkShrink
4
+ # @author Jonah Ruiz <jonah@pixelhipsters.com>
5
+ # Wraps JSON class
6
+ module JSONParser
7
+ def self.included(base)
8
+ base.extend self
9
+ end
10
+
11
+ def self.parse_json(data)
12
+ JSON.load(data)
13
+ end
14
+
15
+ def self.cleanup_json(data)
16
+ data.gsub(/\s+/, '')
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module LinkShrink
2
+ # @author Jonah Ruiz <jonah@pixelhipsters.com>
3
+ # Inherits from Hash to add predicate methods
4
+ class Options < Hash
5
+ def initialize(*args)
6
+ super(*args)
7
+ self.merge!(self.default)
8
+ end
9
+
10
+ # Dynamically defines three predicate methods
11
+ # @return [Trueclass] return true or false if no key found
12
+ [:qr_code, :json, :image_size].map do |method|
13
+ define_method "#{method}?" do
14
+ self.fetch(method, false)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,56 @@
1
+ require 'typhoeus'
2
+
3
+ module LinkShrink
4
+ # @author Jonah Ruiz <jonah@pixelhipsters.com>
5
+ # Handles request and calls parser
6
+ module Request
7
+ def self.included(base)
8
+ base.extend self
9
+ end
10
+
11
+ module_function
12
+
13
+ def process_request(url, options, shrinker = LinkShrink::Shrinkers::Google.new)
14
+ process_response(request(url, shrinker).body, options, shrinker)
15
+ end
16
+
17
+ def process_response(response, options, shrinker, json = JSONParser)
18
+ option = Options.new(options)
19
+ parsed_json = json.parse_json(response)
20
+ plain = parsed_json['id']
21
+
22
+ if option.json? && option.qr_code?
23
+ if option.image_size?
24
+ return parsed_json.merge(qr_code: shrinker.generate_chart_url(plain, options.fetch(:image_size))).to_json
25
+ end
26
+ return parsed_json.merge(qr_code: shrinker.generate_chart_url(plain)).to_json
27
+ end
28
+
29
+ case
30
+ when option.json?
31
+ json.cleanup_json(response)
32
+ when option.qr_code?
33
+ if option.image_size?
34
+ return shrinker.generate_chart_url(plain, options.fetch(:image_size))
35
+ end
36
+ shrinker.generate_chart_url(plain)
37
+ when option.image_size?
38
+ shrinker.generate_chart_url(plain, options.fetch(:image_size))
39
+ else
40
+ plain
41
+ end
42
+ end
43
+
44
+ # Calls URL API
45
+ # @see LinkShrink::Shrinkers::Base#api_url
46
+ # @see LinkShrink::Shrinkers::Base#body_parameters
47
+ def request(url, shrinker)
48
+ Typhoeus::Request.new(
49
+ shrinker.api_url,
50
+ method: :post,
51
+ body: shrinker.body_parameters(url),
52
+ headers: { 'Content-Type' => 'application/json' }
53
+ ).run
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,54 @@
1
+ require 'uri'
2
+
3
+ module LinkShrink
4
+ module Shrinkers
5
+ # @author Jonah Ruiz <jonah@pixelhipsters.com>
6
+ # an Abstract Base class for implementing other URL APIs
7
+ class Base
8
+ # Callback method that dynamically defines a sub_klass method for reference
9
+ # @return [String] inherited class name
10
+ def self.inherited(sub_klass)
11
+ define_method 'sub_klass' do
12
+ "#{sub_klass.name}"[/::(\w+)::(\w+)/, 2]
13
+ end
14
+ end
15
+
16
+ # URL base for API
17
+ def base_url
18
+ fail "#{__method__} not implemented"
19
+ end
20
+
21
+ # URL query parameters
22
+ def api_query_parameter
23
+ fail "#{__method__} not implemented"
24
+ end
25
+
26
+ # Parameters to be used in API request
27
+ def body_parameters(url)
28
+ fail "#{__method__} not implemented"
29
+ end
30
+
31
+ # Complete URL with query parameters
32
+ def api_url
33
+ api_key? ? base_url.concat(api_query_parameter) : base_url
34
+ end
35
+
36
+ # Predicate method for checking if the API key exists
37
+ # @return [TrueClass, FalseClass]
38
+ def api_key?
39
+ ENV.has_key?("#{sub_klass.upcase}_URL_KEY")
40
+ end
41
+
42
+ # Returns API Key
43
+ # @return [String] API key or nil
44
+ def api_key
45
+ api_key? ? ENV["#{sub_klass.upcase}_URL_KEY"] : nil
46
+ end
47
+
48
+ # Encodes URL
49
+ def sanitize_url(url)
50
+ URI.encode(url)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,31 @@
1
+ require 'json'
2
+
3
+ module LinkShrink
4
+ module Shrinkers
5
+ # @author Jonah Ruiz <jonah@pixelhipsters.com>
6
+ # Implements Google's URL Shortener API
7
+ class Google < Base
8
+ def base_url
9
+ 'https://www.googleapis.com/urlshortener/v1/url'
10
+ end
11
+
12
+ def api_query_parameter
13
+ "?key=#{api_key}"
14
+ end
15
+
16
+ def body_parameters(url)
17
+ { 'longUrl' => sanitize_url(url) }.to_json
18
+ end
19
+
20
+ # Generates QR code URL
21
+ # @return [String] QR code url with '150x150' or custom image size
22
+ def generate_chart_url(url, image_size = {})
23
+ return "#{url}.qr" if image_size.empty?
24
+
25
+ chart_url = 'http://chart.googleapis.com/chart'
26
+ params = "?cht=qr&chs=#{image_size}&choe=UTF-8&chld=H&chl=#{url}"
27
+ chart_url.concat(params)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module LinkShrink
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/link_shrink.rb CHANGED
@@ -1,40 +1,36 @@
1
1
  require 'link_shrink/version'
2
- require 'typhoeus'
3
- require 'json'
4
- require 'uri'
2
+ require 'link_shrink/options'
3
+ require 'link_shrink/request'
4
+ require 'link_shrink/json_parser'
5
+ require 'link_shrink/shrinkers/base'
6
+ require 'link_shrink/shrinkers/google'
5
7
 
6
- # @author Jonah Ruiz <jonah@pixelhipsters.com>
7
- # Creates a short URL using Google's URL Shortener API
8
+ # @author Jonah Ruiz <jonah@pixelhipsters.com>
9
+ # Creates a short URL and QR codes
8
10
  module LinkShrink
9
-
11
+ include LinkShrink::Request
10
12
  # Returns a short URL or JSON response
13
+ # example: shrink_url('http://www.wtf.com', { json: true, qr_code: true })
14
+ # example: shrink_url('http://www.wtf.com', { qr_code: true })
11
15
  #
12
16
  # @param url [String] long URL to be shortened
13
17
  # @param options [Hash] format to be returned
14
18
  # @return [String] generated short URL or JSON response
15
- def self.shrink_url(url, options = {:json => false})
16
- res = request(URI.encode(url))
17
- options.fetch(:json, nil) ? cleanup_json(res.body) : parse_json(res.body)['id']
19
+ def self.shrink_url(url, options = { json: false, qr_code: false })
20
+ process_request(url, options)
18
21
  rescue
19
22
  'Problem generating short URL. Try again.'
20
23
  end
21
24
 
22
- private
23
-
24
- def self.request(url)
25
- Typhoeus::Request.new(
26
- 'https://www.googleapis.com/urlshortener/v1/url',
27
- method: :post,
28
- body: { 'longUrl' => url }.to_json,
29
- headers: { 'Content-Type' => 'application/json' }
30
- ).run
31
- end
32
-
33
- def self.parse_json(data)
34
- JSON.parse(data)
35
- end
36
-
37
- def self.cleanup_json(data)
38
- data.gsub(/\s+/, '')
25
+ # Returns a QR code URL
26
+ # example: generate_qr_code('http://www.wtf.com', { image_size: '300x300' })
27
+ #
28
+ # @param url [String] long URL to be shortened
29
+ # @param options [Hash] image_size: '300x300' for a custom size
30
+ # @return [String] QR code URL using default or custom size
31
+ def self.generate_qr_code(url, options = {})
32
+ new_url = process_request(url, {})
33
+ image_size = options.fetch(:image_size, {})
34
+ LinkShrink::Shrinkers::Google.new.generate_chart_url(new_url, image_size)
39
35
  end
40
- end
36
+ end
data/link_shrink.gemspec CHANGED
@@ -22,6 +22,11 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency 'typhoeus', '~> 0.6.3'
23
23
 
24
24
  spec.add_development_dependency 'bundler', '~> 1.3'
25
- spec.add_development_dependency 'rspec', '~> 2.13.0'
25
+ spec.add_development_dependency 'rspec', '~> 2.14.1'
26
26
  spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'autotest-standalone', '~> 4.5.11'
28
+ spec.add_development_dependency 'autotest-fsevent', '~> 0.2.8'
29
+ spec.add_development_dependency 'rspec-nc', '~> 0.0.6'
30
+ spec.add_development_dependency 'simplecov', '~> 0.7.1'
31
+ spec.add_development_dependency 'simplecov-gem-adapter', '~> 1.0.1'
27
32
  end
@@ -0,0 +1 @@
1
+ {"kind":"urlshortener#url","id":"http://goo.gl/fbsS","longUrl":"http://www.google.com/","qr_code":"http://goo.gl/fbsS.qr"}
@@ -0,0 +1 @@
1
+ {"kind":"urlshortener#url","id":"http://goo.gl/fbsS","longUrl":"http://www.google.com/","qr_code":"http://chart.googleapis.com/chart?cht=qr&chs=300x300&choe=UTF-8&chld=H&chl=http://goo.gl/fbsS"}
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkShrink::Shrinkers::Google do
4
+ include_examples 'shared_examples'
5
+
6
+ let(:link_shrink) { described_class.new }
7
+ let(:google_url) { 'https://www.googleapis.com/urlshortener/v1/url' }
8
+ let(:key) { ENV['GOOGLE_URL_KEY'] }
9
+
10
+ describe '#sub_klass' do
11
+ it 'returns the inherited subclass name' do
12
+ expect(link_shrink.sub_klass).to eq('Google')
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(google_url)
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 '#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
+ describe '#body_parameters' do
47
+ it 'returns body parameters for API request' do
48
+ expect(link_shrink.body_parameters("http://www.google.com"))
49
+ .to eq("{\"longUrl\":\"http://www.google.com\"}")
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkShrink::JSONParser do
4
+ include_examples 'shared_examples'
5
+
6
+ describe '.parse_json' do
7
+ it 'delegates to the JSON parser' do
8
+ expect(link_shrink.parse_json(json_response))
9
+ .to eq(parsed_json)
10
+ end
11
+ end
12
+
13
+ describe '.cleanup_json' do
14
+ let(:json) { "{\n \"kind\": \"urlshortener#url\",\n \"id\": \"http://goo.gl/fbsS\",\n \"longUrl\": \"http://www.google.com/\"\n}\n" }
15
+
16
+ it 'cleans JSON response' do
17
+ expect(link_shrink.cleanup_json(json))
18
+ .to eq('{"kind":"urlshortener#url","id":"http://goo.gl/fbsS","longUrl":"http://www.google.com/"}')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkShrink do
4
+ include_examples 'shared_examples'
5
+
6
+ describe '.shrink_url' do
7
+ it 'creates a short url' do
8
+ expect(link_shrink.shrink_url(url)).to eq(short_url)
9
+ 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 json, qr_code and image_size: 300x300' do
56
+ let(:options) { { json: true, qr_code: true, image_size: "300x300" }}
57
+
58
+ it 'returns QR code in JSON with custom size' do
59
+ expect(link_shrink.shrink_url(url, options))
60
+ .to eq(json_qr_code_custom)
61
+ end
62
+ end
63
+ end
64
+
65
+ describe '.generate_qr_code' do
66
+ it 'creates a QR code' do
67
+ expect(link_shrink.generate_qr_code(url)).to eq(qrcode_url)
68
+ end
69
+
70
+ context 'when called with image_size: 200x200' do
71
+ it 'returns QR code with custom size' do
72
+ expect(link_shrink.generate_qr_code(url, { image_size: "200x200" }))
73
+ .to eq(chart_url)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkShrink::Options do
4
+ let(:options_class) { described_class }
5
+ let(:empty_hash) { described_class.new({}) }
6
+
7
+ describe '#initialize' do
8
+ it 'inherits from Hash' do
9
+ expect(options_class.superclass).to be(Hash)
10
+ end
11
+ end
12
+
13
+ describe '#json?' do
14
+ context 'when :json is set to true in the options hash' do
15
+ it 'returns true' do
16
+ options = options_class.new(json: true)
17
+ expect(options.json?).to be_true
18
+ end
19
+ end
20
+
21
+ context 'when :json is set to false in the options hash' do
22
+ it 'returns false' do
23
+ options = options_class.new(json: false)
24
+ expect(options.json?).to be_false
25
+ end
26
+ end
27
+
28
+ context 'when called with empty hash' do
29
+ it 'returns false' do
30
+ expect(empty_hash.json?).to be_false
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '#qr_code?' do
36
+ context 'when :qr_code is set to true in the options hash' do
37
+ it 'returns true' do
38
+ options = options_class.new(qr_code: true)
39
+ expect(options.qr_code?).to be_true
40
+ end
41
+ end
42
+
43
+ context 'when :qr_code is set to false in the options hash' do
44
+ it 'returns false' do
45
+ options = options_class.new(qr_code: false)
46
+ expect(options.qr_code?).to be_false
47
+ end
48
+ end
49
+ end
50
+
51
+ describe '#qr_code?' do
52
+ context 'when :qr_code is set to true in the options hash' do
53
+ it 'returns true' do
54
+ options = options_class.new(qr_code: true)
55
+ expect(options.qr_code?).to be_true
56
+ end
57
+ end
58
+
59
+ context 'when :qr_code is set to false in the options hash' do
60
+ it 'returns false' do
61
+ options = options_class.new(qr_code: false)
62
+ expect(options.qr_code?).to be_false
63
+ end
64
+ end
65
+ end
66
+
67
+ describe '#image_size?' do
68
+ context 'when :image_size has a custom size' do
69
+ it 'returns true' do
70
+ options = options_class.new(qr_code: '300x300')
71
+ expect(options.qr_code?).to be_true
72
+ end
73
+ end
74
+
75
+ context 'when :image_size is not defined in the options hash' do
76
+ it 'returns false' do
77
+ expect(empty_hash.image_size?).to be_false
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkShrink::Request do
4
+ include_examples 'shared_examples'
5
+ let(:shrinker) { LinkShrink::Shrinkers::Google.new }
6
+ let(:json_default) {{ :json => false }}
7
+
8
+ describe '.process_request' do
9
+ it 'calls request and returns short link' do
10
+ expect(link_shrink.process_request(url, json_default)).to eq(short_url)
11
+ end
12
+
13
+ #it 'calls request and returns short link' do
14
+ # expect(link_shrink.process_request(url)).to eq(short_url)
15
+ #end
16
+
17
+ context 'when called with qr_code true, image_size 300x300' do
18
+ it 'returns QR code with custom size' do
19
+ options = { json: true, qr_code: true, image_size: '300x300' }
20
+ expect(link_shrink.process_request(url, options))
21
+ .to eq(json_qr_code_custom)
22
+ end
23
+ end
24
+ end
25
+
26
+ describe '.process_response' do
27
+ context 'when called with json true' do
28
+ it 'returns json' do
29
+ response = link_shrink.request(url, shrinker).body
30
+ expect(link_shrink.process_response(response, {json: true}, shrinker))
31
+ .to eq(json_response)
32
+ end
33
+ end
34
+
35
+ context 'when called with json false or no options' do
36
+ it 'returns link' do
37
+ response = link_shrink.request(url, shrinker).body
38
+ expect(link_shrink.process_response(response, json_default, shrinker))
39
+ .to eq(short_url)
40
+ end
41
+ end
42
+
43
+ context 'when called with qr_code true' do
44
+ it 'returns QR code' do
45
+ response = link_shrink.request(url, shrinker).body
46
+ expect(link_shrink.process_response(response, { qr_code: true }, shrinker))
47
+ .to eq(qrcode_url)
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '.request' do
53
+ it 'is a Typhoeus::Request instance' do
54
+ expect(link_shrink.request(url, shrinker))
55
+ .to be_kind_of(Typhoeus::Response)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkShrink::Shrinkers::Base do
4
+ let(:link_shrink) { described_class.new }
5
+
6
+ describe '#base_url' do
7
+ it 'raise error when not overriden' do
8
+ expect{ link_shrink.base_url }.to raise_error 'base_url not implemented'
9
+ end
10
+ end
11
+
12
+ describe '#api_query_parameter' do
13
+ it 'raises error due to other methods not being overridden' do
14
+ expect{ link_shrink.api_query_parameter }.to raise_error
15
+ end
16
+ end
17
+
18
+ describe '#body_parameters' do
19
+ it 'raises error due to other methods not being overridden' do
20
+ expect{ link_shrink.body_parameters }.to raise_error
21
+ end
22
+ end
23
+
24
+ describe '#api_url' do
25
+ it 'raises error due to other methods not being overridden' do
26
+ expect{ link_shrink.base_url }.to raise_error
27
+ end
28
+ end
29
+ #
30
+ describe '#api_key?' do
31
+ it 'returns false when API is not found' do
32
+ link_shrink.stub(:sub_klass).and_return('blahblah')
33
+ expect(link_shrink.api_key?).to be_false
34
+ end
35
+ end
36
+
37
+ describe '#api_key' do
38
+ it 'returns the api key' do
39
+ link_shrink.stub(:sub_klass).and_return('blahblah')
40
+ expect(link_shrink.api_key).to eq(nil)
41
+ end
42
+ end
43
+
44
+ describe '#sanitize_url' do
45
+ it 'cleans URL' do
46
+ expect(link_shrink.sanitize_url("http://www.google.com/ "))
47
+ .to eq("http://www.google.com/%20")
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,22 @@
1
+ require 'ostruct'
2
+
3
+ # @author Jonah Ruiz <jonah@pixelhipsters.com>
4
+ # Shared examples used in specs
5
+ shared_context 'shared_examples' do
6
+ # Helper method for loading fixtures
7
+ def fixture(file)
8
+ file_path = File.join(File.dirname(__FILE__), 'fixtures', "#{file}")
9
+ File.open(file_path).read
10
+ end
11
+
12
+ let(:link_shrink) { described_class }
13
+ let(:url) { 'http://www.google.com' }
14
+ let(:short_url) { "http://goo.gl/fbsS" }
15
+ let(:qrcode_url) { 'http://goo.gl/fbsS.qr' }
16
+
17
+ 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
+ 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
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,12 @@
1
+ require 'simplecov'
2
+ require 'simplecov-gem-adapter'
3
+
4
+ SimpleCov.start 'gem'
5
+ require 'shared_examples'
1
6
  require 'link_shrink'
2
7
 
3
8
  RSpec.configure do |config|
4
- config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ #config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
5
11
  config.order = 'random'
6
12
  end
7
-
8
- def fixture(file)
9
- file_path = File.join(File.dirname(__FILE__), 'fixtures', "#{file}")
10
- File.open(file_path).read
11
- 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.1
4
+ version: 0.0.2
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: 2013-06-25 00:00:00.000000000 Z
12
+ date: 2013-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: typhoeus
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: 2.13.0
53
+ version: 2.14.1
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 2.13.0
61
+ version: 2.14.1
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rake
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -75,6 +75,86 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: autotest-standalone
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 4.5.11
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 4.5.11
94
+ - !ruby/object:Gem::Dependency
95
+ name: autotest-fsevent
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 0.2.8
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.2.8
110
+ - !ruby/object:Gem::Dependency
111
+ name: rspec-nc
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 0.0.6
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 0.0.6
126
+ - !ruby/object:Gem::Dependency
127
+ name: simplecov
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 0.7.1
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 0.7.1
142
+ - !ruby/object:Gem::Dependency
143
+ name: simplecov-gem-adapter
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: 1.0.1
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 1.0.1
78
158
  description: Shrink those long and nasty links into a shorter URL
79
159
  email:
80
160
  - jonah@pixelhipsters.com
@@ -83,11 +163,13 @@ executables:
83
163
  extensions: []
84
164
  extra_rdoc_files: []
85
165
  files:
166
+ - .autotest
86
167
  - .gitignore
87
168
  - .rspec
88
169
  - .ruby-gemset
89
170
  - .ruby-version
90
171
  - .travis.yml
172
+ - Changelog.md
91
173
  - Gemfile
92
174
  - LICENSE
93
175
  - README.md
@@ -95,10 +177,23 @@ files:
95
177
  - bin/linkshrink
96
178
  - lib/link_shrink.rb
97
179
  - lib/link_shrink/cli.rb
180
+ - lib/link_shrink/json_parser.rb
181
+ - lib/link_shrink/options.rb
182
+ - lib/link_shrink/request.rb
183
+ - lib/link_shrink/shrinkers/base.rb
184
+ - lib/link_shrink/shrinkers/google.rb
98
185
  - lib/link_shrink/version.rb
99
186
  - link_shrink.gemspec
100
187
  - spec/fixtures/response.json
101
- - spec/link_shrink_spec.rb
188
+ - spec/fixtures/response_qr_code.json
189
+ - spec/fixtures/response_qr_code_custom.json
190
+ - spec/link_shrink/google_spec.rb
191
+ - spec/link_shrink/json_parser_spec.rb
192
+ - spec/link_shrink/link_shrink_spec.rb
193
+ - spec/link_shrink/options_spec.rb
194
+ - spec/link_shrink/request_spec.rb
195
+ - spec/link_shrink/shrinker_base_spec.rb
196
+ - spec/shared_examples.rb
102
197
  - spec/spec_helper.rb
103
198
  homepage: https://github.com/jonahoffline/link_shrink
104
199
  licenses:
@@ -121,7 +216,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
216
  version: '0'
122
217
  segments:
123
218
  - 0
124
- hash: 1575228594279785560
219
+ hash: 1699152346401212239
125
220
  requirements: []
126
221
  rubyforge_project:
127
222
  rubygems_version: 1.8.25
@@ -130,5 +225,13 @@ specification_version: 3
130
225
  summary: Shrink those long and nasty links using Google's URL Shortner API
131
226
  test_files:
132
227
  - spec/fixtures/response.json
133
- - spec/link_shrink_spec.rb
228
+ - spec/fixtures/response_qr_code.json
229
+ - spec/fixtures/response_qr_code_custom.json
230
+ - spec/link_shrink/google_spec.rb
231
+ - spec/link_shrink/json_parser_spec.rb
232
+ - spec/link_shrink/link_shrink_spec.rb
233
+ - spec/link_shrink/options_spec.rb
234
+ - spec/link_shrink/request_spec.rb
235
+ - spec/link_shrink/shrinker_base_spec.rb
236
+ - spec/shared_examples.rb
134
237
  - spec/spec_helper.rb
@@ -1,56 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe LinkShrink do
4
- let(:link_shrink) { LinkShrink }
5
- let(:url) { 'http://www.google.com' }
6
- let(:short_url) { 'http://goo.gl/fbsS' }
7
- let(:json) { fixture('response.json') }
8
-
9
-
10
- describe '.shrink_url' do
11
- it 'creates a short url using the API' do
12
- expect(link_shrink.shrink_url(url)).to eq(short_url)
13
- end
14
-
15
- context 'when called with additional options' do
16
- it 'returns response in JSON format when set to true' do
17
- expect(link_shrink.shrink_url(url, { json: true })).to eq(json)
18
- end
19
-
20
- it 'returns default response when set to false' do
21
- expect(link_shrink.shrink_url(url, { :json => false })).to eq(short_url)
22
- end
23
- end
24
-
25
- end
26
-
27
- describe '.request' do
28
- it 'is a Typhoeus::Request instance' do
29
- expect(link_shrink.request(url))
30
- .to be_kind_of(Typhoeus::Response)
31
- end
32
-
33
- it 'sends request to the Google URL Shortner API' do
34
- response = link_shrink.request(url)
35
-
36
- expect(response.return_code).to eq(:ok)
37
- expect(response.response_code).to eq(200)
38
- end
39
- end
40
-
41
- describe '.parse_json' do
42
- it 'delegates to the JSON parser' do
43
- expect(link_shrink.parse_json(json))
44
- .to eq( {"kind" => "urlshortener#url", "id" => "http://goo.gl/fbsS", "longUrl" => "http://www.google.com/" } )
45
- end
46
- end
47
-
48
- describe '.cleanup_json' do
49
- let(:json) {"{\n \"kind\": \"urlshortener#url\",\n \"id\": \"http://goo.gl/fbsS\",\n \"longUrl\": \"http://www.google.com/\"\n}\n"}
50
-
51
- it 'cleans JSON response' do
52
- expect(link_shrink.cleanup_json(json))
53
- .to eq('{"kind":"urlshortener#url","id":"http://goo.gl/fbsS","longUrl":"http://www.google.com/"}')
54
- end
55
- end
56
- end