httpify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.DS_Store +0 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +35 -0
- data/httpify.gemspec +24 -0
- data/lib/.DS_Store +0 -0
- data/lib/httpify/.DS_Store +0 -0
- data/lib/httpify/core_ext/.DS_Store +0 -0
- data/lib/httpify/core_ext/string/httpify.rb +8 -0
- data/lib/httpify/httpifier.rb +27 -0
- data/lib/httpify.rb +47 -0
- data/spec/httpify_spec.rb +119 -0
- data/spec/spec_helper.rb +4 -0
- metadata +83 -0
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
== MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2010, Kieran Johnson.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
Httpify
|
2
|
+
=======
|
3
|
+
|
4
|
+
Usage
|
5
|
+
-----
|
6
|
+
|
7
|
+
Include in your ActiveRecord model and specify any attributes you wish to
|
8
|
+
automatically be prefixed with http://...
|
9
|
+
|
10
|
+
Example
|
11
|
+
-------
|
12
|
+
|
13
|
+
class Link
|
14
|
+
include Httpify
|
15
|
+
attr_accessor :url
|
16
|
+
httpify :url
|
17
|
+
end
|
18
|
+
|
19
|
+
link = Link.new
|
20
|
+
link.url = "google.com"
|
21
|
+
link.url # Returns http://google.com
|
22
|
+
|
23
|
+
The gem also exposes a method on String
|
24
|
+
|
25
|
+
"mydomain.com".httpify
|
26
|
+
|
27
|
+
Options available:
|
28
|
+
|
29
|
+
"mydomain.com".httpify(:https => true, :trailing_slash => true)
|
30
|
+
|
31
|
+
|
32
|
+
Copyright
|
33
|
+
---------
|
34
|
+
|
35
|
+
Copyright (c) 2010 Kieran Johnson. See LICENSE for details.
|
data/httpify.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'httpify'
|
6
|
+
|
7
|
+
Gem::Specification.new do |gem|
|
8
|
+
gem.name = "httpify"
|
9
|
+
gem.version = Httpify::VERSION
|
10
|
+
gem.authors = ["Kieran Johnson"]
|
11
|
+
gem.email = ["kieran@invisiblelines.com"]
|
12
|
+
gem.homepage = "http://github.com/invisiblelines/httpify"
|
13
|
+
gem.summary = "Prefixes any httpified elements with http://"
|
14
|
+
gem.description = "Prefixes any httpified elements with http:// should they be missing this"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_dependency "active_support", ">= 3.0"
|
22
|
+
|
23
|
+
gem.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/.DS_Store
ADDED
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Httpify
|
2
|
+
|
3
|
+
class Httpifier
|
4
|
+
|
5
|
+
def initialize(url, options = {})
|
6
|
+
@url = url
|
7
|
+
@options = options.symbolize_keys!
|
8
|
+
end
|
9
|
+
|
10
|
+
def perform!
|
11
|
+
return @url if @url.blank?
|
12
|
+
|
13
|
+
if !@url.match(/^http[s]?:\/\//)
|
14
|
+
url = "http"
|
15
|
+
url << "s" if @options[:https]
|
16
|
+
url << "://#{@url}"
|
17
|
+
url << "/" if @options[:trailing_slash]
|
18
|
+
|
19
|
+
url
|
20
|
+
else
|
21
|
+
@url
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/lib/httpify.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'active_support/core_ext/hash'
|
2
|
+
|
3
|
+
require 'httpify/httpifier'
|
4
|
+
require 'httpify/core_ext/string/httpify'
|
5
|
+
|
6
|
+
# = Overview:
|
7
|
+
# A small extension to ensure any variable that should return a url
|
8
|
+
# includes the correct http prefix
|
9
|
+
# ---
|
10
|
+
# = Usage:
|
11
|
+
# require 'httpify'
|
12
|
+
#
|
13
|
+
# class Link
|
14
|
+
# include Httpify
|
15
|
+
# httpify :url
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
|
19
|
+
module Httpify
|
20
|
+
|
21
|
+
VERSION = "0.1.0"
|
22
|
+
|
23
|
+
extend ActiveSupport::Concern
|
24
|
+
|
25
|
+
module ClassMethods
|
26
|
+
|
27
|
+
# Formats the variable
|
28
|
+
# <tt>:args</tt>:: The names of the variables to httpify
|
29
|
+
#
|
30
|
+
# Optional parameters passed in as a hash
|
31
|
+
# <tt>:https</tt>:: Boolean - adds https
|
32
|
+
# <tt>:trailing_slash</tt>:: Boolean - adds a trailing slash
|
33
|
+
#
|
34
|
+
def httpify(*args)
|
35
|
+
options = args.extract_options!
|
36
|
+
|
37
|
+
args.each do |arg|
|
38
|
+
define_method arg.to_sym do
|
39
|
+
str = instance_variable_get("@#{arg}")
|
40
|
+
str.httpify(options) unless !str.is_a?(String)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module Httpify
|
4
|
+
|
5
|
+
class Link
|
6
|
+
|
7
|
+
include Httpify
|
8
|
+
|
9
|
+
attr_accessor :url, :source
|
10
|
+
|
11
|
+
httpify :url
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "a String" do
|
16
|
+
|
17
|
+
it "should respond to httpify" do
|
18
|
+
"google.com".should respond_to(:httpify)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return the string prefixed with http://" do
|
22
|
+
"www.google.com".httpify.should == "http://www.google.com"
|
23
|
+
end
|
24
|
+
|
25
|
+
context "an httpified attribute starting http://" do
|
26
|
+
|
27
|
+
it "should not alter the string" do
|
28
|
+
"http://www.google.com".httpify.should == "http://www.google.com"
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
context "an httpified attribute starting https://" do
|
34
|
+
|
35
|
+
it "should not alter the string" do
|
36
|
+
"https://www.google.com".httpify.should == "https://www.google.com"
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
context "options" do
|
42
|
+
|
43
|
+
it "should prefix the string with https" do
|
44
|
+
"www.google.com".httpify(:https => true).should == "https://www.google.com"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should add a trailing slash" do
|
48
|
+
"www.google.com".httpify("trailing_slash" => true).should == "http://www.google.com/"
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "A class with Httpify included" do
|
56
|
+
|
57
|
+
before(:each) do
|
58
|
+
@link = Link.new
|
59
|
+
end
|
60
|
+
|
61
|
+
context "an httpified attribute that is nil" do
|
62
|
+
|
63
|
+
it "should return nil" do
|
64
|
+
@link.url = nil
|
65
|
+
@link.url.should be_nil
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
context "an httpified attribute that is blank" do
|
71
|
+
|
72
|
+
it "should return blank" do
|
73
|
+
@link.url = ""
|
74
|
+
@link.url.should == ""
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
context "a httpified attribute that is a String" do
|
80
|
+
|
81
|
+
it "should return the httpified version" do
|
82
|
+
@link.url = "www.google.com"
|
83
|
+
@link.url.should == "http://www.google.com"
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
context "options" do
|
89
|
+
|
90
|
+
before do
|
91
|
+
Link.httpify :url, :https => true
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should account for options" do
|
95
|
+
@link = Link.new
|
96
|
+
@link.url = "www.google.com"
|
97
|
+
@link.url.should == "https://www.google.com"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context "multiple attributes" do
|
102
|
+
|
103
|
+
before do
|
104
|
+
Link.httpify :url, :source
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should allow multiple attributes to be specified" do
|
108
|
+
@link = Link.new
|
109
|
+
@link.url = "www.google.com"
|
110
|
+
@link.source = "news.ycombinator.com"
|
111
|
+
@link.url.should == "http://www.google.com"
|
112
|
+
@link.source.should == "http://news.ycombinator.com"
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: httpify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kieran Johnson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: active_support
|
16
|
+
requirement: &70362648672500 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70362648672500
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70362648672000 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70362648672000
|
36
|
+
description: Prefixes any httpified elements with http:// should they be missing this
|
37
|
+
email:
|
38
|
+
- kieran@invisiblelines.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .DS_Store
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE
|
47
|
+
- README.md
|
48
|
+
- httpify.gemspec
|
49
|
+
- lib/.DS_Store
|
50
|
+
- lib/httpify.rb
|
51
|
+
- lib/httpify/.DS_Store
|
52
|
+
- lib/httpify/core_ext/.DS_Store
|
53
|
+
- lib/httpify/core_ext/string/httpify.rb
|
54
|
+
- lib/httpify/httpifier.rb
|
55
|
+
- spec/httpify_spec.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
homepage: http://github.com/invisiblelines/httpify
|
58
|
+
licenses: []
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.8.11
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Prefixes any httpified elements with http://
|
81
|
+
test_files:
|
82
|
+
- spec/httpify_spec.rb
|
83
|
+
- spec/spec_helper.rb
|