link_shrink 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data/.rspec +1 -1
  2. data/.travis.yml +2 -0
  3. data/Changelog.md +13 -0
  4. data/README.md +45 -0
  5. data/lib/link_shrink/cli.rb +15 -3
  6. data/lib/link_shrink/config.rb +24 -0
  7. data/lib/link_shrink/request.rb +5 -3
  8. data/lib/link_shrink/shrinkers/base.rb +41 -6
  9. data/lib/link_shrink/shrinkers/google.rb +18 -6
  10. data/lib/link_shrink/shrinkers/tinyurl.rb +31 -0
  11. data/lib/link_shrink/version.rb +1 -1
  12. data/lib/link_shrink.rb +14 -3
  13. data/link_shrink.gemspec +4 -4
  14. data/spec/link_shrink/config_spec.rb +29 -0
  15. data/spec/link_shrink/google_spec.rb +2 -1
  16. data/spec/link_shrink/link_shrink_spec.rb +31 -2
  17. data/spec/link_shrink/request_spec.rb +3 -6
  18. data/spec/link_shrink/shrinker_base_spec.rb +69 -8
  19. data/spec/link_shrink/tiny_url_spec.rb +43 -0
  20. data/spec/spec_helper.rb +24 -1
  21. data/spec/vcr_cassettes/LinkShrink/_shrink_url/creates_a_short_url.yml +134 -0
  22. data/spec/vcr_cassettes/LinkShrink/_shrink_url/when_called_with_additional_options/json_option/returns_JSON_when_set_to_true.yml +91 -0
  23. data/spec/vcr_cassettes/LinkShrink/_shrink_url/when_called_with_additional_options/json_option/returns_default_response_when_set_to_false.yml +91 -0
  24. data/spec/vcr_cassettes/LinkShrink/_shrink_url/when_called_with_additional_options/qr_code_option/returns_QR_code_when_set_to_true.yml +91 -0
  25. data/spec/vcr_cassettes/LinkShrink/_shrink_url/when_called_with_additional_options/qr_code_option/returns_default_response_when_set_to_false.yml +91 -0
  26. data/spec/vcr_cassettes/LinkShrink/_shrink_url/when_called_with_additional_options/returns_default_response_when_called_with_empty_hash.yml +91 -0
  27. data/spec/vcr_cassettes/LinkShrink/_shrink_url/when_called_with_image_size_200x200/returns_QR_code_link_with_custom_size.yml +91 -0
  28. data/spec/vcr_cassettes/LinkShrink/_shrink_url/when_called_with_json_and_qr_code_options/returns_qr_code_in_JSON.yml +91 -0
  29. data/spec/vcr_cassettes/LinkShrink/_shrink_url/when_called_with_json_qr_code_and_image_size_300x300/returns_QR_code_in_JSON_with_custom_size.yml +91 -0
  30. data/spec/vcr_cassettes/LinkShrink/_shrink_url/when_called_with_no_qr_code_and_image_size_200x200/returns_QR_code_link_with_custom_size.yml +95 -0
  31. metadata +44 -18
data/.rspec CHANGED
@@ -1,3 +1,3 @@
1
1
  --color
2
2
  --format documentation
3
- --format Nc
3
+ --fail-fast
data/.travis.yml CHANGED
@@ -2,6 +2,8 @@
2
2
  language: ruby
3
3
  rvm:
4
4
  - 1.9.3
5
+ - 2.0.0
6
+ - jruby-19mode
5
7
  script: bundle exec rake test
6
8
  notifications:
7
9
  email: false
data/Changelog.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # LinkShrink Changelog
2
2
 
