multi_bitly 0.1.0

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 @@
1
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ multi_bitly
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sharethrough, Inc. http://www.sharethrough.com/
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,74 @@
1
+ # MultiBitly
2
+
3
+ Allows access to the bit.ly API with multiple accounts.
4
+
5
+ MultiBitly is a wrapper around the [Bitly](https://github.com/philnash/bitly) gem that makes it easy to generate multiple shortened Bitly links for a given URL.
6
+
7
+ When shortening a URL, Bitly will return the same hash regardless of how many times you request a new one. One way around this is to use a different Bitly account to shorten the same URL, resulting in a different hash.
8
+
9
+ Such a pain. Until now.
10
+
11
+ Now, with a list of Bitly accounts, MultiBitly will automatically shorten URLs for each account, resulting in a list of shortened Bitly links for one URL.
12
+
13
+ ### So what?
14
+
15
+ If you've ever wanted to use Bitly links as distribution channels to track a URL, you're going to be a [whole](http://cutestuff.co/wp-content/uploads/2011/09/tumblr_lr6ed9uQSi1qzfvwyo1_500.jpg) [lot](http://www.stevecurtin.com/blog/wp-content/uploads/2011/10/Cute-puppy.jpg) [happier](http://onwardstate.com/wp-content/uploads/2013/04/puppy1.jpg). Just keep track of which account has already shortened the URL, and we'll take care of the rest.
16
+
17
+ ## Installation
18
+
19
+ Add this to your Gemfile
20
+
21
+ gem install multi_bitly
22
+
23
+ MultiBitly currently requires an ActiveRecord class that keeps track of the usernames and URLs already shortened through Bitly. The default name is `BitlyAccount`, but you can override it in the config.
24
+
25
+ You will also need to create an initializer to set the account information. You can find a Bitly account's API key [here](https://bitly.com/a/your_api_key).
26
+
27
+ ## Show me the code
28
+
29
+ # config/initializers/multi_bitly.rb
30
+ MultiBitly::configure do |config|
31
+ config.accounts = [
32
+ { username: 'user1', api_key: 'key1' },
33
+ { username: 'user1', api_key: 'key2' }
34
+ ]
35
+ end
36
+
37
+ # app/models/bitly_account.rb
38
+ class BitlyAccount < ActiveRecord::Base
39
+ attr_accessible :username, :url
40
+
41
+ validates :username, :url, presence: true
42
+ validates :username, uniqueness: { scope: :url } # one URL per user
43
+ end
44
+
45
+ # a sample class to shorten a URL and mark the account that shortened it
46
+ class ShortenAllTheThings
47
+
48
+ def initialize(url)
49
+ @url = url
50
+ end
51
+
52
+ def shorten
53
+ client = MultiBitly::Client.new
54
+ response = client.shorten(@url)
55
+ BitlyAccount.create!(username: response.account.username, url: 'http://google.com')
56
+ response.short_url
57
+ end
58
+
59
+ ShortenAllTheThings.new('http://google.com').shorten # => http://bit.ly/900913
60
+ ShortenAllTheThings.new('http://google.com').shorten # => http://bit.ly/11JpHp5
61
+ # now we're all out of acounts so we'll use the last one
62
+ ShortenAllTheThings.new('http://google.com').shorten # => http://bit.ly/11JpHp5
63
+
64
+ ## Contributors
65
+ * [Danny Olson](https://github.com/dbolson)
66
+
67
+ ## Contributions welcome
68
+ Any contributions are welcome. Simply follow the steps below to get started:
69
+
70
+ 1. Fork it
71
+ 2. Create your feature branch (git checkout -b my-new-feature)
72
+ 3. Commit your changes (git commit -am 'Added some feature')
73
+ 4. Push to the branch (git push origin my-new-feature)
74
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task test: :spec
8
+ task default: :spec
9
+ rescue LoadError => e
10
+ desc 'Run specs'
11
+ task :default do
12
+ abort 'rake task is not available.'
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ module MultiBitly
2
+ class Account
3
+ def initialize(url)
4
+ @url = url
5
+ end
6
+
7
+ def username
8
+ finder.new(url).account[:username]
9
+ end
10
+
11
+ def api_key
12
+ finder.new(url).account[:api_key]
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :url
18
+
19
+ def finder
20
+ AccountFinder
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ module MultiBitly
2
+ class AccountFinder
3
+ def initialize(url)
4
+ @url = url
5
+ @accounts = MultiBitly.configuration.accounts
6
+ @data_store = MultiBitly.configuration.data_store
7
+ end
8
+
9
+ def account
10
+ last_account = nil
11
+
12
+ accounts.each do |account|
13
+ found_account = existing_account(account)
14
+ if found_account.empty?
15
+ return account
16
+ else
17
+ last_account = account
18
+ end
19
+ end
20
+ last_account
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :url,
26
+ :accounts,
27
+ :data_store
28
+
29
+ def existing_account(account)
30
+ data_store.where(username: account[:username], url: url)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ module MultiBitly
2
+ class Client
3
+ class Error < StandardError; end
4
+
5
+ def initialize(api_client=Bitly)
6
+ @api_client = api_client
7
+ end
8
+
9
+ def shorten(url)
10
+ account = account(url)
11
+ api_response = client(account).shorten(url)
12
+ response(account, api_response)
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :api_client
18
+
19
+ def account(url)
20
+ Account.new(url)
21
+ end
22
+
23
+ def response(account, api_response)
24
+ Response.new(account, api_response)
25
+ end
26
+
27
+ def client(account)
28
+ @_client ||= api_client.new(account.username, account.api_key)
29
+ rescue => exception
30
+ raise Error.new("#{exception.class.to_s}: '#{exception.message}'")
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ module MultiBitly
2
+ class Configuration
3
+ attr_accessor :accounts,
4
+ :data_store
5
+
6
+ def initialize
7
+ @accounts = []
8
+ end
9
+
10
+ def data_store
11
+ @data_store ||= ::BitlyAccount
12
+ end
13
+ end
14
+
15
+ class << self
16
+ attr_accessor :configuration
17
+ end
18
+
19
+ def self.configure
20
+ self.configuration ||= Configuration.new
21
+ yield configuration
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ require 'delegate'
2
+
3
+ module MultiBitly
4
+ class Response < SimpleDelegator
5
+ attr_reader :account
6
+
7
+ def initialize(account, response)
8
+ @account = account
9
+ super(response)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module MultiBitly
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'multi_bitly/version'
2
+ require 'multi_bitly/account'
3
+ require 'multi_bitly/account_finder'
4
+ require 'multi_bitly/client'
5
+ require 'multi_bitly/configuration'
6
+ require 'multi_bitly/response'
7
+
8
+ module MultiBitly
9
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multi_bitly/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'multi_bitly'
8
+ spec.version = MultiBitly::VERSION
9
+ spec.authors = ['Danny Olson']
10
+ spec.email = ['dolson@sharethrough.com']
11
+ spec.description = %q{Allows access to the bit.ly API with multiple accounts.}
12
+ spec.summary = %q{Allows access to the bit.ly API with multiple accounts.}
13
+ spec.homepage = 'https://github.com/sharethrough/multi_bitly'
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
+
21
+ spec.add_dependency 'bitly'
22
+ spec.add_dependency 'json', '>= 1.7.7'
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec', '~> 2.13'
26
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe MultiBitly::AccountFinder do
4
+ let(:account_finder) { MultiBitly::AccountFinder }
5
+ let(:data_store) { BitlyAccount }
6
+ let(:account1) {{ username: 'user1', api_key: 'key1' }}
7
+ let(:account2) {{ username: 'user2', api_key: 'key2' }}
8
+
9
+ before do
10
+ set_configuration
11
+ end
12
+
13
+ describe '#account' do
14
+ context 'with an unused account' do
15
+ it 'gets the attributes for the account' do
16
+ data_store.stub(:where).with(username: 'user1', url: 'example.com').and_return([])
17
+ found_account = account_finder.new('example.com').account
18
+ expect(found_account).to eq(account1)
19
+ end
20
+ end
21
+
22
+ context 'with a used account' do
23
+ it 'gets the attributes for the next available account' do
24
+ data_store.stub(:where).with(username: 'user1', url: 'example.com').and_return([data_store])
25
+ data_store.stub(:where).with(username: 'user2', url: 'example.com').and_return([])
26
+ found_account = account_finder.new('example.com').account
27
+ expect(found_account).to eq(account2)
28
+ end
29
+ end
30
+
31
+ context 'without any available accounts' do
32
+ it 'finds the last used account' do
33
+ data_store.stub(:where).with(username: 'user1', url: 'example.com').and_return([data_store])
34
+ data_store.stub(:where).with(username: 'user2', url: 'example.com').and_return([data_store])
35
+ found_account = account_finder.new('example.com').account
36
+ expect(found_account).to eq(account2)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe MultiBitly::Account do
4
+ let(:account) { MultiBitly::Account }
5
+
6
+ before do
7
+ set_configuration
8
+ end
9
+
10
+ describe '#username' do
11
+ it 'gets the username from the found account' do
12
+ found_account = account.new('example.com')
13
+ expect(found_account.username).to eq('user1')
14
+ end
15
+ end
16
+
17
+ describe '#api_key' do
18
+ it 'gets the api key from the found account' do
19
+ found_account = account.new('example.com')
20
+ expect(found_account.api_key).to eq('key1')
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe MultiBitly::Client do
4
+ let(:client) { MultiBitly::Client }
5
+ let(:api_client) { ApiClient }
6
+
7
+ before do
8
+ set_configuration
9
+ end
10
+
11
+ context '#shorten' do
12
+ it 'delegates to the underlying client with an account' do
13
+ instance = client.new(api_client)
14
+ expect(instance.shorten('http://google.com').user_hash).to eq('g123')
15
+ end
16
+
17
+ context 'when the underlying client raises an exception' do
18
+ let(:account) { stub(:account, username: nil, api_key: nil) }
19
+
20
+ before do
21
+ api_client.stub(:new).and_raise(StandardError, 'api error')
22
+ end
23
+
24
+ it 'rescues and reraises the exception' do
25
+ instance = client.new(api_client)
26
+ expect {
27
+ instance.shorten('http://google.com')
28
+ }.to raise_error(MultiBitly::Client::Error, "StandardError: 'api error'")
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe MultiBitly::Configuration do
4
+ describe '#accounts' do
5
+ context 'when set' do
6
+ before do
7
+ MultiBitly::configure do |config|
8
+ config.accounts = [
9
+ { username: 'user1', api_key: 'key1' },
10
+ { username: 'user2', api_key: 'key2' }
11
+ ]
12
+ end
13
+ end
14
+
15
+ after do
16
+ MultiBitly.configuration = nil
17
+ end
18
+
19
+ it 'uses the supplied accounts' do
20
+ expect(MultiBitly.configuration.accounts).to eq([
21
+ { username: 'user1', api_key: 'key1' },
22
+ { username: 'user2', api_key: 'key2' }
23
+ ])
24
+ end
25
+ end
26
+
27
+ context 'when not set' do
28
+ before do
29
+ MultiBitly::configure do |config|
30
+ end
31
+ end
32
+
33
+ it 'is an empty list' do
34
+ expect(MultiBitly.configuration.accounts).to eq([])
35
+ end
36
+ end
37
+ end
38
+
39
+ describe '#data_store' do
40
+ context 'when set' do
41
+ let(:data_store) { stub(:data_store) }
42
+
43
+ before do
44
+ MultiBitly::configure do |config|
45
+ config.data_store = data_store
46
+ end
47
+ end
48
+
49
+ after do
50
+ MultiBitly.configuration = nil
51
+ end
52
+
53
+ it 'uses the supplied data store' do
54
+ expect(MultiBitly.configuration.data_store).to eq(data_store)
55
+ end
56
+ end
57
+
58
+ context 'when not set' do
59
+ before do
60
+ MultiBitly::configure do |config|
61
+ end
62
+ end
63
+
64
+ it 'sets a default' do
65
+ expect(MultiBitly.configuration.data_store).to eq(BitlyAccount)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe MultiBitly::Response do
4
+ let(:responder) { MultiBitly::Response }
5
+
6
+ it 'delegates to the underlying response' do
7
+ account = stub(:account)
8
+ response = stub(:response, a_method: 'underlying method')
9
+ expect(responder.new(account, response).a_method).to eq('underlying method')
10
+ end
11
+
12
+ it 'sets the account' do
13
+ account = stub(:account)
14
+ expect(responder.new(account, stub).account).to eq(account)
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ require 'bitly'
2
+ require 'multi_bitly'
3
+
4
+ Bitly.use_api_version_3
5
+
6
+ class BitlyAccount
7
+ def self.where(*args)
8
+ []
9
+ end
10
+ end
11
+
12
+ class ApiClient
13
+ def initialize(username, api_key)
14
+ end
15
+
16
+ def shorten(url)
17
+ Struct.new('ApiClient') do
18
+ def long_url; 'http://www.google.com'; end
19
+ def short_url; 'http://bit.ly/g123'; end
20
+ def user_hash; 'g123'; end
21
+ end.new
22
+ end
23
+ end
24
+
25
+ def set_configuration
26
+ MultiBitly::configure do |config|
27
+ config.accounts = [
28
+ { username: 'user1', api_key: 'key1' },
29
+ { username: 'user2', api_key: 'key2' }
30
+ ]
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_bitly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Danny Olson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bitly
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.7.7
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.7.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
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: '1.3'
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
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '2.13'
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: '2.13'
94
+ description: Allows access to the bit.ly API with multiple accounts.
95
+ email:
96
+ - dolson@sharethrough.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - .ruby-gemset
104
+ - .ruby-version
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/multi_bitly.rb
110
+ - lib/multi_bitly/account.rb
111
+ - lib/multi_bitly/account_finder.rb
112
+ - lib/multi_bitly/client.rb
113
+ - lib/multi_bitly/configuration.rb
114
+ - lib/multi_bitly/response.rb
115
+ - lib/multi_bitly/version.rb
116
+ - multi_bitly.gemspec
117
+ - spec/multi_bitly/account_finder_spec.rb
118
+ - spec/multi_bitly/account_spec.rb
119
+ - spec/multi_bitly/client_spec.rb
120
+ - spec/multi_bitly/configuration_spec.rb
121
+ - spec/multi_bitly/response_spec.rb
122
+ - spec/spec_helper.rb
123
+ homepage: https://github.com/sharethrough/multi_bitly
124
+ licenses:
125
+ - MIT
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ segments:
137
+ - 0
138
+ hash: -2121263376112597564
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ segments:
146
+ - 0
147
+ hash: -2121263376112597564
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 1.8.25
151
+ signing_key:
152
+ specification_version: 3
153
+ summary: Allows access to the bit.ly API with multiple accounts.
154
+ test_files:
155
+ - spec/multi_bitly/account_finder_spec.rb
156
+ - spec/multi_bitly/account_spec.rb
157
+ - spec/multi_bitly/client_spec.rb
158
+ - spec/multi_bitly/configuration_spec.rb
159
+ - spec/multi_bitly/response_spec.rb
160
+ - spec/spec_helper.rb