bitly_quickly 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 17cb39c19898b60d73e95bd5978331074f749ae8
4
+ data.tar.gz: 0a16234d8845c2cf02c3a847eb0aa22734eeb6ec
5
+ SHA512:
6
+ metadata.gz: 8b9acad3207d9542f1a0d720256542e96a21f55c51e49d6d0163be891f16ad47e0a667a3f6f49aaa22f193b5c2b10f50c46ac6f112fc9e3fb48f07e7510db9d5
7
+ data.tar.gz: 796293f24eefd7cef97f2be42f855d67a8ea5a7b09a936193812616e2088de4807d7ae657b85cb57be8076e4f35afad7607f9051c8d39c7486f57fa364b44efe
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bitly_quickly.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Oldrich Vetesnik
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,35 @@
1
+ # BitlyQuickly
2
+
3
+ There are many great [bit.ly](https://bitly.com/) gems, it's just that I wanted something as fast and as simple as possible.
4
+ It can only shorten URLs. :-)
5
+
6
+ It uses [Quickly](https://github.com/typhoeus/typhoeus) which is an awesome HTTP library for fast
7
+ and parallel networking. It also uses [Oj](https://github.com/ohler55/oj) which is a fast JSON parser.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'bitly_quickly'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bitly_quickly
22
+
23
+ ## Usage
24
+
25
+ client = BitlyQuickly.new(access_token: 'token')
26
+ shortened_url = client.shorten('http://www.google.com/')
27
+ shortened_urls = client.shorten([
28
+ 'https://www.google.com/',
29
+ 'https://www.youtube.com/',
30
+ 'https://www.yahoo.com/',
31
+ ])
32
+
33
+ ## Contributing
34
+
35
+ You know the routine…
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new 'spec'
5
+
6
+ # If you want to make this the default task
7
+ task default: :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bitly_quickly/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'bitly_quickly'
8
+ spec.version = BitlyQuickly::VERSION
9
+ spec.authors = ['Oldrich Vetesnik']
10
+ spec.email = ['oldrich.vetesnik@gmail.com']
11
+ spec.summary = %q{Shorten URLs with bit.ly API}
12
+ spec.description = %q{Shorten URLs with bit.ly API, make parallel requests.}
13
+ spec.homepage = 'https://github.com/ollie/bitly_quickly'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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
+
21
+ spec.add_development_dependency 'bundler', '~> 1.5'
22
+ spec.add_development_dependency 'rake', '~> 10.1'
23
+ spec.add_development_dependency 'rspec', '~> 2.14'
24
+ spec.add_development_dependency 'webmock', '~> 1.17'
25
+ spec.add_development_dependency 'simplecov', '~> 0.8'
26
+
27
+ spec.add_runtime_dependency 'typhoeus', '~> 0.6'
28
+ spec.add_runtime_dependency 'oj', '~> 2.5'
29
+ end
@@ -0,0 +1,94 @@
1
+ require 'typhoeus'
2
+ require 'oj'
3
+
4
+ require 'bitly_quickly/version'
5
+
6
+ # V3 Wrapper
7
+ class BitlyQuickly
8
+ class InvalidAccessToken < StandardError; end
9
+
10
+ DEFAULT_API_ADDRESS = 'https://api-ssl.bitly.com'
11
+ OJ_OPTIONS = {
12
+ mode: :compat, # Converts values with to_hash or to_json
13
+ symbol_keys: true, # Symbol keys to string keys
14
+ time_format: :xmlschema, # ISO8061 format
15
+ second_precision: 0, # No include milliseconds
16
+ }
17
+
18
+ attr_reader :access_token,
19
+ :api_address
20
+
21
+ def initialize(options)
22
+ @access_token = options.delete(:access_token) || raise( ArgumentError.new('Missing access_token option') )
23
+ @api_address = options.delete(:api_address) || DEFAULT_API_ADDRESS
24
+ end
25
+
26
+ def shorten(long_url_or_array)
27
+ if long_url_or_array.respond_to? :each
28
+ get_many_responses long_url_or_array
29
+ else
30
+ get_single_response long_url_or_array
31
+ end
32
+ end
33
+
34
+ def get_many_responses(array_of_long_urls)
35
+ hydra = Typhoeus::Hydra.new
36
+ responses = []
37
+
38
+ array_of_long_urls.each do |long_url|
39
+ request = make_shorten_request long_url
40
+
41
+ request.on_complete do |response|
42
+ json_response = response_to_json response
43
+ responses << json_response[:data][:url]
44
+ end
45
+
46
+ hydra.queue request
47
+ end
48
+
49
+ hydra.run
50
+ responses
51
+ end
52
+
53
+ def get_single_response(long_url)
54
+ response = get_response long_url
55
+ response[:data][:url]
56
+ end
57
+
58
+ def api_url(path)
59
+ URI.join(api_address, path).to_s
60
+ end
61
+
62
+ def make_shorten_request(long_url)
63
+ Typhoeus::Request.new(
64
+ api_url('/v3/shorten'),
65
+ {
66
+ params: {
67
+ access_token: access_token,
68
+ longUrl: long_url
69
+ },
70
+ headers: {
71
+ user_agent: 'Photolane.co'
72
+ }
73
+ }
74
+ )
75
+ end
76
+
77
+ def get_response(long_url)
78
+ request = make_shorten_request long_url
79
+ response = request.run
80
+ response_to_json response
81
+ end
82
+
83
+ def response_to_json(response)
84
+ json_response = parse_response response
85
+
86
+ raise InvalidAccessToken if json_response[:status_txt] == 'INVALID_ARG_ACCESS_TOKEN'
87
+
88
+ json_response
89
+ end
90
+
91
+ def parse_response(response)
92
+ Oj.load response.body, OJ_OPTIONS
93
+ end
94
+ end
@@ -0,0 +1,3 @@
1
+ class BitlyQuickly
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ describe BitlyQuickly do
4
+ it 'fails to make a client' do
5
+ expect { BitlyQuickly.new }.to raise_error
6
+ end
7
+
8
+ context 'With access_token' do
9
+ before do
10
+ @client = BitlyQuickly.new access_token: 'token'
11
+ end
12
+
13
+ it 'access_token' do
14
+ expect(@client.access_token).to eq('token')
15
+ end
16
+
17
+ it 'api_address' do
18
+ expect(@client.api_address).to eq(BitlyQuickly::DEFAULT_API_ADDRESS)
19
+ end
20
+ end
21
+
22
+ context 'With access_token and api_address' do
23
+ before do
24
+ @client = BitlyQuickly.new access_token: 'token', api_address: 'http://api.bitly.com'
25
+ end
26
+
27
+ it 'api_address' do
28
+ expect(@client.api_address).to eq('http://api.bitly.com')
29
+ end
30
+ end
31
+
32
+ context 'Methods' do
33
+ before do
34
+ @client = BitlyQuickly.new access_token: 'token'
35
+ end
36
+
37
+ it 'api_url' do
38
+ expect(@client.api_url('/test')).to eq('https://api-ssl.bitly.com/test')
39
+ end
40
+
41
+ it 'makes request' do
42
+ expect(@client.make_shorten_request('http://www.google.com/1')).to be_kind_of(Typhoeus::Request)
43
+ end
44
+ end
45
+
46
+ context 'Response with invalid token' do
47
+ before do
48
+ @client = BitlyQuickly.new access_token: 'token'
49
+ end
50
+
51
+ it 'raises an execption' do
52
+ expect { @client.shorten('http://www.google.com/1') }.to raise_error
53
+ end
54
+ end
55
+
56
+ context 'Response with valid token' do
57
+ before do
58
+ @client = BitlyQuickly.new access_token: 'valid_token'
59
+ end
60
+
61
+ it 'raises an execption' do
62
+ expect { @client.shorten('http://www.google.com/1') }.to_not raise_error
63
+ end
64
+
65
+ it 'status code' do
66
+ expect(@client.shorten('http://www.google.com/1')).to eq('http://pht.io/1eyUhF1')
67
+ end
68
+ end
69
+
70
+ context 'Many requests' do
71
+ before do
72
+ @client = BitlyQuickly.new access_token: 'valid_token'
73
+ end
74
+
75
+ it 'makes many requests' do
76
+ request_urls = [
77
+ 'http://www.google.com/1',
78
+ 'http://www.google.com/2',
79
+ 'http://www.google.com/3',
80
+ 'http://www.google.com/4',
81
+ 'http://www.google.com/5',
82
+ ]
83
+
84
+ response_urls = [
85
+ 'http://pht.io/1eyUhF1',
86
+ 'http://pht.io/1eyUhF2',
87
+ 'http://pht.io/1eyUhF3',
88
+ 'http://pht.io/1eyUhF4',
89
+ 'http://pht.io/1eyUhF5',
90
+ ]
91
+
92
+ expect(@client.shorten(request_urls)).to eq(response_urls)
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,29 @@
1
+ require 'simplecov'
2
+ require 'webmock/rspec' # Disable all HTTP access
3
+
4
+ # Coverage tool, needs to be started as soon as possible
5
+ SimpleCov.start do
6
+ add_filter '/spec/' # Ignore spec directory
7
+ end
8
+
9
+ require 'bitly_quickly'
10
+
11
+ RSpec.configure do |config|
12
+ config.before(:each) do
13
+ stub_request(:get, 'https://api-ssl.bitly.com/v3/shorten?access_token=token&longUrl=http://www.google.com/1').
14
+ with(headers: { 'User-Agent' => 'Photolane.co' }).
15
+ to_return(
16
+ status: 200,
17
+ body: '{ "data": [ ], "status_code": 500, "status_txt": "INVALID_ARG_ACCESS_TOKEN" }'
18
+ )
19
+
20
+ 1.upto(5) do |n|
21
+ stub_request(:get, %(https://api-ssl.bitly.com/v3/shorten?access_token=valid_token&longUrl=http://www.google.com/#{ n })).
22
+ with(headers: { 'User-Agent' => 'Photolane.co' }).
23
+ to_return(
24
+ status: 200,
25
+ body: %({ "status_code": 200, "status_txt": "OK", "data": { "long_url": "http:\/\/www.google.com\/#{ n }", "url": "http:\/\/pht.io\/1eyUhF#{ n }", "hash": "1eyUhFo", "global_hash": "2V6CFi", "new_hash": 0 } })
26
+ )
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitly_quickly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Oldrich Vetesnik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.17'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.17'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: typhoeus
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.6'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: oj
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.5'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.5'
111
+ description: Shorten URLs with bit.ly API, make parallel requests.
112
+ email:
113
+ - oldrich.vetesnik@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - bitly_quickly.gemspec
125
+ - lib/bitly_quickly.rb
126
+ - lib/bitly_quickly/version.rb
127
+ - spec/lib/bitly_quickly_spec.rb
128
+ - spec/spec_helper.rb
129
+ homepage: https://github.com/ollie/bitly_quickly
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.2.1
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: Shorten URLs with bit.ly API
153
+ test_files:
154
+ - spec/lib/bitly_quickly_spec.rb
155
+ - spec/spec_helper.rb
156
+ has_rdoc: