georgedrummond_sinatra_helpers 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  require "georgedrummond_sinatra_helpers/version"
2
2
  require "digest/md5"
3
+ require "uri"
3
4
 
4
5
  module GeorgeDrummond
5
6
  module Sinatra
@@ -8,17 +9,24 @@ module GeorgeDrummond
8
9
  #
9
10
  # ==== Examples
10
11
  #
11
- # With only an email address provided
12
+ # With only an email address provided:
12
13
  #
13
14
  # mail_to("georgedrummond@gmail.com")
14
15
  # # => <a href="mailto:georgedrummond@gmail.com" class="mailto">georgedrummond@gmail.com</a>
15
16
  #
16
- # With an optional text field provided
17
+ # With an optional text field provided:
17
18
  #
18
19
  # mail_to("georgedrummond@gmail.com", "George Drummond")
19
20
  # # => <a href="mailto:georgedrummond@gmail.com" class="mailto">George Drummond</a>
20
- def mail_to(email, text=email)
21
- "<a href=\"mailto:#{email}\" class=\"mailto\">#{text}</a>"
21
+ #
22
+ # With an options hash:
23
+ #
24
+ # mail_to("georgedrummond@gmail.com", "George Drummond", :class => :email, :title => "How do you do")
25
+ # # => <a href="mailto:georgedrummond@gmail.com" class="email" title="How do you do">George Drummond</a>
26
+ #
27
+ def mail_to(email, text=email, opts={})
28
+ options = {:class => :mailto}.merge(opts)
29
+ link_to text, "mailto:#{email}", options, false
22
30
  end
23
31
 
24
32
  # Creates the gravatar (www.gravatar.com) html from the email address and arguments provided.
@@ -26,93 +34,170 @@ module GeorgeDrummond
26
34
  #
27
35
  # ==== Examples
28
36
  #
29
- # With no size specified
37
+ # With no size specified:
30
38
  #
31
39
  # gravatar_image("georgedrummond@gmail.com")
32
40
  # # => <img src="http://www.gravatar.com/avatar/d278a12b969a495ab16fdd942e748fe5?s=50" class="gravatar" />
33
41
  #
34
- # With a size specified
42
+ # With a size specified:
35
43
  #
36
44
  # gravatar_image("georgedrummond@gmail.com", 150)
37
45
  # # => <img src="http://www.gravatar.com/avatar/d278a12b969a495ab16fdd942e748fe5?s=150" class="gravatar" />
38
- def gravatar_image(email, size=50)
46
+ #
47
+ # With options:
48
+ #
49
+ # gravatar_image("georgedrummond@gmail.com", 150, :class => :icon)
50
+ # # => <img src="http://www.gravatar.com/avatar/d278a12b969a495ab16fdd942e748fe5?s=150" class="icon" />
51
+ #
52
+ def gravatar_image(email, size=50, opts={})
39
53
  hash = Digest::MD5.hexdigest(email)
40
- "<img src=\"http://www.gravatar.com/avatar/#{hash}?s=#{size}\" class=\"gravatar\" />"
54
+ url = "http://www.gravatar.com/avatar/#{hash}?s=#{size}"
55
+ image_tag url, {:class => :gravatar}.merge(opts)
41
56
  end
42
57
 
43
58
  # Creates html tags for stylesheets from the arguements provided. Dont include the <tt>.css</tt> extension as this
44
59
  # will be automatically appended. You can specify one or multiple css files at once. Place your css files in
45
- # the <tt>public/css</tt> folder of your app.
60
+ # the <tt>public/css</tt> folder of your app. URLs will link to external files
46
61
  #
47
62
  # ==== Examples
48
63
  #
49
- # With only one CSS file
64
+ # With only one CSS file:
50
65
  #
51
66
  # stylesheet_link_tag :app
52
67
  # # => <link href="/css/app.css" type="text/css" rel="stylesheet" />
53
68
  #
54
- # With an array of CSS files we want to show
69
+ # With an array of CSS files we want to show:
55
70
  #
56
71
  # stylesheet_link_tag :app, :header # =>
57
72
  # <link href="/css/app.css" type="text/css" rel="stylesheet" />
58
73
  # <link href="/css/header.css" type="text/css" rel="stylesheet" />
