formatted_url 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.travis.yml +2 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +18 -0
- data/README.md +13 -0
- data/Rakefile +12 -0
- data/formatted_url.gemspec +14 -0
- data/lib/formatted_url/strategies/default_url_strategy.rb +34 -0
- data/lib/formatted_url/strategies/youtube_url_strategy.rb +28 -0
- data/lib/formatted_url/strategies.rb +25 -0
- data/lib/formatted_url/url_source_resolver.rb +40 -0
- data/lib/formatted_url/version.rb +3 -0
- data/lib/formatted_url.rb +17 -0
- data/test/default_url_strategy_test.rb +13 -0
- data/test/default_url_test.rb +16 -0
- data/test/test_helper.rb +2 -0
- data/test/url_source_resolver_test.rb +16 -0
- data/test/url_strategy_test.rb +11 -0
- data/test/youtube_url_strategy_test.rb +27 -0
- metadata +75 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
FormattedURL
|
2
|
+
=============
|
3
|
+
[![Build Status](https://secure.travis-ci.org/philss/url_formatter.png?branch=master "Build Status")](http://travis-ci.org/philss/url_formatter)
|
4
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/philss/formatted_url)
|
5
|
+
|
6
|
+
The easiest way to get formatted URLs.
|
7
|
+
|
8
|
+
Example:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
FormattedURL.url("http://www.youtube.com/watch?v=RchTlF4aKGs", :embed)
|
12
|
+
# => "http://youtube.com/embed/RchTlF4aKGs"
|
13
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "formatted_url"
|
3
|
+
s.summary = "Formatted URLs."
|
4
|
+
s.files = `git ls-files`.split("\n")
|
5
|
+
s.test_files = `git ls-files -- {test}/*`.split("\n")
|
6
|
+
s.require_path = ['lib']
|
7
|
+
s.version = '0.0.3'
|
8
|
+
s.add_dependency('youtube_id')
|
9
|
+
|
10
|
+
s.author = "Philip Sampaio Silva"
|
11
|
+
s.email = "philip.sampaio@gmail.com"
|
12
|
+
s.required_ruby_version = '>= 1.9.3'
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
module FormattedURL
|
3
|
+
module Strategies
|
4
|
+
# Default strategy to manage
|
5
|
+
# paths. Returns the +path+ by
|
6
|
+
# default
|
7
|
+
class DefaultURLStrategy
|
8
|
+
|
9
|
+
# The URL path
|
10
|
+
attr_reader :path
|
11
|
+
|
12
|
+
# Formats +path+ for a given +format+
|
13
|
+
def self.url(path, format)
|
14
|
+
new(path).send(format)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Receives URL +path+
|
18
|
+
def initialize(path)
|
19
|
+
@path = path
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns +path+ by default
|
23
|
+
def default
|
24
|
+
path
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns default path when
|
28
|
+
# receive unknown message
|
29
|
+
def method_missing(*args)
|
30
|
+
default
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'youtube_id'
|
2
|
+
require File.expand_path('../default_url_strategy', __FILE__)
|
3
|
+
|
4
|
+
module FormattedURL
|
5
|
+
module Strategies
|
6
|
+
# Youtube Strategy to work with embed
|
7
|
+
# and short formats
|
8
|
+
class YoutubeURLStrategy < DefaultURLStrategy
|
9
|
+
|
10
|
+
# Short path from youtube
|
11
|
+
def short
|
12
|
+
"http://youtu.be/#{youtube_id}"
|
13
|
+
end
|
14
|
+
|
15
|
+
# Embed format should be
|
16
|
+
# used only with iframes
|
17
|
+
def embed
|
18
|
+
"http://youtube.com/embed/#{youtube_id}"
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def youtube_id # :nodoc:
|
24
|
+
YoutubeID.from(self.path)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path('../strategies/default_url_strategy', __FILE__)
|
2
|
+
require File.expand_path('../strategies/youtube_url_strategy', __FILE__)
|
3
|
+
|
4
|
+
module FormattedURL
|
5
|
+
# Setting the strategies
|
6
|
+
# TODO: Configurable strategies
|
7
|
+
STRATEGIES = {
|
8
|
+
:youtube => Strategies::YoutubeURLStrategy,
|
9
|
+
:default => Strategies::DefaultURLStrategy
|
10
|
+
}
|
11
|
+
# Returns strategy based on source
|
12
|
+
# which is a Symbol
|
13
|
+
#
|
14
|
+
# Example:
|
15
|
+
#
|
16
|
+
# FormattedURL.strategy(:youtube)
|
17
|
+
# => Strategies::YoutubeURLStrategy
|
18
|
+
def self.strategy(source)
|
19
|
+
STRATEGIES[source]
|
20
|
+
end
|
21
|
+
|
22
|
+
# Namespace for URL Strategies
|
23
|
+
module Strategies
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.expand_path('../strategies', __FILE__)
|
2
|
+
|
3
|
+
module FormattedURL
|
4
|
+
# Find URL source
|
5
|
+
class URLSourceResolver
|
6
|
+
# The URL path
|
7
|
+
attr_accessor :path
|
8
|
+
# Working format
|
9
|
+
attr_reader :format
|
10
|
+
|
11
|
+
# Receives the +path+ for URL
|
12
|
+
# and +format+ to work with
|
13
|
+
def initialize(path, format = :default)
|
14
|
+
@path = path
|
15
|
+
@format = format
|
16
|
+
end
|
17
|
+
|
18
|
+
# Defines url source
|
19
|
+
def source
|
20
|
+
STRATEGIES.each_key do |key|
|
21
|
+
if clean_url.include? key.to_s
|
22
|
+
return key
|
23
|
+
end
|
24
|
+
end
|
25
|
+
return :default
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns the final formatted URL
|
29
|
+
def url
|
30
|
+
STRATEGIES[source].url(path, format)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
# URL without "dot"
|
36
|
+
def clean_url
|
37
|
+
@path.gsub('.','')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path('../formatted_url/url_source_resolver', __FILE__)
|
2
|
+
|
3
|
+
# FormattedURL provides an easy way
|
4
|
+
# to catch URL in differents formats
|
5
|
+
# Working with Youtube URLs, you can get the
|
6
|
+
# short, embed and default format
|
7
|
+
module FormattedURL
|
8
|
+
# Format url acording to it's source
|
9
|
+
#
|
10
|
+
## Examples:
|
11
|
+
#
|
12
|
+
# FormattedURL.url("http://www.youtube.com/watch?v=RchTlF4aKGs", :embed)
|
13
|
+
# => "http://youtube.com/embed/RchTlF4aKGs"
|
14
|
+
def self.url(path, format = :default)
|
15
|
+
URLSourceResolver.new(path, format).url
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DefaultURLStrategyTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@strategy = FormattedURL::Strategies::DefaultURLStrategy
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_default_url_always_return_same_url
|
9
|
+
[:default, :short, :embed].each do |format|
|
10
|
+
assert_equal(@strategy.url('http://google.com',format), 'http://google.com')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DefaultURLTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@base_url = 'http://google.com'
|
6
|
+
@youtube_url = 'http://www.youtube.com/watch?v=bNlNZ2T9EeY'
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_default_url_returns_always_the_same
|
10
|
+
assert_equal(FormattedURL.url(@base_url, :default) ,@base_url)
|
11
|
+
[:short, :embed, :default].each do |format|
|
12
|
+
assert_equal(FormattedURL.url(@base_url, format) ,@base_url)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class URLSourceResolverTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@resolver = FormattedURL::URLSourceResolver.new('http://youtube.com')
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_find_youtube_as_source
|
9
|
+
assert_equal(:youtube, @resolver.source)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_default_source_for_url
|
13
|
+
@resolver.path = "http://google.com"
|
14
|
+
assert_equal(:default, @resolver.source)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class URLStrategyTest < Test::Unit::TestCase
|
4
|
+
def test_youtube_strategy
|
5
|
+
assert_equal(FormattedURL::Strategies::YoutubeURLStrategy, FormattedURL.strategy(:youtube))
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_default_strategy
|
9
|
+
assert_equal(FormattedURL::Strategies::DefaultURLStrategy, FormattedURL.strategy(:default))
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class YoutubeURLStrategyTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@strategy = FormattedURL::Strategies::YoutubeURLStrategy
|
6
|
+
@url = 'http://www.youtube.com/watch?v=bNlNZ2T9EeY'
|
7
|
+
@url2 = 'http://www.youtube.com/watch?v=XXXdDDASDFw'
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_default_url_returns_the_same
|
11
|
+
assert_equal(@url, @strategy.url(@url,:default))
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_short_url
|
15
|
+
assert_equal('http://youtu.be/bNlNZ2T9EeY', @strategy.url(@url, :short))
|
16
|
+
assert_equal('http://youtu.be/XXXdDDASDFw', @strategy.url(@url2, :short))
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_embed_url
|
20
|
+
assert_equal('http://youtube.com/embed/bNlNZ2T9EeY', @strategy.url(@url, :embed))
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_unknown_format_returns_default
|
24
|
+
assert_equal(@url, @strategy.url(@url,:other_format))
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: formatted_url
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Philip Sampaio Silva
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: youtube_id
|
16
|
+
requirement: &15928500 !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: *15928500
|
25
|
+
description:
|
26
|
+
email: philip.sampaio@gmail.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- .travis.yml
|
33
|
+
- Gemfile
|
34
|
+
- Gemfile.lock
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- formatted_url.gemspec
|
38
|
+
- lib/formatted_url.rb
|
39
|
+
- lib/formatted_url/strategies.rb
|
40
|
+
- lib/formatted_url/strategies/default_url_strategy.rb
|
41
|
+
- lib/formatted_url/strategies/youtube_url_strategy.rb
|
42
|
+
- lib/formatted_url/url_source_resolver.rb
|
43
|
+
- lib/formatted_url/version.rb
|
44
|
+
- test/default_url_strategy_test.rb
|
45
|
+
- test/default_url_test.rb
|
46
|
+
- test/test_helper.rb
|
47
|
+
- test/url_source_resolver_test.rb
|
48
|
+
- test/url_strategy_test.rb
|
49
|
+
- test/youtube_url_strategy_test.rb
|
50
|
+
homepage:
|
51
|
+
licenses: []
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- - lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.9.3
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.8.10
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Formatted URLs.
|
74
|
+
test_files: []
|
75
|
+
has_rdoc:
|