get_shorty 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/LICENSE +20 -0
- data/README.md +101 -0
- data/Rakefile +47 -0
- data/VERSION +1 -0
- data/get_shorty.gemspec +77 -0
- data/get_shorty.rb +1 -0
- data/init.rb +1 -0
- data/lib/get_shorty.rb +75 -0
- data/lib/providers/bitly.rb +3 -0
- data/lib/providers/bitly/client.rb +44 -0
- data/lib/providers/bitly/config.rb +34 -0
- data/lib/providers/bitly/response.rb +44 -0
- data/lib/providers/init.rb +1 -0
- data/spec/active_record_helper.rb +24 -0
- data/spec/bitly/client_spec.rb +70 -0
- data/spec/bitly/config_spec.rb +9 -0
- data/spec/bitly/response_spec.rb +58 -0
- data/spec/bitly/spec_helper.rb +6 -0
- data/spec/get_shorty_spec.rb +66 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +14 -0
- metadata +142 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.DS_Store
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Louis Gillies
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# get_shorty
|
2
|
+
|
3
|
+
A plugin for rails that shortens a url using the bitly shortening service and persists to the model.
|
4
|
+
|
5
|
+
* Default shortening provider is bitly
|
6
|
+
* Easily extendible by adding client / provider for other shortening service eg t.co from twitter when a public api is available.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
### As a rails plugin
|
11
|
+
script/plugin install git://github.com/playgood/get_shorty.git
|
12
|
+
|
13
|
+
Then in the model that you want to add a short url to - this model must have a `short_url` column (this is the default but can be customised see options below)
|
14
|
+
|
15
|
+
Class ModelName
|
16
|
+
include GetShorty
|
17
|
+
has_short_url
|
18
|
+
end
|
19
|
+
|
20
|
+
has_short_url by default relies on the [Bitly](http://bit.ly/ "Bitly") API to shorten urls
|
21
|
+
You need to register with Bitly to get a login and api_key.
|
22
|
+
This needs to be set in a rails initializer to allow the Bitly provider to connect to your api account.
|
23
|
+
|
24
|
+
In your initializer
|
25
|
+
|
26
|
+
Bitly::Config.set do |config|
|
27
|
+
config.login = "my_bitly_login"
|
28
|
+
config.api_key = "BLAH_BLAH_BLAH"
|
29
|
+
end
|
30
|
+
|
31
|
+
or with a yaml file with the api_key and login keys.
|
32
|
+
|
33
|
+
api_key: "BLAH_BLAH_BLAH"
|
34
|
+
|
35
|
+
login: "my_bitly_login"
|
36
|
+
|
37
|
+
|
38
|
+
Bitly::Config.set do |config|
|
39
|
+
config.config_file = File.join(Rails.root, "path/to/config/file")
|
40
|
+
end
|
41
|
+
|
42
|
+
Or
|
43
|
+
|
44
|
+
You can roll out your own shortening provider - all it has to do is respond to the shorten method.
|
45
|
+
This can then be set using has_short_url option :shortening_service
|
46
|
+
|
47
|
+
For example:
|
48
|
+
Class ModelName
|
49
|
+
include GetShorty
|
50
|
+
|
51
|
+
has_short_url :shortening_service => MyShortener
|
52
|
+
end
|
53
|
+
|
54
|
+
_This assumes you have defined a Class `MyShortener` which respond to the method `shorten` with the parameter url_
|
55
|
+
|
56
|
+
## Parameters & Options
|
57
|
+
|
58
|
+
`has_short_url` has the following options:
|
59
|
+
* `long_url_host` - this is required so the rails url_helper method work. Takes a string or a hash with a value for each environment.
|
60
|
+
* `:shortening_service` - default is Bitly::Client.connection - you can create your own (it just needs to be an object that responds to `.shorten(url)`)
|
61
|
+
* `:short_url_method` - default is :short_url (most commonly a db column - but not necessarily)
|
62
|
+
* `:long_url_method` - default is :generate_long_url - In a rails app this would probably be a resourceful route helper method for example page_url.
|
63
|
+
|
64
|
+
|
65
|
+
## Integration
|
66
|
+
|
67
|
+
To try to keep the design of this plugin / gem clean and allow for future integration into more ruby frameworks there are no automatic hooks or callbacks to trigger the
|
68
|
+
shortening service.
|
69
|
+
|
70
|
+
You will have to add these yourself.
|
71
|
+
|
72
|
+
For example in rails
|
73
|
+
|
74
|
+
Class ModelName
|
75
|
+
include GetShorty
|
76
|
+
has_short_url
|
77
|
+
|
78
|
+
after_create :set_short_url
|
79
|
+
end
|
80
|
+
|
81
|
+
This allows you more flexibility than adding callbacks into the plugin. For example you may want to process the api calls to bitly out of process using delayed_job or something similar.
|
82
|
+
|
83
|
+
|
84
|
+
## Note on Patches/Pull Requests
|
85
|
+
|
86
|
+
* Fork the project.
|
87
|
+
* Make your feature addition or bug fix.
|
88
|
+
* Add tests for it. This is important so I don't break it in a
|
89
|
+
future version unintentionally.
|
90
|
+
* Commit, do not mess with rakefile, version, or history.
|
91
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
92
|
+
* Send me a pull request. Bonus points for topic branches.
|
93
|
+
|
94
|
+
## TODO
|
95
|
+
|
96
|
+
* support for other ORM - at present only works with ActiveRecord
|
97
|
+
* support for other shortening url services.
|
98
|
+
|
99
|
+
## Copyright
|
100
|
+
|
101
|
+
Copyright (c) 2010 Louis Gillies. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "get_shorty"
|
8
|
+
gem.summary = %Q{Shorten resource urls}
|
9
|
+
gem.description = %Q{Shorten the url of a resource - at present supports active record models and caches the short url to a database column.}
|
10
|
+
gem.email = "louisgillies@yahoo.co.uk"
|
11
|
+
gem.homepage = "http://github.com/playgood/get_shorty"
|
12
|
+
gem.authors = ["Louis Gillies"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_dependency "rest-client", ">= 1.4.2"
|
15
|
+
gem.add_dependency "nokogiri", ">= 1.4.2"
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'spec/rake/spectask'
|
24
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
+
spec.libs << 'lib' << 'spec'
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :spec => :check_dependencies
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
require 'rake/rdoctask'
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
41
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
42
|
+
|
43
|
+
rdoc.rdoc_dir = 'rdoc'
|
44
|
+
rdoc.title = "get_shorty #{version}"
|
45
|
+
rdoc.rdoc_files.include('README*')
|
46
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
47
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
data/get_shorty.gemspec
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{get_shorty}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Louis Gillies"]
|
12
|
+
s.date = %q{2010-09-12}
|
13
|
+
s.description = %q{Shorten the url of a resource - at present supports active record models and caches the short url to a database column.}
|
14
|
+
s.email = %q{louisgillies@yahoo.co.uk}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.md",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"get_shorty.gemspec",
|
26
|
+
"get_shorty.rb",
|
27
|
+
"init.rb",
|
28
|
+
"lib/get_shorty.rb",
|
29
|
+
"lib/providers/bitly.rb",
|
30
|
+
"lib/providers/bitly/client.rb",
|
31
|
+
"lib/providers/bitly/config.rb",
|
32
|
+
"lib/providers/bitly/response.rb",
|
33
|
+
"lib/providers/init.rb",
|
34
|
+
"spec/active_record_helper.rb",
|
35
|
+
"spec/bitly/client_spec.rb",
|
36
|
+
"spec/bitly/config_spec.rb",
|
37
|
+
"spec/bitly/response_spec.rb",
|
38
|
+
"spec/bitly/spec_helper.rb",
|
39
|
+
"spec/get_shorty_spec.rb",
|
40
|
+
"spec/spec.opts",
|
41
|
+
"spec/spec_helper.rb"
|
42
|
+
]
|
43
|
+
s.homepage = %q{http://github.com/playgood/get_shorty}
|
44
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
45
|
+
s.require_paths = ["lib"]
|
46
|
+
s.rubygems_version = %q{1.3.7}
|
47
|
+
s.summary = %q{Shorten resource urls}
|
48
|
+
s.test_files = [
|
49
|
+
"spec/active_record_helper.rb",
|
50
|
+
"spec/bitly/client_spec.rb",
|
51
|
+
"spec/bitly/config_spec.rb",
|
52
|
+
"spec/bitly/response_spec.rb",
|
53
|
+
"spec/bitly/spec_helper.rb",
|
54
|
+
"spec/get_shorty_spec.rb",
|
55
|
+
"spec/spec_helper.rb"
|
56
|
+
]
|
57
|
+
|
58
|
+
if s.respond_to? :specification_version then
|
59
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
60
|
+
s.specification_version = 3
|
61
|
+
|
62
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
63
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
64
|
+
s.add_runtime_dependency(%q<rest-client>, [">= 1.4.2"])
|
65
|
+
s.add_runtime_dependency(%q<nokogiri>, [">= 1.4.2"])
|
66
|
+
else
|
67
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
68
|
+
s.add_dependency(%q<rest-client>, [">= 1.4.2"])
|
69
|
+
s.add_dependency(%q<nokogiri>, [">= 1.4.2"])
|
70
|
+
end
|
71
|
+
else
|
72
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
73
|
+
s.add_dependency(%q<rest-client>, [">= 1.4.2"])
|
74
|
+
s.add_dependency(%q<nokogiri>, [">= 1.4.2"])
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
data/get_shorty.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'lib/get_shorty'
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'get_shorty'
|
data/lib/get_shorty.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "providers/init")
|
2
|
+
|
3
|
+
module GetShorty
|
4
|
+
def self.included(base) #:nodoc:
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
# Returns the short_url_options defined by each call to
|
11
|
+
# has_short_url.
|
12
|
+
def short_url_options
|
13
|
+
@short_url_options
|
14
|
+
end
|
15
|
+
|
16
|
+
def shortening_serivce
|
17
|
+
@shortening_serivce
|
18
|
+
end
|
19
|
+
|
20
|
+
def has_short_url(options={})
|
21
|
+
include InstanceMethods
|
22
|
+
include ActionController::UrlWriter if defined?(ActionController )# non mvc - but need to know the url for model instance to persist the short_url.
|
23
|
+
@short_url_options = {:long_url_method => :generate_long_url, :short_url_method => :short_url}.update(options)
|
24
|
+
@shortening_serivce = options[:shortener_service] || Bitly::Client.connection
|
25
|
+
set_long_url_host options[:long_url_host]
|
26
|
+
end
|
27
|
+
|
28
|
+
# for Rails when using UrlWriter
|
29
|
+
def set_long_url_host(value)
|
30
|
+
self.default_url_options[:host] = if value.is_a?(Hash) && defined?(Rails)
|
31
|
+
value.stringify_keys[Rails.env]
|
32
|
+
else
|
33
|
+
value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module InstanceMethods
|
39
|
+
|
40
|
+
def has_short_url?
|
41
|
+
self.send("#{self.class.short_url_options[:short_url_method]}?")
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_short_url!(long_url=nil)
|
45
|
+
short_url = get_short_url(long_url)
|
46
|
+
self.update_attributes( self.class.short_url_options[:short_url_method] => short_url ) unless has_short_url? || self.readonly?
|
47
|
+
short_url
|
48
|
+
end
|
49
|
+
|
50
|
+
def set_short_url(short_url=generate_short_url)
|
51
|
+
if short_url
|
52
|
+
self.update_attributes( self.class.short_url_options[:short_url_method] => short_url )
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_short_url(url=nil)
|
57
|
+
self.send(self.class.short_url_options[:short_url_method]) || generate_short_url(url)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Goes against the MVC conventions (although we are generating this for the purpose of creating a short_url for the database so it is to do with the Model.)
|
61
|
+
def generate_long_url()
|
62
|
+
begin
|
63
|
+
send("#{self.class.name.downcase}_url", self) unless self.new_record? # Can't generate the url until we have an id and title.
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def generate_short_url(long_url=nil)
|
68
|
+
url = long_url || self.send(self.class.short_url_options[:long_url_method])
|
69
|
+
if url
|
70
|
+
self.class.shortening_serivce.shorten( url )
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# Simple wrapper so we can store the api authentication details in the config/bitly.yml file.
|
2
|
+
require 'rest_client'
|
3
|
+
|
4
|
+
module Bitly
|
5
|
+
class Client
|
6
|
+
|
7
|
+
attr_accessor :format, :login, :api_key, :response
|
8
|
+
BASE_URI = "http://api.bit.ly/v3"
|
9
|
+
|
10
|
+
|
11
|
+
def initialize(login, api_key, options={})
|
12
|
+
@login = login
|
13
|
+
@api_key = api_key
|
14
|
+
@format = options[:format] || :json
|
15
|
+
end
|
16
|
+
|
17
|
+
def default_params
|
18
|
+
{:login => login, :apiKey => api_key, :format => format}
|
19
|
+
end
|
20
|
+
|
21
|
+
def get(end_point, options={})
|
22
|
+
RestClient.get(BASE_URI + end_point, :params => default_params.merge(options))
|
23
|
+
end
|
24
|
+
|
25
|
+
def shorten(url)
|
26
|
+
response = get("/shorten", :longUrl => url)
|
27
|
+
@response = ShortenResponse.new(response)
|
28
|
+
@response.url
|
29
|
+
rescue
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
|
33
|
+
# creates a connection with default loaded.
|
34
|
+
def self.connection(options={})
|
35
|
+
@connection ||= Bitly::Client.new(Bitly::Config.login, Bitly::Config.api_key, options)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Bitly
|
2
|
+
module Config
|
3
|
+
|
4
|
+
class << self
|
5
|
+
attr_accessor :login, :api_key, :config_file
|
6
|
+
|
7
|
+
def set(&block)
|
8
|
+
self.class_eval(&block) if block_given?
|
9
|
+
load_config
|
10
|
+
end
|
11
|
+
|
12
|
+
def load_config()
|
13
|
+
unless config_file.nil?
|
14
|
+
api_details = YAML.load(File.open(config_file)).symbolize_keys
|
15
|
+
@api_key, @login = api_details[:api_key], api_details[:login]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def login
|
20
|
+
return @login unless @login.nil?
|
21
|
+
self.load_config
|
22
|
+
raise "Bitly Login must be defined" if @login.nil?
|
23
|
+
@login
|
24
|
+
end
|
25
|
+
|
26
|
+
def api_key
|
27
|
+
return @api_key unless @api_key.nil?
|
28
|
+
self.load_config
|
29
|
+
raise "Bitly Api key must be defined" if @api_key.nil?
|
30
|
+
@api_key
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module Bitly
|
4
|
+
class Response
|
5
|
+
attr_reader :format
|
6
|
+
|
7
|
+
def parse; end
|
8
|
+
|
9
|
+
def initialize(response, format=:json)
|
10
|
+
@response = response
|
11
|
+
@format = format
|
12
|
+
@parser = parser
|
13
|
+
parse
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def parser
|
18
|
+
case format
|
19
|
+
when :json
|
20
|
+
return JSON
|
21
|
+
when :xml
|
22
|
+
return Nokogiri::XML
|
23
|
+
else
|
24
|
+
raise UnsupportedFormat, format
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class ShortenResponse < Response
|
30
|
+
attr_reader :url, :long_url, :hash, :global_hash
|
31
|
+
|
32
|
+
# Assuming format is always json for now.
|
33
|
+
def parse
|
34
|
+
@parsed_response = @parser.parse(@response)
|
35
|
+
@url = @parsed_response["data"]["url"]
|
36
|
+
@hash = @parsed_response["data"]["hash"]
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
class UnsupportedFormat < StandardError
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "bitly")
|
@@ -0,0 +1,24 @@
|
|
1
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
4
|
+
|
5
|
+
ActiveRecord::Schema.define(:version => 1) do
|
6
|
+
create_table :posts do |t|
|
7
|
+
t.string :title, :url, :short_url
|
8
|
+
t.text :excerpt, :body
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
set_bitly_configuration
|
13
|
+
|
14
|
+
class Post < ActiveRecord::Base
|
15
|
+
# to replicate rails url_writer
|
16
|
+
def self.default_url_options
|
17
|
+
{}
|
18
|
+
end
|
19
|
+
|
20
|
+
include GetShorty
|
21
|
+
has_short_url
|
22
|
+
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Bitly::Client do
|
4
|
+
before do
|
5
|
+
set_bitly_configuration
|
6
|
+
end
|
7
|
+
|
8
|
+
[:new, :connection].each do |method|
|
9
|
+
it "should respond to #{method}" do
|
10
|
+
Bitly::Client.respond_to?(method).should be_true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return a authenticated instance with connection" do
|
15
|
+
Bitly::Client.connection.class.should == Bitly::Client
|
16
|
+
end
|
17
|
+
|
18
|
+
context "an instance" do
|
19
|
+
|
20
|
+
let(:client) { Bitly::Client.new("login_test", "api_key_test")}
|
21
|
+
|
22
|
+
it "should set the api_key" do
|
23
|
+
client.respond_to?(:api_key).should be_true
|
24
|
+
client.api_key.should == "api_key_test"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should set the login" do
|
28
|
+
client.respond_to?(:login).should be_true
|
29
|
+
client.login.should == "login_test"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should set the format to json by default" do
|
33
|
+
client.respond_to?(:format).should be_true
|
34
|
+
client.format.should == :json
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should have a shorten method" do
|
38
|
+
client.respond_to?(:shorten).should be_true
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should set the default params" do
|
42
|
+
client.respond_to?(:default_params).should be_true
|
43
|
+
client.default_params.should == {:login => "login_test", :apiKey => "api_key_test", :format => :json}
|
44
|
+
end
|
45
|
+
|
46
|
+
context "with custom options" do
|
47
|
+
it "should set the format to xml" do
|
48
|
+
new_client = Bitly::Client.new("login", "key", :format => :xml)
|
49
|
+
new_client.format.should == :xml
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "shortening a url" do
|
54
|
+
before do
|
55
|
+
RestClient.should_receive(:get).and_return({ :data => {:url => "test.com", :hash => "blah" } }.to_json)
|
56
|
+
@url = client.shorten("test_url")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should return the url" do
|
60
|
+
@url.should == "test.com"
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should keep the response in memory" do
|
64
|
+
client.response.should_not be_nil
|
65
|
+
client.response.url.should == @url
|
66
|
+
client.response.hash.should == "blah"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Bitly::Config do
|
4
|
+
it "should load the config/bitly.yml file and read the keys" do
|
5
|
+
Bitly::Config.respond_to?(:load_config).should be_true
|
6
|
+
Bitly::Config.respond_to?(:api_key).should be_true
|
7
|
+
Bitly::Config.respond_to?(:login).should be_true
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Bitly::Response do
|
4
|
+
|
5
|
+
let(:json_response) { { :data => {:url => "test.com", :hash => "blah" } }.to_json }
|
6
|
+
let(:xml_response) { "<data><url>test.com</url><hash>blah</hash></data>"}
|
7
|
+
context "an instance" do
|
8
|
+
it "should error without a response" do
|
9
|
+
lambda {
|
10
|
+
Bitly::Response.new
|
11
|
+
}.should raise_error(ArgumentError)
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:response) { Bitly::Response.new( json_response ) }
|
15
|
+
|
16
|
+
it "should deafult the format to :json" do
|
17
|
+
response.respond_to?(:format).should be_true
|
18
|
+
response.format.should == :json
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should set the correct parser" do
|
22
|
+
response.respond_to?(:parser).should be_true
|
23
|
+
response.parser.should == JSON
|
24
|
+
end
|
25
|
+
|
26
|
+
context "formatted as xml" do
|
27
|
+
let(:xml) { Bitly::Response.new( xml_response, :xml) }
|
28
|
+
|
29
|
+
it "should format as xml" do
|
30
|
+
xml.format.should == :xml
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should set the xml parser" do
|
34
|
+
xml.respond_to?(:parser).should be_true
|
35
|
+
xml.parser.should == Nokogiri::XML
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "an unsupported format" do
|
40
|
+
it "should raise an error" do
|
41
|
+
lambda { Bitly::Response.new( json_response, :pink ) }.should raise_error(Bitly::UnsupportedFormat)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
describe Bitly::ShortenResponse do
|
48
|
+
let(:shorten_response) { Bitly::ShortenResponse.new( json_response ) }
|
49
|
+
|
50
|
+
{:url => "test.com", :hash => "blah" }.each do |method, value|
|
51
|
+
it "should set the #{method}" do
|
52
|
+
shorten_response.respond_to?(method).should be_true
|
53
|
+
shorten_response.send(method).should == value
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe GetShorty do
|
4
|
+
before(:each) do
|
5
|
+
Bitly::Config.set do |config|
|
6
|
+
config.login = "tester"
|
7
|
+
config.api_key = "test_key"
|
8
|
+
end
|
9
|
+
@post = Post.new(:url => "http://testserver.com")
|
10
|
+
end
|
11
|
+
|
12
|
+
context "when included" do
|
13
|
+
it "should add the has_short_url class method" do
|
14
|
+
Post.respond_to?(:has_short_url).should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should add the the shortener_service method" do
|
18
|
+
Post.respond_to?(:shortening_serivce).should be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should set the default shortener service to bitly" do
|
22
|
+
Post.shortening_serivce.should == Bitly::Client.connection
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should set the default options" do
|
26
|
+
Post.short_url_options[:short_url_method].should == :short_url
|
27
|
+
Post.short_url_options[:long_url_method].should == :generate_long_url
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "an instance" do
|
33
|
+
[:set_short_url, :get_short_url, :generate_short_url, :generate_long_url, :has_short_url?].each do |method|
|
34
|
+
it "should respond to #{method}" do
|
35
|
+
@post.respond_to?(method).should be_true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
before(:each) do
|
40
|
+
# stub web call
|
41
|
+
Bitly::Client.connection.stub!(:shorten => "v.co")
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should not have a short_url" do
|
45
|
+
@post.has_short_url?.should be_false
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should not call the service when the record is not saved" do
|
49
|
+
Bitly::Client.connection.should_not_receive(:shorten)
|
50
|
+
@post.get_short_url
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should not update the record until the record is saved" do
|
54
|
+
@post.should_not_receive(:update_attributes)
|
55
|
+
@post.set_short_url
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should update the record when the short url is set" do
|
59
|
+
@post.save
|
60
|
+
@post.stub!(:post_url => "http://www.example.com/post/1")
|
61
|
+
@post.should_receive(:update_attributes).with( {Post.short_url_options[:short_url_method] => "v.co"} )
|
62
|
+
@post.set_short_url
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --debugger
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'get_shorty'
|
4
|
+
require 'spec'
|
5
|
+
require 'spec/autorun'
|
6
|
+
require 'rubygems'
|
7
|
+
require 'active_record'
|
8
|
+
require "rest_client"
|
9
|
+
require 'bitly/spec_helper'
|
10
|
+
require 'active_record_helper'
|
11
|
+
|
12
|
+
Spec::Runner.configure do |config|
|
13
|
+
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: get_shorty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Louis Gillies
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-12 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 13
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
- 9
|
34
|
+
version: 1.2.9
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rest-client
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 4
|
49
|
+
- 2
|
50
|
+
version: 1.4.2
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: nokogiri
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 1
|
64
|
+
- 4
|
65
|
+
- 2
|
66
|
+
version: 1.4.2
|
67
|
+
type: :runtime
|
68
|
+
version_requirements: *id003
|
69
|
+
description: Shorten the url of a resource - at present supports active record models and caches the short url to a database column.
|
70
|
+
email: louisgillies@yahoo.co.uk
|
71
|
+
executables: []
|
72
|
+
|
73
|
+
extensions: []
|
74
|
+
|
75
|
+
extra_rdoc_files:
|
76
|
+
- LICENSE
|
77
|
+
- README.md
|
78
|
+
files:
|
79
|
+
- .gitignore
|
80
|
+
- LICENSE
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- VERSION
|
84
|
+
- get_shorty.gemspec
|
85
|
+
- get_shorty.rb
|
86
|
+
- init.rb
|
87
|
+
- lib/get_shorty.rb
|
88
|
+
- lib/providers/bitly.rb
|
89
|
+
- lib/providers/bitly/client.rb
|
90
|
+
- lib/providers/bitly/config.rb
|
91
|
+
- lib/providers/bitly/response.rb
|
92
|
+
- lib/providers/init.rb
|
93
|
+
- spec/active_record_helper.rb
|
94
|
+
- spec/bitly/client_spec.rb
|
95
|
+
- spec/bitly/config_spec.rb
|
96
|
+
- spec/bitly/response_spec.rb
|
97
|
+
- spec/bitly/spec_helper.rb
|
98
|
+
- spec/get_shorty_spec.rb
|
99
|
+
- spec/spec.opts
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
has_rdoc: true
|
102
|
+
homepage: http://github.com/playgood/get_shorty
|
103
|
+
licenses: []
|
104
|
+
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options:
|
107
|
+
- --charset=UTF-8
|
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
|
+
hash: 3
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
version: "0"
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
hash: 3
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
version: "0"
|
128
|
+
requirements: []
|
129
|
+
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 1.3.7
|
132
|
+
signing_key:
|
133
|
+
specification_version: 3
|
134
|
+
summary: Shorten resource urls
|
135
|
+
test_files:
|
136
|
+
- spec/active_record_helper.rb
|
137
|
+
- spec/bitly/client_spec.rb
|
138
|
+
- spec/bitly/config_spec.rb
|
139
|
+
- spec/bitly/response_spec.rb
|
140
|
+
- spec/bitly/spec_helper.rb
|
141
|
+
- spec/get_shorty_spec.rb
|
142
|
+
- spec/spec_helper.rb
|