74
+ #
75
+ # With an external stylesheet:
76
+ #
77
+ # stylesheet_link_tag :app, "http://accountsapp.com/styles/common.css"
78
+ # <link href="/css/app.css" type="text/css" rel="stylesheet" />
79
+ # <link href="http://accountsapp.com/styles/common.css" type="text/css" rel="stylesheet" />
80
+ #
59
81
  def stylesheet_link_tag(*sources)
60
82
  html = []
61
83
  sources.each do |stylesheet|
62
- path = "/css/#{stylesheet}.css"
63
- html << "<link href=\"#{ url(path) }\" type=\"text/css\" rel=\"stylesheet\" />"
84
+ if is_uri?(stylesheet)
85
+ path = stylesheet
86
+ else
87
+ path = url("/css/#{stylesheet}.css")
88
+ end
89
+ html << "<link href=\"#{path}\" type=\"text/css\" rel=\"stylesheet\" />"
64
90
  end
65
91
  return html.join("\n")
66
92
  end
67
93
 
68
94
  # Creates html tags for javascript includes from the arguments provided. Dont include the <tt>.js</tt> extension as this
69
95
  # will be automatically appended. You can specify one or multiple javascript files at once. Place your javascript files in
70
- # the <tt>public/js</tt> folder of your app.
96
+ # the <tt>public/js</tt> folder of your app. URLs will link to external files
71
97
  #
72
98
  # ==== Examples
73
99
  #
74
100
  # With only one javascript file
75
101
  #
76
- # javascript_include_tag :app
77
- # # => <script type="text/javascript" src="/js/app.js"></script>
102
+ # javascript_include_tag :app
103
+ # # => <script type="text/javascript" src="/js/app.js"></script>
104
+ #
105
+ # With an array of javascript files we want to show:
106
+ #
107
+ # javascript_include_tag :jquery, :app # =>
108
+ # <script type="text/javascript" src="/js/jquery.js"></script>
109
+ # <script type="text/javascript" src="/js/app.js"></script>
78
110
  #
79
- # With an array of javascript files we want to show
111
+ # With an external stylesheet:
80
112
  #
81
- # javascript_include_tag :jquery, :app # =>
82
- # <script type="text/javascript" src="/js/jquery.js"></script>
83
- # <script type="text/javascript" src="/js/app.js"></script>
113
+ # javascript_include_tag :jquery, "http://accountsapp.com/js/app.js"
114
+ # <script type="text/javascript" src="/js/jquery.js"></script>
115
+ # <script type="text/javascript" src="http://accountsapp.com/js/app.js"></script>
116
+ #
84
117
  def javascript_include_tag(*sources)
85
118
  html = []
86
119
  sources.each do |javascript|
87
- path = "/js/#{javascript}.js"
88
- html << "<script type=\"text/javascript\" src=\"#{ url(path) }\"></script>"
120
+ if is_uri?(javascript)
121
+ path = javascript
122
+ else
123
+ path = url("/js/#{javascript}.js")
124
+ end
125
+ html << "<script type=\"text/javascript\" src=\"#{path}\"></script>"
89
126
  end
90
127
  return html.join("\n")
91
128
  end
92
129
 
93
- # Creates a html image tag given a file name. File should be located in <tt>public/images</tt>. Will not work with external
94
- # files for now however I may add this at a later date if it is needed.
130
+ # Creates a html image tag given a file name. File should be located in <tt>public/images</tt>.
95
131
  #
96
132
  # ==== Examples
97
133
  #
98
- # image_tag "home.png"
99
- # # => <img src="/images/home.png" />
100
- def image_tag(image)
101
- path = "/images/#{image}"
102
- return "<img src=\"#{url(path)}\" />\n"
134
+ # With a local image:
135
+ #
136
+ # image_tag "home.png"
137
+ # # => <img src="/images/home.png" />
138
+ #
139
+ # With options:
140
+ #
141
+ # image_tag "home.png", :title => "home", :class => :icon
142
+ # # => <img src="/images/home.png" title="home" class="icon" />
143
+ #
144
+ # With a remote image:
145
+ #
146
+ # image_tag "http://accountsapp.com/logo.png", :title => "Online Accounting Software"
147
+ # # => <img src="http://accountsapp.com/logo.png" title="Online Accounting Software" />
148
+ #
149
+ def image_tag(image, opts={})
150
+ if is_uri?(image)
151
+ path = image
152
+ else
153
+ path = url("/images/#{image}")
154
+ end
155
+ return "<img src=\"#{path}\"#{parse_options(opts)} />"
103
156
  end
104
157
 
105
- # Creates a html link tag given a page <tt>title</tt> and resource <tt>path</tt>. Works similar to the Rails link_to method
106
- # however does not accept any other options.
158
+ # Creates a html link tag given a page <tt>title</tt> and resource <tt>path</tt>.
107
159
  #
108
160
  # ==== Examples
109
161
  #
110
- # link_to "Home", "/"
111
- # # => <a href="/">Home</a>
112
- def link_to(title, path)
113
- return "<a href=\"#{url(path)}\">#{title}</a>"
162
+ # With an internal link
163
+ #
164
+ # link_to "Home", "/home"
165
+ # # => <a href="/home">Home</a>
166
+ #
167
+ # With options:
168
+ #
169
+ # link_to "Home", "/home", :title => "Home Page"
170
+ # # => <a href="/home" title="Home Page">Home</a>
171
+ #
172
+ # For an external URL:
173
+ #
174
+ # link_to "AccountsApp", "http://accountsapp.com", :title => "Online Invoicing Software"
175
+ # # => <a href="http://accountsapp.com" title="Online Invoicing Software">AccountsApp</a>
176
+ #
177
+ def link_to(title, path, opts={}, base=true)
178
+ unless is_uri?(path) || base == false
179
+ path = url(path)
180
+ end
181
+
182
+ return "<a href=\"#{path}\"#{parse_options(opts)}>#{title}</a>"
114
183
  end
115
-
184
+
185
+ private
186
+ def parse_options(opts)
187
+ unless opts.empty?
188
+ html = [""]
189
+ opts.each do |key, value|
190
+ final_value = value.is_a?(Array) ? value.join(" ") : value
191
+ html << "#{key}=\"#{final_value}\""
192
+ #return attrs.join
193
+ end
194
+ return html.join(" ")
195
+ end
196
+ end
197
+
198
+ def is_uri?(path)
199
+ URI.parse(path).class == URI::HTTP
200
+ end
116
201
  end
117
202
  end
118
203
  end
@@ -1,7 +1,7 @@
1
1
  module GeorgeDrummond
2
2
  module Sinatra
3
3
  module Helpers
