svenaas-trim 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +44 -0
- data/Rakefile +40 -0
- data/lib/trim.rb +29 -0
- data/lib/trim/version.rb +13 -0
- data/test/test_helper.rb +8 -0
- data/test/unit/trim_test.rb +32 -0
- metadata +61 -0
data/README.rdoc
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
= Trim
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
The trim gem is a wrapper for the tr.im URL shortening service,
|
6
|
+
which can be found at http://tr.im/
|
7
|
+
|
8
|
+
This software is not affiliated with tr.im in any way. tr.im is
|
9
|
+
a product of The Nambu Network (http://www.nambu.com/).
|
10
|
+
|
11
|
+
== Installation
|
12
|
+
|
13
|
+
sudo gem install trim
|
14
|
+
|
15
|
+
== Usage
|
16
|
+
|
17
|
+
require 'trim'
|
18
|
+
|
19
|
+
Trim.trim('http://www.google.com') # Returns trimmed URL, or nil if any error occurs
|
20
|
+
|
21
|
+
== License
|
22
|
+
|
23
|
+
Copyright (c) 2009 Sven Aas (mailto:sven.aas@gmail.com)
|
24
|
+
|
25
|
+
Permission is hereby granted, free of charge, to any person
|
26
|
+
obtaining a copy of this software and associated documentation
|
27
|
+
files (the "Software"), to deal in the Software without
|
28
|
+
restriction, including without limitation the rights to use,
|
29
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
30
|
+
copies of the Software, and to permit persons to whom the
|
31
|
+
Software is furnished to do so, subject to the following
|
32
|
+
conditions:
|
33
|
+
|
34
|
+
The above copyright notice and this permission notice shall be
|
35
|
+
included in all copies or substantial portions of the Software.
|
36
|
+
|
37
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
38
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
39
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
40
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
41
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
42
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
43
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
44
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
require 'lib/trim/version'
|
6
|
+
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
spec = Gem::Specification.new do |s|
|
10
|
+
s.name = 'trim'
|
11
|
+
s.version = Trim::Version.to_s
|
12
|
+
s.has_rdoc = true
|
13
|
+
s.extra_rdoc_files = %w(README.rdoc)
|
14
|
+
s.rdoc_options = %w(--main README.rdoc)
|
15
|
+
s.summary = "Gem interface to the URL shortening service tr.im (http://tr.im/)"
|
16
|
+
s.author = 'Sven Aas'
|
17
|
+
s.email = 'sven.aas@gmail.com'
|
18
|
+
s.homepage = 'http://github.com/svenaas/trim/'
|
19
|
+
s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib,test}/**/*")
|
20
|
+
# s.executables = ['trim']
|
21
|
+
|
22
|
+
# s.add_dependency('gem_name', '~> 0.0.1')
|
23
|
+
end
|
24
|
+
|
25
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
26
|
+
pkg.gem_spec = spec
|
27
|
+
end
|
28
|
+
|
29
|
+
Rake::TestTask.new do |t|
|
30
|
+
t.libs << 'test'
|
31
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
32
|
+
t.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
desc 'Generate the gemspec to serve this Gem from Github'
|
36
|
+
task :github do
|
37
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
38
|
+
File.open(file, 'w') {|f| f << spec.to_ruby }
|
39
|
+
puts "Created gemspec: #{file}"
|
40
|
+
end
|
data/lib/trim.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
|
3
|
+
require 'trim/version.rb'
|
4
|
+
|
5
|
+
require 'open-uri'
|
6
|
+
|
7
|
+
module Trim
|
8
|
+
|
9
|
+
#
|
10
|
+
# Alias for Trim::trim_simple
|
11
|
+
#
|
12
|
+
def Trim::trim(url)
|
13
|
+
response = nil
|
14
|
+
open("http://api.tr.im/api/trim_simple?url=#{url}") {|http| response = http.read }
|
15
|
+
response.strip!.size > 0 ? response : nil
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# Returns a trimmed URL based on +url+, or nil if any error occurs.
|
20
|
+
#
|
21
|
+
# trimmed_url = Trim.trim_simple('http://sample.com/some/long/url')
|
22
|
+
#
|
23
|
+
def Trim::trim_simple(url)
|
24
|
+
response = nil
|
25
|
+
open("http://api.tr.im/api/trim_simple?url=#{url}") {|http| response = http.read }
|
26
|
+
response.strip!.size > 0 ? response : nil
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/lib/trim/version.rb
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
|
6
|
+
class TrimTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
context "A simple trim request to a valid URL" do
|
9
|
+
setup do
|
10
|
+
@long_url = 'http://www.google.com/'
|
11
|
+
end
|
12
|
+
|
13
|
+
should "provide a working shortened URL" do
|
14
|
+
trimmed_url = Trim.trim @long_url
|
15
|
+
response = Net::HTTP.get_response(URI.parse(trimmed_url))
|
16
|
+
assert response.kind_of? Net::HTTPRedirection
|
17
|
+
assert_equal @long_url, response['location']
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "A simple trim request to an invalid URL" do
|
22
|
+
setup do
|
23
|
+
@long_url = 'www'
|
24
|
+
end
|
25
|
+
|
26
|
+
should "return nil" do
|
27
|
+
trimmed_url = Trim.trim @long_url
|
28
|
+
assert_nil trimmed_url
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: svenaas-trim
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sven Aas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-01 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: sven.aas@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- Rakefile
|
27
|
+
- lib/trim
|
28
|
+
- lib/trim/version.rb
|
29
|
+
- lib/trim.rb
|
30
|
+
- test/test_helper.rb
|
31
|
+
- test/unit
|
32
|
+
- test/unit/trim_test.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/svenaas/trim/
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --main
|
38
|
+
- README.rdoc
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.2.0
|
57
|
+
signing_key:
|
58
|
+
specification_version: 2
|
59
|
+
summary: Gem interface to the URL shortening service tr.im (http://tr.im/)
|
60
|
+
test_files: []
|
61
|
+
|