simpl 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/.rvmrc +7 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +24 -0
- data/Rakefile +10 -0
- data/lib/simpl.rb +24 -0
- data/lib/simpl/url.rb +55 -0
- data/lib/simpl/version.rb +3 -0
- data/simpl.gemspec +28 -0
- data/spec/simpl/url_spec.rb +142 -0
- data/spec/simpl_spec.rb +14 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/support/vcr.rb +10 -0
- metadata +175 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright 2012 Platform Q Limited
|
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,24 @@
|
|
1
|
+
# Simpl is a Googl URL shortener
|
2
|
+
|
3
|
+
The API is simple:
|
4
|
+
|
5
|
+
your_url = "http://example.com/"
|
6
|
+
your_api_key = "1234567890abcedf"
|
7
|
+
Simpl::Url.new(your_url, api_key: your_api_key).shortened
|
8
|
+
# -> http://goo.gl/123
|
9
|
+
|
10
|
+
If you're using Rails, it's probably best to setup an initializer with your key in it:
|
11
|
+
|
12
|
+
# config/initializers/simpl.rb
|
13
|
+
Simpl.api_key = "1234567890abcedf"
|
14
|
+
|
15
|
+
# then use the API like this:
|
16
|
+
Simpl::Url.new(your_url).shortened
|
17
|
+
|
18
|
+
# Testing
|
19
|
+
|
20
|
+
To test the project on your local machine, download it, run `bundle install`, then run `rake`.
|
21
|
+
|
22
|
+
# License
|
23
|
+
|
24
|
+
Please see [LICENSE](https://github.com/PlatformQ/simpl/blob/master/LICENSE)
|
data/Rakefile
ADDED
data/lib/simpl.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "simpl/version"
|
2
|
+
require "simpl/url"
|
3
|
+
|
4
|
+
class Simpl
|
5
|
+
class << self
|
6
|
+
# your API key to access Goo.gl
|
7
|
+
# get your's at: https://code.google.com/apis/console/
|
8
|
+
attr_writer :api_key
|
9
|
+
|
10
|
+
def api_key
|
11
|
+
@api_key || (raise NoApiKeyError, %Q(
|
12
|
+
No Goo.gl API key is present, please set one using:
|
13
|
+
# if you're using Rails put this in config/initializers/googl.rb
|
14
|
+
Googl.api_key = 'YOUR_KEY_HERE'
|
15
|
+
))
|
16
|
+
end
|
17
|
+
|
18
|
+
# how long to wait for Goo.gl response
|
19
|
+
# default: 10
|
20
|
+
attr_accessor :timeout
|
21
|
+
end
|
22
|
+
|
23
|
+
class NoApiKeyError < StandardError; end
|
24
|
+
end
|
data/lib/simpl/url.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'timeout'
|
2
|
+
require 'httparty'
|
3
|
+
|
4
|
+
class Simpl
|
5
|
+
class Url
|
6
|
+
include HTTParty
|
7
|
+
base_uri 'https://www.googleapis.com'
|
8
|
+
|
9
|
+
attr_accessor :url
|
10
|
+
|
11
|
+
# required: url to be shortened
|
12
|
+
def initialize(url, options = {})
|
13
|
+
# sets the URL to be shortened
|
14
|
+
self.url = url
|
15
|
+
# allows an instance-level override for the api key
|
16
|
+
@api_key = options[:api_key]
|
17
|
+
# allows an instance-level override for the timeout
|
18
|
+
@timeout = options[:timeout]
|
19
|
+
end
|
20
|
+
|
21
|
+
# returns: a string respresenting the shortened url
|
22
|
+
# note: may be still the full url should a problem occur
|
23
|
+
def shortened
|
24
|
+
@shortened ||= shorten
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
# processes the shortening of the full url
|
29
|
+
def shorten
|
30
|
+
Timeout::timeout(timeout) do
|
31
|
+
# submit the url to Goo.gl
|
32
|
+
result = self.class.post("/urlshortener/v1/url?key=#{api_key}", {
|
33
|
+
body: { longUrl: url }.to_json,
|
34
|
+
headers: { 'Content-Type' => 'application/json' }
|
35
|
+
})
|
36
|
+
# parse the JSON
|
37
|
+
response = JSON.parse(result.body)
|
38
|
+
# return the response id or the url
|
39
|
+
response['id'] || url
|
40
|
+
end
|
41
|
+
# if a problem occurs
|
42
|
+
rescue Timeout::Error, JSON::ParserError => e
|
43
|
+
# just return the original url
|
44
|
+
url
|
45
|
+
end
|
46
|
+
|
47
|
+
def api_key
|
48
|
+
@api_key || Simpl.api_key
|
49
|
+
end
|
50
|
+
|
51
|
+
def timeout
|
52
|
+
@timeout || Simpl.timeout || 10
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/simpl.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "simpl/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "simpl"
|
7
|
+
s.version = Simpl::VERSION
|
8
|
+
s.authors = ["Ryan Townsend"]
|
9
|
+
s.email = ["ryan@ryantownsend.co.uk"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Shortens URLs using the Goo.gl service}
|
12
|
+
s.description = s.summary
|
13
|
+
|
14
|
+
s.rubyforge_project = "simpl"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_runtime_dependency "httparty"
|
22
|
+
s.add_development_dependency "webmock", "~> 1.8"
|
23
|
+
s.add_development_dependency "rake", "~> 0.9"
|
24
|
+
s.add_development_dependency "rspec", "~> 2.8"
|
25
|
+
s.add_development_dependency "vcr", "~> 2.1"
|
26
|
+
s.add_development_dependency "simplecov", "~> 0.6"
|
27
|
+
s.add_development_dependency "json", "~> 1.6"
|
28
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require "json"
|
2
|
+
require "spec_helper"
|
3
|
+
require_relative "../../lib/simpl"
|
4
|
+
|
5
|
+
describe Simpl::Url do
|
6
|
+
let(:url) { "http://example.com/test-page" }
|
7
|
+
let(:actual_api_key) { "1234567890abcdef" }
|
8
|
+
|
9
|
+
before do
|
10
|
+
Simpl.api_key = actual_api_key
|
11
|
+
Simpl.timeout = 30
|
12
|
+
end
|
13
|
+
|
14
|
+
subject { Simpl::Url.new(url) }
|
15
|
+
|
16
|
+
context "when no custom timeout is set" do
|
17
|
+
let(:timeout) { 123 }
|
18
|
+
|
19
|
+
before(:each) { Simpl.timeout = timeout }
|
20
|
+
|
21
|
+
it "should return the timeout set on the Simpl class" do
|
22
|
+
subject.send(:timeout).should == timeout
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when setting a custom timeout" do
|
27
|
+
let(:timeout) { 654 }
|
28
|
+
|
29
|
+
subject { Simpl::Url.new(url, timeout: timeout) }
|
30
|
+
|
31
|
+
it "should return the custom timeout" do
|
32
|
+
subject.send(:timeout).should == timeout
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "when no timeout is set on the class or instance" do
|
37
|
+
before(:each) { Simpl.timeout = nil }
|
38
|
+
|
39
|
+
it "should return 10 seconds as the default timeout" do
|
40
|
+
subject.send(:timeout).should == 10
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when no custom API key is set" do
|
45
|
+
let(:api_key) { "hello" }
|
46
|
+
|
47
|
+
before(:each) do
|
48
|
+
Simpl.api_key = api_key
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return the API key set on the Simpl class" do
|
52
|
+
subject.send(:api_key).should == api_key
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "when setting a custom API key" do
|
57
|
+
let(:api_key) { "0987654321" }
|
58
|
+
|
59
|
+
subject { Simpl::Url.new(url, api_key: api_key) }
|
60
|
+
|
61
|
+
it "should return the custom API key" do
|
62
|
+
subject.send(:api_key).should == api_key
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "when no API key is set on the class or instance" do
|
67
|
+
before(:each) { Simpl.api_key = nil }
|
68
|
+
|
69
|
+
it "should raise an error" do
|
70
|
+
lambda { subject.send(:api_key) }.should raise_error(Simpl::NoApiKeyError)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "when invalid data is returned from the API" do
|
75
|
+
before(:each) do
|
76
|
+
response = mock("response", body: "<html><head></head><body></body></html>")
|
77
|
+
Simpl::Url.should_receive(:post).and_return(response)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should return the original URL" do
|
81
|
+
subject.shortened.should == url
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "when the API times out" do
|
86
|
+
before(:each) do
|
87
|
+
Simpl::Url.should_receive(:post).and_raise(Timeout::Error)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should return the original URL" do
|
91
|
+
subject.shortened.should == url
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "when the API returns valid JSON with an id" do
|
96
|
+
let(:shortened_url) { "http://goo.gl/123" }
|
97
|
+
|
98
|
+
before(:each) do
|
99
|
+
response = mock("response", body: { id: shortened_url }.to_json)
|
100
|
+
Simpl::Url.should_receive(:post).and_return(response)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should return a valid shortened URL" do
|
104
|
+
subject.shortened.should == shortened_url
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "when the API returns valid JSON without an ID" do
|
109
|
+
before(:each) do
|
110
|
+
response = mock("response", body: {}.to_json)
|
111
|
+
Simpl::Url.should_receive(:post).and_return(response)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should return the original URL" do
|
115
|
+
subject.shortened.should == url
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context "against the real API" do
|
120
|
+
use_vcr_cassette "googl-valid-submission"
|
121
|
+
|
122
|
+
let(:actual_api_key) do
|
123
|
+
api_key_file = File.join(File.dirname(__FILE__), "../../", "API_KEY")
|
124
|
+
unless File.exist?(api_key_file)
|
125
|
+
raise StandardError, %Q(
|
126
|
+
You haven't specified your Googl API key in ./API_KEY,
|
127
|
+
to obtain your's go here:
|
128
|
+
https://code.google.com/apis/console/
|
129
|
+
)
|
130
|
+
end
|
131
|
+
File.read(api_key_file)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should store a valid full URL" do
|
135
|
+
subject.url.should == url
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should shorten the url" do
|
139
|
+
subject.shortened.should =~ /^http\:\/\/goo.gl\/.*$/
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
data/spec/simpl_spec.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require_relative "../lib/simpl"
|
3
|
+
|
4
|
+
describe Simpl do
|
5
|
+
it "should allow specification of an API key" do
|
6
|
+
Simpl.api_key = "123"
|
7
|
+
Simpl.api_key.should == "123"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should allow specification of a timeout" do
|
11
|
+
Simpl.timeout = 11
|
12
|
+
Simpl.timeout.should == 11
|
13
|
+
end
|
14
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/support/vcr.rb
ADDED
metadata
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simpl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryan Townsend
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
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: webmock
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.8'
|
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.8'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0.9'
|
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: '0.9'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.8'
|
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: '2.8'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: vcr
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '2.1'
|
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.1'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: simplecov
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0.6'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0.6'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: json
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.6'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '1.6'
|
126
|
+
description: Shortens URLs using the Goo.gl service
|
127
|
+
email:
|
128
|
+
- ryan@ryantownsend.co.uk
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- .gitignore
|
134
|
+
- .rvmrc
|
135
|
+
- Gemfile
|
136
|
+
- LICENSE
|
137
|
+
- README.md
|
138
|
+
- Rakefile
|
139
|
+
- lib/simpl.rb
|
140
|
+
- lib/simpl/url.rb
|
141
|
+
- lib/simpl/version.rb
|
142
|
+
- simpl.gemspec
|
143
|
+
- spec/simpl/url_spec.rb
|
144
|
+
- spec/simpl_spec.rb
|
145
|
+
- spec/spec_helper.rb
|
146
|
+
- spec/support/vcr.rb
|
147
|
+
homepage: ''
|
148
|
+
licenses: []
|
149
|
+
post_install_message:
|
150
|
+
rdoc_options: []
|
151
|
+
require_paths:
|
152
|
+
- lib
|
153
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
154
|
+
none: false
|
155
|
+
requirements:
|
156
|
+
- - ! '>='
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
+
none: false
|
161
|
+
requirements:
|
162
|
+
- - ! '>='
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
165
|
+
requirements: []
|
166
|
+
rubyforge_project: simpl
|
167
|
+
rubygems_version: 1.8.23
|
168
|
+
signing_key:
|
169
|
+
specification_version: 3
|
170
|
+
summary: Shortens URLs using the Goo.gl service
|
171
|
+
test_files:
|
172
|
+
- spec/simpl/url_spec.rb
|
173
|
+
- spec/simpl_spec.rb
|
174
|
+
- spec/spec_helper.rb
|
175
|
+
- spec/support/vcr.rb
|