4
- VERSION = "0.0.7"
4
+ VERSION = "0.0.8"
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,94 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'georgedrummond_sinatra_helpers.rb')
2
+
3
+ include GeorgeDrummond::Sinatra::Helpers
4
+
5
+ def url(url)
6
+ return File.join("/path", url)
7
+ end
8
+
9
+ describe GeorgeDrummond::Sinatra::Helpers do
10
+
11
+ describe "mail_to" do
12
+ it "with only an email address provided" do
13
+ mail_to("georgedrummond@gmail.com").should == "<a href=\"mailto:georgedrummond@gmail.com\" class=\"mailto\">georgedrummond@gmail.com</a>"
14
+ end
15
+
16
+ it "with an optional text field provided" do
17
+ mail_to("georgedrummond@gmail.com", "George Drummond").should == "<a href=\"mailto:georgedrummond@gmail.com\" class=\"mailto\">George Drummond</a>"
18
+ end
19
+
20
+ it "wth an options hash" do
21
+ mail_to("georgedrummond@gmail.com", "George Drummond", :class => :email, :title => "How do you do").should == "<a href=\"mailto:georgedrummond@gmail.com\" class=\"email\" title=\"How do you do\">George Drummond</a>"
22
+ end
23
+ end
24
+
25
+ describe "gravatar_image" do
26
+ it "with no size specified" do
27
+ gravatar_image("georgedrummond@gmail.com").should == "<img src=\"http://www.gravatar.com/avatar/d278a12b969a495ab16fdd942e748fe5?s=50\" class=\"gravatar\" />"
28
+ end
29
+
30
+ it "with a size specified" do
31
+ gravatar_image("georgedrummond@gmail.com", 150).should == "<img src=\"http://www.gravatar.com/avatar/d278a12b969a495ab16fdd942e748fe5?s=150\" class=\"gravatar\" />"
32
+ end
33
+
34
+ it "with options" do
35
+ gravatar_image("georgedrummond@gmail.com", 150, :class => :icon).should == "<img src=\"http://www.gravatar.com/avatar/d278a12b969a495ab16fdd942e748fe5?s=150\" class=\"icon\" />"
36
+ end
37
+ end
38
+
39
+
40
+ describe "link_to" do
41
+ it "with an internal link" do
42
+ link_to("Home", "/home").should == '<a href="/path/home">Home</a>'
43
+ end
44
+
45
+ it "with options" do
46
+ link_to("Home", "/home", :title => "Home Page").should == '<a href="/path/home" title="Home Page">Home</a>'
47
+ end
48
+
49
+ it "for an external URL" do
50
+ link_to("AccountsApp", "http://accountsapp.com", :title => "Online Invoicing Software").should == '<a href="http://accountsapp.com" title="Online Invoicing Software">AccountsApp</a>'
51
+ end
52
+ end
53
+
54
+ describe "image_tag" do
55
+ it "with a local image" do
56
+ image_tag("home.png").should == '<img src="/path/images/home.png" />'
57
+ end
58
+
59
+ it "with options" do
60
+ image_tag("home.png", :title => "home", :class => :icon).should == '<img src="/path/images/home.png" title="home" class="icon" />'
61
+ end
62
+
63
+ it "with an external image" do
64
+ image_tag("http://accountsapp.com/logo.png", :title => "Online Accounting Software").should == '<img src="http://accountsapp.com/logo.png" title="Online Accounting Software" />'
65
+ end
66
+ end
67
+
68
+ describe "stylesheet_link_tag" do
69
+ it "with a single stylesheet" do
70
+ stylesheet_link_tag(:app).should == "<link href=\"/path/css/app.css\" type=\"text/css\" rel=\"stylesheet\" />"
71
+ end
72
+
73
+ it "with an array of stylesheets" do
74
+ stylesheet_link_tag(:app, :header).should == "<link href=\"/path/css/app.css\" type=\"text/css\" rel=\"stylesheet\" />\n<link href=\"/path/css/header.css\" type=\"text/css\" rel=\"stylesheet\" />"
75
+ end
76
+
77
+ it "with an external stylesheet" do
78
+ stylesheet_link_tag(:app, "http://accountsapp.com/styles/common.css").should == "<link href=\"/path/css/app.css\" type=\"text/css\" rel=\"stylesheet\" />\n<link href=\"http://accountsapp.com/styles/common.css\" type=\"text/css\" rel=\"stylesheet\" />"
79
+ end
80
+ end
81
+
82
+ describe "javascript_include_tag" do
83
+ it "with a single js file" do
84
+ javascript_include_tag(:app).should == "<script type=\"text/javascript\" src=\"/path/js/app.js\"></script>"
85
+ end
86
+
87
+ it "with an array of js files" do
88
+ javascript_include_tag(:jquery, :app).should == "<script type=\"text/javascript\" src=\"/path/js/jquery.js\"></script>\n<script type=\"text/javascript\" src=\"/path/js/app.js\"></script>"
89
+ end
90
+ it "with an external js file" do
91
+ javascript_include_tag(:jquery, "http://accountsapp.com/js/app.js").should == "<script type=\"text/javascript\" src=\"/path/js/jquery.js\"></script>\n<script type=\"text/javascript\" src=\"http://accountsapp.com/js/app.js\"></script>"
92
+ end
93
+ end
94
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: georgedrummond_sinatra_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-21 00:00:00.000000000Z
12
+ date: 2011-10-30 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: This is a collection of common helper methods that I use within my Sinatra
15
15
  apps. They have a rails like feel and they mainly fill in for helpers you would
@@ -31,6 +31,7 @@ files:
31
31
  - georgedrummond_sinatra_helpers.gemspec
32
32
  - lib/georgedrummond_sinatra_helpers.rb
33
33
  - lib/georgedrummond_sinatra_helpers/version.rb
34
+ - spec/sinatra_helpers_spec.rb
34
35
  homepage: https://github.com/georgedrummond/georgedrummond_sinatra_helpers
35
36
  licenses: []
36
37
  post_install_message:
@@ -64,4 +65,5 @@ summary: This is a collection of common helper methods that I use within my Sina
64
65
  apps. They have a rails like feel and they mainly fill in for helpers you would
65
66
  find in Rails. They do not behave exactly like their rails counterparts so please
66
67
  be careful to read the RDOC.
67
- test_files: []
68
+ test_files:
69
+ - spec/sinatra_helpers_spec.rb