link_shrink 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ link_shrink
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p392
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ script: 'bundle exec rake test'
5
+ before_install:
6
+ - gem install bundler
7
+ rvm:
8
+ - 1.9.3
9
+ notifications:
10
+ email: false
11
+ irc:
12
+ on_success: change
13
+ on_failure: always
14
+ channels:
15
+ - "irc.freenode.org#rubyonadhd"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in link_shrink.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jonah Ruiz
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
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)
2
+ =================
3
+
4
+ A Ruby Gem and Command-Line Application for shrinking those long and nasty links into a shorter URL
5
+
6
+ Installation
7
+ ---------------------
8
+
9
+ $ gem install link_shrink
10
+
11
+ ## Usage ##
12
+
13
+ Ruby:
14
+
15
+ ```ruby
16
+ require 'link_shrink'
17
+
18
+ LinkShrink.shrink_url("http://www.ruby-lang.org")
19
+ => "http://goo.gl/QuXj"
20
+
21
+ LinkShrink.shrink_url("http://www.ruby-lang.org", { :json => true })
22
+ => "{\"kind\":\"urlshortener#url\",\"id\":\"http://goo.gl/MprR\",\"longUrl\":\"http://www.ruby-lang.org/\"}"
23
+ ```
24
+
25
+
26
+ In your terminal:
27
+
28
+ $ linkshrink http://www.rubyrogues.com
29
+ http://goo.gl/Noh9X
30
+
31
+ ### Command-Line Options ###
32
+
33
+ * -j, --json - return JSON response
34
+ * -h, --help - show help message
35
+
36
+ ## Author
37
+ * [Jonah Ruiz](http://www.pixelhipsters.com)
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/bin/linkshrink ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ Signal.trap("INT") { exit 1 }
4
+
5
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib/'
6
+ require 'link_shrink'
7
+ require 'link_shrink/cli'
8
+
9
+ puts LinkShrink::CLI.new(ARGV).parse
@@ -0,0 +1,58 @@
1
+ require 'optparse'
2
+
3
+ module LinkShrink
4
+ # @author Jonah Ruiz <jonah@pixelhipsters.com>
5
+ # A Simple class for the executable version of the gem
6
+ class CLI
7
+
8
+ # @param args [Array<String>] The command-line arguments
9
+ def initialize(args)
10
+ @args = args
11
+ end
12
+
13
+ # Configures the arguments for the command
14
+ # @param opts [OptionParser]
15
+ def set_options(opts)
16
+ @json = false
17
+ opts.version = LinkShrink::VERSION
18
+ opts.banner = <<MSG
19
+ Usage: link_shrink [OPTION] [URL]
20
+ Description:
21
+ LinkShrink, Turn long and nasty links into short urls.
22
+
23
+ Options:
24
+ MSG
25
+ opts.set_program_name 'LinkShrink'
26
+ opts.on_head('-j', '--json', 'return JSON response') do
27
+ @json = :true
28
+ end
29
+
30
+ opts.on_tail('-v', '--version', 'display the version of LinkShrink and exit') do
31
+ puts opts.version
32
+ exit
33
+ end
34
+
35
+ opts.on_tail('-h', '--help', 'print this help') do
36
+ puts opts.help
37
+ exit
38
+ end
39
+ end
40
+
41
+ # Parses the command-line arguments and runs the executable
42
+ # @return [String] The short url or argument passed
43
+ def parse
44
+ opts = OptionParser.new(&method(:set_options))
45
+ opts.parse!(@args)
46
+ return process_url if url_present?
47
+ opts.help
48
+ end
49
+
50
+ def process_url
51
+ LinkShrink.shrink_url(@args.last, { json: @json })
52
+ end
53
+
54
+ def url_present?
55
+ !!(@args.last =~ /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module LinkShrink
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,40 @@
1
+ require 'link_shrink/version'
2
+ require 'typhoeus'
3
+ require 'json'
4
+ require 'uri'
5
+
6
+ # @author Jonah Ruiz <jonah@pixelhipsters.com>
7
+ # Creates a short URL using Google's URL Shortener API
8
+ module LinkShrink
9
+
10
+ # Returns a short URL or JSON response
11
+ #
12
+ # @param url [String] long URL to be shortened
13
+ # @param options [Hash] format to be returned
14
+ # @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']
18
+ rescue
19
+ 'Problem generating short URL. Try again.'
20
+ end
21
+
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+/, '')
39
+ end
40
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'link_shrink/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'link_shrink'
8
+ spec.version = LinkShrink::VERSION
9
+ spec.authors = ['Jonah Ruiz']
10
+ spec.email = ['jonah@pixelhipsters.com']
11
+ spec.description = %q{Shrink those long and nasty links into a shorter URL}
12
+ spec.summary = %q{Shrink those long and nasty links using Google's URL Shortner API}
13
+ spec.homepage = 'https://github.com/jonahoffline/link_shrink'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+ spec.required_ruby_version = '>= 1.9.3'
21
+
22
+ spec.add_dependency 'typhoeus', '~> 0.6.3'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rspec', '~> 2.13.0'
26
+ spec.add_development_dependency 'rake'
27
+ end
@@ -0,0 +1 @@
1
+ {"kind":"urlshortener#url","id":"http://goo.gl/fbsS","longUrl":"http://www.google.com/"}
@@ -0,0 +1,56 @@
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
@@ -0,0 +1,11 @@
1
+ require 'link_shrink'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.order = 'random'
6
+ 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 ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: link_shrink
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jonah Ruiz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: typhoeus
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.6.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.6.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.13.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.13.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Shrink those long and nasty links into a shorter URL
79
+ email:
80
+ - jonah@pixelhipsters.com
81
+ executables:
82
+ - linkshrink
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .rspec
88
+ - .ruby-gemset
89
+ - .ruby-version
90
+ - .travis.yml
91
+ - Gemfile
92
+ - LICENSE
93
+ - README.md
94
+ - Rakefile
95
+ - bin/linkshrink
96
+ - lib/link_shrink.rb
97
+ - lib/link_shrink/cli.rb
98
+ - lib/link_shrink/version.rb
99
+ - link_shrink.gemspec
100
+ - spec/fixtures/response.json
101
+ - spec/link_shrink_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: https://github.com/jonahoffline/link_shrink
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: 1.9.3
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ segments:
123
+ - 0
124
+ hash: 1575228594279785560
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.25
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Shrink those long and nasty links using Google's URL Shortner API
131
+ test_files:
132
+ - spec/fixtures/response.json
133
+ - spec/link_shrink_spec.rb
134
+ - spec/spec_helper.rb