embiggen 0.1.0 → 0.1.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 +4 -4
- data/README.md +135 -1
- data/lib/embiggen/uri.rb +1 -1
- data/spec/embiggen/uri_spec.rb +14 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47bfb5ace689946c57eb0595e0aa5c9992a6fe70
|
4
|
+
data.tar.gz: 7a1d1441cf49e8b785c9c63ed9f07899d08d0283
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac5abe95ea7a88f6e93d8815b91c963362fe671fbb1334d4b8e104d06bac61cf846ce2fb7ee9bdf6a73a8d9b33e1c26a417b63ef4d6d228097aa9ee8136aeb64
|
7
|
+
data.tar.gz: 40a02c961adffd16f0f06b4d0851817ba8fc9f21ed08f3383831118e2ee1a101a91fd574e9b60751e99452b9000be6fd2ac81b3eeacb2d4d34108f97c2f444a7
|
data/README.md
CHANGED
@@ -2,7 +2,23 @@
|
|
2
2
|
|
3
3
|
A Ruby library to expand shortened URLs.
|
4
4
|
|
5
|
+
**Current version:** 0.1.1
|
6
|
+
**Supported Ruby versions:** 1.8.7, 1.9.2, 1.9.3, 2.0, 2.1, 2.2
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
```
|
11
|
+
gem install embiggen -v '~> 0.1'
|
12
|
+
```
|
13
|
+
|
14
|
+
Or, in your `Gemfile`:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'embiggen', '~> 0.1'
|
18
|
+
```
|
19
|
+
|
5
20
|
## Usage
|
21
|
+
|
6
22
|
```ruby
|
7
23
|
require 'embiggen'
|
8
24
|
|
@@ -45,9 +61,125 @@ Embiggen.configure do |config|
|
|
45
61
|
end
|
46
62
|
```
|
47
63
|
|
64
|
+
## Shorteners
|
65
|
+
|
66
|
+
Embiggen ships with a default list of URL shortening service domains (c.f.
|
67
|
+
[Acknowledgements](#acknowledgements)) but as it is likely to be outdated and
|
68
|
+
incomplete, you are strongly encouraged to supply your own via
|
69
|
+
`Embiggen.configure`:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
Embiggen.configure do |config|
|
73
|
+
config.shorteners = %w(myshorten.er anoth.er)
|
74
|
+
# or load from a file...
|
75
|
+
config.shorteners = File.readlines('shorteners.txt').map(&:chomp)
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
## API Documentation
|
80
|
+
|
81
|
+
### `Embiggen::URI`
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
uri = Embiggen::URI('https://youtu.be/dQw4w9WgXcQ')
|
85
|
+
uri = Embiggen::URI(URI('https://youtu.be/dQw4w9WgXcQ'))
|
86
|
+
```
|
87
|
+
|
88
|
+
Return a new `Embiggen::URI` instance which can be expanded and asked whether
|
89
|
+
it is shortened or not.
|
90
|
+
|
91
|
+
Takes instances of [Ruby's
|
92
|
+
`URI`][URI] or
|
93
|
+
anything with a string representation (through `to_s`) that can be parsed as a
|
94
|
+
valid URI.
|
95
|
+
|
96
|
+
### `Embiggen::URI#expand`
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
Embiggen::URI('https://youtu.be/dQw4w9WgXcQ').expand
|
100
|
+
#=> #<URI:HTTPS https://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtu.be>
|
101
|
+
|
102
|
+
Embiggen::URI('http://www.altmetric.com/').expand
|
103
|
+
#=> #<URI::HTTP http://www.altmetric.com/>
|
104
|
+
|
105
|
+
Embiggen::URI('https://youtu.be/dQw4w9WgXcQ').expand(:timeout => 5)
|
106
|
+
Embiggen::URI('https://youtu.be/dQw4w9WgXcQ').expand(:redirects => 2)
|
107
|
+
```
|
108
|
+
|
109
|
+
Expand the given URI, returning the full version as a [`URI`][URI] if it is
|
110
|
+
shortened or the original if it cannot be expanded. Will not raise any
|
111
|
+
exceptions thrown during expansion (e.g. timeouts, network errors, invalid
|
112
|
+
return URIs); see `expand!` for an alternative.
|
113
|
+
|
114
|
+
Takes an optional hash of options for expansion:
|
115
|
+
|
116
|
+
* `:timeout`: overrides the default timeout for following redirects;
|
117
|
+
* `:redirects`: overrides the default number of redirects to follow.
|
118
|
+
|
119
|
+
Uses a whitelist of shortening domains (as configured through
|
120
|
+
`Embiggen.configure`) to determine whether a URI is shortened or not. Be sure
|
121
|
+
to [configure this to suit your needs](#shorteners).
|
122
|
+
|
123
|
+
### `Embiggen::URI#expand!`
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
Embiggen::URI('https://youtu.be/dQw4w9WgXcQ').expand!
|
127
|
+
#=> #<URI:HTTPS https://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtu.be>
|
128
|
+
|
129
|
+
Embiggen::URI('http://bit.ly/some-bad-link').expand!
|
130
|
+
# TooManyRedirects: http://bit.ly/some-bad-link redirected too many times
|
131
|
+
```
|
132
|
+
|
133
|
+
Expand the given URI as with `Embiggen::URI#expand` but don't suppress any
|
134
|
+
exceptions raised during expansion (including timeouts, network errors,
|
135
|
+
invalid return URIs, too many redirects or no redirects whatsoever).
|
136
|
+
|
137
|
+
Takes the same options as `Embiggen::URI#expand`.
|
138
|
+
|
139
|
+
Two Embiggen-specific errors (both inheriting from `Embiggen::Error`) can be
|
140
|
+
raised:
|
141
|
+
|
142
|
+
* `Embiggen::TooManyRedirects`: when a URI redirects more than the configured
|
143
|
+
number of times;
|
144
|
+
* `Embiggen::BadShortenedURI`: when a URI appears to be shortened but
|
145
|
+
following it does not result in a redirect.
|
146
|
+
|
147
|
+
### `Embiggen::URI#shortened?`
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
Embiggen::URI('https://youtu.be/dQw4w9WgXcQ').shortened?
|
151
|
+
#=> true
|
152
|
+
|
153
|
+
Embiggen::URI('http://www.altmetric.com').shortened?
|
154
|
+
#=> false
|
155
|
+
```
|
156
|
+
|
157
|
+
Return true if the URI appears to be shortened. Uses the configured whitelist
|
158
|
+
of shorteners, c.f. [Shorteners](#shorteners).
|
159
|
+
|
160
|
+
### `Embiggen.configure`
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
Embiggen.configure do |config|
|
164
|
+
config.timeout = 5
|
165
|
+
config.redirects = 2
|
166
|
+
config.shorteners += %w(myshorten.er anoth.er)
|
167
|
+
end
|
168
|
+
```
|
169
|
+
|
170
|
+
Override the following settings:
|
171
|
+
|
172
|
+
* `timeout`: the default timeout for following any redirects (can be
|
173
|
+
overridden by passing options to `Embiggen::URI#expand` or
|
174
|
+
`Embiggen::URI#expand!`);
|
175
|
+
* `redirects`: the default number of redirects to follow (can be overridden by
|
176
|
+
passing options to `Embiggen::URI#expand` or `Embiggen::URI#expand!`);
|
177
|
+
* `shorteners`: the list of domains of shortening services, c.f.
|
178
|
+
[Shorteners](#shorteners).
|
179
|
+
|
48
180
|
## Acknowledgements
|
49
181
|
|
50
|
-
* The list of shorteners comes from [LongURL.org's curated
|
182
|
+
* The default list of shorteners comes from [LongURL.org's curated
|
51
183
|
list](http://longurl.org/services).
|
52
184
|
|
53
185
|
## License
|
@@ -55,3 +187,5 @@ end
|
|
55
187
|
Copyright © 2015 Altmetric LLP
|
56
188
|
|
57
189
|
Distributed under the MIT License.
|
190
|
+
|
191
|
+
[URI]: http://ruby-doc.org/stdlib/libdoc/uri/rdoc/URI.html
|
data/lib/embiggen/uri.rb
CHANGED
data/spec/embiggen/uri_spec.rb
CHANGED
@@ -63,6 +63,20 @@ module Embiggen
|
|
63
63
|
expect(uri.expand).to eq(URI('http://bit.ly/bad'))
|
64
64
|
end
|
65
65
|
|
66
|
+
it 'does not expand URIs whose host is unreachable' do
|
67
|
+
stub_request(:head, 'http://bit.ly/bad').to_raise(Errno::EHOSTUNREACH)
|
68
|
+
uri = described_class.new(URI('http://bit.ly/bad'))
|
69
|
+
|
70
|
+
expect(uri.expand).to eq(URI('http://bit.ly/bad'))
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'does not expand URIs whose name or service is not known' do
|
74
|
+
stub_request(:head, 'http://bit.ly/bad').to_raise(SocketError)
|
75
|
+
uri = described_class.new(URI('http://bit.ly/bad'))
|
76
|
+
|
77
|
+
expect(uri.expand).to eq(URI('http://bit.ly/bad'))
|
78
|
+
end
|
79
|
+
|
66
80
|
it 'takes an optional timeout' do
|
67
81
|
stub_request(:head, 'http://bit.ly/bad').to_timeout
|
68
82
|
uri = described_class.new(URI('http://bit.ly/bad'))
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: embiggen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Mucur
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|