3
+ ## 0.0.3
4
+
5
+ Released September 6, 2013 ([0.0.3](https://github.com/jonahoffline/link_shrink/tree/v0.0.3)).
6
+
7
+ * Add Coverall gem to gemspec.
8
+ * Add Config class for configuring Shrinker API to use.
9
+ * Add TinyURL Shrinker.
10
+ * Add documentation to TinyUrl, Google and Base class.
11
+ * Add new options for command-line application.
12
+ * Update README.
13
+ * Refactor Shrinker Base class and the Google class.
14
+ * Added 2.0 and JRuby support through TravisCI.
15
+
3
16
  ## 0.0.2
4
17
 
5
18
  Released July 16, 2013 ([0.0.2](https://github.com/jonahoffline/link_shrink/tree/v0.0.2)).
data/README.md CHANGED
@@ -8,6 +8,27 @@ Installation
8
8
 
9
9
  $ gem install link_shrink
10
10
 
11
+
12
+ Setup
13
+ -------
14
+
15
+ Works best with a Google URL API key. You can sign-up for a free one at
16
+ [Google URL API](https://code.google.com/apis/console)
17
+
18
+ 1. In the Services pane, activate the Google URL Shortener API;
19
+ if the Terms of Service appear, read and accept them.
20
+
21
+ 2. Go to the [API Access pane](https://code.google.com/apis/console#access). The API key is near the bottom of that pane,
22
+ in the section titled "Simple API Access."
23
+
24
+ #### Configuration ####
25
+
26
+ Set your key as an environment variable:
27
+
28
+ $ export GOOGLE_URL_KEY='your_api_key_here'
29
+
30
+ You could also save it in your ~/.bash_profile
31
+
11
32
  ## Usage ##
12
33
 
13
34
  Ruby:
@@ -29,6 +50,25 @@ LinkShrink.shrink_url("http://www.ruby-lang.org", { :qr_code => true, image_size
29
50
 
30
51
  LinkShrink.shrink_url("http://www.ruby-lang.org", { json: true, qr_code: true, image_size: '300x300' })
31
52
  => "{\"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\"}"
53
+
54
+ ```
55
+
56
+ To change the default shrinker (Google API):
57
+
58
+
59
+ ```ruby
60
+ LinkShrink.configure do |c|
61
+ c.api = 'TinyUrl'
62
+ end
63
+
64
+ # or
65
+
66
+ LinkShrink::Config.api = 'TinyUrl'
67
+
68
+ LinkShrink.shrink_url('http://www.google.com')
69
+ => "http://tinyurl.com/1c2"
70
+
71
+
32
72
  ```
33
73
 
34
74
 
@@ -36,9 +76,14 @@ In your terminal:
36
76
 
37
77
  $ linkshrink http://www.rubyrogues.com
38
78
  http://goo.gl/Noh9X
79
+
80
+ $ linkshrink --tinyurl http://www.rubyrogues.com
81
+ http://tinyurl.com/k2butj9
39
82
 
40
83
  ### Command-Line Options ###
41
84
 
85
+ * -t, --tinyurl - use TinyUrl API
86
+ * -g, --google - use Google API (Default)
42
87
  * -j, --json - return JSON response
43
88
  * -q, --qrcode - return QR Code
44
89
  * -h, --help - show help message
@@ -13,8 +13,11 @@ module LinkShrink
13
13
  # Configures the arguments for the command
14
14
  # @param opts [OptionParser]
15
15
  def set_options(opts)
16
+ %w(@json @qr_code @tiny_url)
16
17
  @json = false
17
18
  @qr_code = false
19
+ @tiny_url = false
20
+ @google = false
18
21
  opts.version = LinkShrink::VERSION
19
22
  opts.banner = <<MSG
20
23
  Usage: link_shrink [OPTION] [URL]
@@ -24,12 +27,20 @@ Description:
24
27
  Options:
25
28
  MSG
26
29
  opts.set_program_name 'LinkShrink'
30
+ opts.on_head('-t', '--tinyurl', 'use TinyURL') do
31
+ @tiny_url = :true
32
+ end
33
+
34
+ opts.on_head('-g', '--google', 'use Google (Default)') do
35
+ @google = :true
36
+ end
37
+
27
38
  opts.on_head('-j', '--json', 'return JSON response') do
28
39
  @json = :true
29
40
  end
30
41
 
31
42
  opts.on_head('-q', '--qrcode', 'return QR Code') do
32
- @qr_code = :true
43
+ @qr_code = :true unless @tiny_url
33
44
  end
34
45
 
35
46
  opts.on_tail('-v', '--version', 'display the version of LinkShrink and exit') do
@@ -53,11 +64,12 @@ MSG
53
64
  end
54
65
 
55
66
  def process_url
67
+ LinkShrink.configure { |c| c.api = 'TinyUrl' } if @tiny_url
56
68
  LinkShrink.shrink_url(@args.last, { json: @json, qr_code: @qr_code })
57
69
  end
58
70
 
59
71
  def url_present?
60
- !!(@args.last =~ /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/)
72
+ !!(@args.last =~ /^(http?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/)
61
73
  end
62
74
  end
63
- end
75
+ end
@@ -0,0 +1,24 @@
1
+ module LinkShrink
2
+ module Config
3
+ # @author Jonah Ruiz <jonah@pixelhipsters.com>
4
+ # Configurable module for integrating with other URL APIs
5
+ extend self
6
+
7
+ # Returns and Sets API key to be used
8
+ # @param api_key [String] API key to be used
9
+ attr_accessor :api_key
10
+
11
+ # Sets API to be used
12
+ # @param api [String or Symbol] api to be used
13
+ def api=(api)
14
+ @api = LinkShrink::Shrinkers.const_get(api).new
15
+ end
16
+
17
+ # Returns API used
18
+ # Unless an API is defined, it returns Google by default
19
+ # @return [LinkShrink::Shrinkers::Google] instance
20
+ def api
21
+ @api || LinkShrink::Shrinkers::Google.new
22
+ end
23
+ end
24
+ end
@@ -10,11 +10,12 @@ module LinkShrink
10
10
 
11
11
  module_function
12
12
 
13
- def process_request(url, options, shrinker = LinkShrink::Shrinkers::Google.new)
13
+ def process_request(url, options, shrinker = LinkShrink::Config.api)
14
14
  process_response(request(url, shrinker).body, options, shrinker)
15
15
  end
16
16
 
17
17
  def process_response(response, options, shrinker, json = JSONParser)
18
+ return response if shrinker.content_type.eql?('text/plain')
18
19
  option = Options.new(options)
19
20
  parsed_json = json.parse_json(response)
20
21
  plain = parsed_json['id']
@@ -45,11 +46,12 @@ module LinkShrink
45
46
  # @see LinkShrink::Shrinkers::Base#api_url
46
47
  # @see LinkShrink::Shrinkers::Base#body_parameters
47
48
  def request(url, shrinker)
49
+ shrinker.url = url
48
50
  Typhoeus::Request.new(
49
51
  shrinker.api_url,
50
- method: :post,
52
+ method: shrinker.http_method,
51
53
  body: shrinker.body_parameters(url),
52
- headers: { 'Content-Type' => 'application/json' }
54
+ headers: { 'Content-Type' => shrinker.content_type }
53
55
  ).run
54
56
  end
55
57
  end
@@ -5,11 +5,14 @@ module LinkShrink
5
5
  # @author Jonah Ruiz <jonah@pixelhipsters.com>
6
6
  # an Abstract Base class for implementing other URL APIs
7
7
  class Base
8
+ attr_reader :url
8
9
  # Callback method that dynamically defines a sub_klass method for reference
9
10
  # @return [String] inherited class name
10
11
  def self.inherited(sub_klass)
11
- define_method 'sub_klass' do
12
- "#{sub_klass.name}"[/::(\w+)::(\w+)/, 2]
12
+ sub_klass.class_eval do
13
+ define_method 'sub_klass' do
14
+ @sub_klass = "#{sub_klass.name}"[/::(\w+)::(\w+)/, 2]
15
+ end
13
16
  end
14
17
  end
15
18
 
@@ -24,8 +27,10 @@ module LinkShrink
24
27
  end
25
28
 
26
29
  # Parameters to be used in API request
27
- def body_parameters(url)
28
- fail "#{__method__} not implemented"
30
+ # @param params [Hash] parameters to be used
31
+ # @return [NilClass] nil if parameters are empty
32
+ def body_parameters(params = {})
33
+ nil if params.empty?
29
34
  end
30
35
 
31
36
  # Complete URL with query parameters
@@ -46,8 +51,38 @@ module LinkShrink
46
51
  end
47
52
 
48
53
  # Encodes URL
49
- def sanitize_url(url)
50
- URI.encode(url)
54
+ # @param new_url [String] url to be parsed
55
+ # @return [String] parsed URL
56
+ def sanitize_url(new_url)
57
+ URI.encode(!(new_url =~ /^(http?:\/\/)?/) ? "http://#{new_url}" : new_url)
58
+ end
59
+
60
+ # Returns HTTP method to be used in request
61
+ # @return [Symbol] http method
62
+ def http_method
63
+ :get
64
+ end
65
+
66
+ # Returns Content-Type to be used in Request headers
67
+ # @return [String] content-type
68
+ def content_type
69
+ 'application/json'
70
+ end
71
+
72
+ # Sets URL to be used in request
73
+ # @see #sanitize_url
74
+ # @param new_url [String] url to be parsed
75
+ # @return [String] parsed url
76
+ def url=(new_url)
77
+ @url = sanitize_url(new_url)
78
+ end
79
+
80
+ # Method for generating QR codes or charts
81
+ # @param new_url [String] url to be processed
82
+ # @param image_size [Hash<symbol>] image size target
83
+ # @return [String] chart or qr code url
84
+ def generate_chart_url(new_url, image_size = {})
85
+ fail "#{__method__} not implemented"
51
86
  end
52
87
  end
53
88
  end
@@ -5,27 +5,39 @@ module LinkShrink
5
5
  # @author Jonah Ruiz <jonah@pixelhipsters.com>
6
6
  # Implements Google's URL Shortener API
7
7
  class Google < Base
8
+ # Returns URL base for API
9
+ # @return [String] api base url
8
10
  def base_url
9
11
  'https://www.googleapis.com/urlshortener/v1/url'
10
12
  end
11
13
 
14
+ # Returns URL query parameters
15
+ # @return [String] query parameters to be used in request
12
16
  def api_query_parameter
13
17
  "?key=#{api_key}"
14
18
  end
15
19
 
16
- def body_parameters(url)
17
- { 'longUrl' => sanitize_url(url) }.to_json
20
+ # Returns body parameters used in request header
21
+ # @return [String] body parameters to be used in request header
22
+ def body_parameters(new_url = nil)
23
+ { 'longUrl' => url }.to_json
18
24
  end
19
25
 
20
26
  # Generates QR code URL
21
27
  # @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?
28
+ def generate_chart_url(new_url, image_size = {})
29
+ return "#{new_url}.qr" if image_size.empty?
24
30
 
25
31
  chart_url = 'http://chart.googleapis.com/chart'
26
- params = "?cht=qr&chs=#{image_size}&choe=UTF-8&chld=H&chl=#{url}"
32
+ params = "?cht=qr&chs=#{image_size}&choe=UTF-8&chld=H&chl=#{new_url}"
27
33
  chart_url.concat(params)
28
34
  end
35
+
36
+ # Returns HTTP method to be used in request
37
+ # @return [Symbol] post http method
38
+ def http_method
39
+ :post
40
+ end
29
41
  end
30
42
  end
31
- end
43
+ end
@@ -0,0 +1,31 @@
1
+ module LinkShrink
2
+ module Shrinkers
3
+ # @author Jonah Ruiz <jonah@pixelhipsters.com>
4
+ # Implements Tiny URL Shortener API
5
+ class TinyUrl < Base
6
+ # Returns URL base for API
7
+ # @return [String] api base url
8
+ def base_url
9
+ 'http://tinyurl.com/api-create.php'
10
+ end
11
+
12
+ # URL query parameters
13
+ # @return [String] query parameters to be used in request
14
+ def api_query_parameter
15
+ "?url=#{url}"
16
+ end
17
+
18
+ # Returns Content-Type to be used in Request headers
19
+ # @return [String] text/plain as content-type
20
+ def content_type
21
+ 'text/plain'
22
+ end
23
+
24
+ # Returns full api url
25
+ # @return [String] full api url with query parameters
26
+ def api_url
27
+ base_url.concat api_query_parameter
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module LinkShrink
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
data/lib/link_shrink.rb CHANGED
@@ -4,10 +4,13 @@ require 'link_shrink/request'
4
4
  require 'link_shrink/json_parser'
5
5
  require 'link_shrink/shrinkers/base'
6
6
  require 'link_shrink/shrinkers/google'
7
+ require 'link_shrink/shrinkers/tinyurl'
8
+ require 'link_shrink/config'
7
9
 
8
10
  # @author Jonah Ruiz <jonah@pixelhipsters.com>
9
11
  # Creates a short URL and QR codes
10
12
  module LinkShrink
13
+ extend self
11
14
  include LinkShrink::Request
12
15
  # Returns a short URL or JSON response
13
16
  # example: shrink_url('http://www.wtf.com', { json: true, qr_code: true })
@@ -16,7 +19,7 @@ module LinkShrink
16
19
  # @param url [String] long URL to be shortened
17
20
  # @param options [Hash] format to be returned
18
21
  # @return [String] generated short URL or JSON response
19
- def self.shrink_url(url, options = { json: false, qr_code: false })
22
+ def shrink_url(url, options = { json: false, qr_code: false })
20
23
  process_request(url, options)
21
24
  rescue
22
25
  'Problem generating short URL. Try again.'
@@ -28,9 +31,17 @@ module LinkShrink
28
31
  # @param url [String] long URL to be shortened
29
32
  # @param options [Hash] image_size: '300x300' for a custom size
30
33
  # @return [String] QR code URL using default or custom size
31
- def self.generate_qr_code(url, options = {})
34
+ def generate_qr_code(url, options = {})
32
35
  new_url = process_request(url, {})
33
36
  image_size = options.fetch(:image_size, {})
34
- LinkShrink::Shrinkers::Google.new.generate_chart_url(new_url, image_size)
37
+ Config.api.generate_chart_url(new_url, image_size)
38
+ end
39
+
40
+ # Yield's to Config for options
41
+ #
42
+ # @param <config> [String] api interface to use
43
+ # @param <api_key> [String] api key to use
44
+ def configure
45
+ yield LinkShrink::Config if block_given?
35
46
  end
36
47
  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 '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
27
  spec.add_development_dependency 'simplecov', '~> 0.7.1'
31
28
  spec.add_development_dependency 'simplecov-gem-adapter', '~> 1.0.1'
32
- end
29
+ spec.add_development_dependency 'coveralls', '~> 0.6.7'
30
+ spec.add_development_dependency 'webmock', '~> 1.13.0'
31
+ spec.add_development_dependency 'vcr', '~> 2.5.0'
32
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkShrink::Config do
4
+ include_examples 'shared_examples'
5
+
6
+ let(:link_shrink) { described_class }
7
+
8
+ describe '#api_key' do
9
+ it 'sets api_key' do
10
+ link_shrink.api_key = '12345'
11
+ expect(link_shrink.api_key).to eq('12345')
12
+ end
13
+ end
14
+
15
+ describe '#api' do
16
+ it 'sets api' do
17
+ link_shrink = LinkShrink
18
+ link_shrink.configure { |c| c.api = 'Google' }
19
+ expect(link_shrink::Config.api.class).to eq(LinkShrink::Shrinkers::Google)
20
+ end
21
+
22
+ context 'by default api is set to Google' do
23
+ it 'returns api name' do
24
+ expect(link_shrink.api).to be_a(LinkShrink::Shrinkers::Google)
25
+ end
26
+ end
27
+ end
28
+
29
+ end
@@ -45,7 +45,8 @@ describe LinkShrink::Shrinkers::Google do
45
45
 
46
46
  describe '#body_parameters' do
47
47
  it 'returns body parameters for API request' do
48
- expect(link_shrink.body_parameters("http://www.google.com"))
48
+ link_shrink.stub(:url).and_return('http://www.google.com')
49
+ expect(link_shrink.body_parameters())
49
50
  .to eq("{\"longUrl\":\"http://www.google.com\"}")
50
51
  end
51
52
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe LinkShrink do
4
4
  include_examples 'shared_examples'
5
5
 
6
- describe '.shrink_url' do
6
+ describe '.shrink_url', :vcr => 'google_url' do
7
7
  it 'creates a short url' do
8
8
  expect(link_shrink.shrink_url(url)).to eq(short_url)
9
9
  end
@@ -52,14 +52,30 @@ describe LinkShrink do
52
52
  end
53
53
  end
54
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
+
55
62
  context 'when called with json, qr_code and image_size: 300x300' do
56
- let(:options) { { json: true, qr_code: true, image_size: "300x300" }}
63
+ let(:options) { { json: true, qr_code: true, image_size: "300x300" } }
57
64
 
58
65
  it 'returns QR code in JSON with custom size' do
59
66
  expect(link_shrink.shrink_url(url, options))
60
67
  .to eq(json_qr_code_custom)
61
68
  end
62
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
63
79
  end
64
80
 
65
81
  describe '.generate_qr_code' do
@@ -74,4 +90,17 @@ describe LinkShrink do
74
90
  end
75
91
  end
76
92
  end
93
+
94
+ describe '.configure' do
95
+ context 'when passed a block' do
96
+ it 'yields to Config' do
97
+ expect { |c| link_shrink.configure(&c) }.to yield_control
98
+ end
99
+
100
+ it 'sets API Shrinker' do
101
+ link_shrink.should_receive(:configure).and_yield(LinkShrink::Config)
102
+ link_shrink.configure { |config| api = 'TinyURL' }
103
+ end
104
+ end
105
+ end
77
106
  end
@@ -2,18 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe LinkShrink::Request do
4
4
  include_examples 'shared_examples'
5
- let(:shrinker) { LinkShrink::Shrinkers::Google.new }
5
+ let(:shrinker) { LinkShrink::Config.api }
6
6
  let(:json_default) {{ :json => false }}
7
7
 
8
8
  describe '.process_request' do
9
9
  it 'calls request and returns short link' do
10
- expect(link_shrink.process_request(url, json_default)).to eq(short_url)
10
+ expect(link_shrink.process_request(url, json_default, shrinker))
11
+ .to eq(short_url)
11
12
  end
12
13
 
13
- #it 'calls request and returns short link' do
14
- # expect(link_shrink.process_request(url)).to eq(short_url)
15
- #end
16
-
17
14
  context 'when called with qr_code true, image_size 300x300' do
18
15
  it 'returns QR code with custom size' do
19
16
  options = { json: true, qr_code: true, image_size: '300x300' }
@@ -16,28 +16,62 @@ describe LinkShrink::Shrinkers::Base do
16
16
  end
17
17
 
18
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
19
+ it 'raises nil when called without arguments' do
20
+ expect(link_shrink.body_parameters).to eq(nil)
21
21
  end
22
22
  end
23
23
 
24
24
  describe '#api_url' do
25
+ context 'when api_key is found' do
26
+ it 'returns base_url with api_query_parameters' do
27
+ link_shrink.stub(:sub_klass).and_return('Blahblah')
28
+ link_shrink.stub(:api_key).and_return('12345')
29
+ link_shrink.stub(:api_key?).and_return(true)
30
+
31
+ link_shrink.stub(:base_url).and_return('http://blah.io/api')
32
+ link_shrink.stub(:api_query_parameter).and_return('?key=12345')
33
+ expect(link_shrink.api_url).to eq('http://blah.io/api?key=12345')
34
+ end
35
+ end
36
+
37
+ context 'when api_key is not found' do
38
+ it 'returns base_url' do
39
+ link_shrink.stub(:sub_klass).and_return('Blahblahblah')
40
+ link_shrink.stub(:api_key?).and_return(false)
41
+ link_shrink.stub(:base_url).and_return('http://blah.io/api')
42
+ expect(link_shrink.api_url).to eq('http://blah.io/api')
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '#generate_chart_url' do
25
48
  it 'raises error due to other methods not being overridden' do
26
- expect{ link_shrink.base_url }.to raise_error
49
+ expect{ link_shrink.generate_chart_url('something') }.to raise_error
27
50
  end
28
51
  end
29
- #
52
+
30
53
  describe '#api_key?' do
31
54
  it 'returns false when API is not found' do
32
- link_shrink.stub(:sub_klass).and_return('blahblah')
55
+ link_shrink.stub(:sub_klass).and_return('blahbla')
33
56
  expect(link_shrink.api_key?).to be_false
34
57
  end
35
58
  end
36
59
 
37
60
  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)
61
+ context 'when not found' do
62
+ it 'returns nil' do
63
+ link_shrink.stub(:sub_klass).and_return('Blahh')
64
+ expect(link_shrink.api_key).to eq(nil)
65
+ end
66
+ end
67
+
68
+ context 'when found' do
69
+ it 'returns api_key' do
70
+ link_shrink.stub(:sub_klass).and_return('Blah')
71
+ #link_shrink.stub(:api).and_return('Blah')
72
+ ENV.store('BLAH_URL_KEY', '12345')
73
+ expect(link_shrink.api_key).to eq('12345')
74
+ end
41
75
  end
42
76
  end
43
77
 
@@ -47,4 +81,31 @@ describe LinkShrink::Shrinkers::Base do
47
81
  .to eq("http://www.google.com/%20")
48
82
  end
49
83
  end
84
+
85
+ describe '#http_method' do
86
+ it 'returns :get' do
87
+ expect(link_shrink.http_method).to eq(:get)
88
+ end
89
+ end
90
+
91
+ describe '#content_type' do
92
+ it 'returns application/json by default' do
93
+ expect(link_shrink.content_type).to eq('application/json')
94
+ end
95
+ end
96
+
97
+ describe '#url' do
98
+ context 'when set' do
99
+ it 'returns url' do
100
+ link_shrink.url = 'http://www.google.com'
101
+ expect(link_shrink.url).to eq('http://www.google.com')
102
+ end
103
+ end
104
+
105
+ context 'when called without being set' do
106
+ it 'returns nil' do
107
+ expect(link_shrink.url).to eq(nil)
108
+ end
109
+ end
110
+ end
50
111
  end