Module: Sinatra::Piano

Defined in:
lib/sinatra/piano.rb

Overview

Piano was originally though as a Sinatra extension That’s why the code was defined here

Instance Method Summary collapse

Instance Method Details

#bad_luck(path) ⇒ Object

Fails. Shouts a 404 response and prints hints

If Piano is running in production mode, prints a plain 404 html or, if a 404.haml exists in the :views directory, returns it



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sinatra/piano.rb', line 69

def bad_luck(path)
  content_type :html
  if settings.environment == :production
    if File.exists? "#{pwd}/404.haml"
      @data = data_for "404"
      halt 404, haml(:"404")
    else
      halt 404, "<h1>404 - Not Found</h1><p>Piano has found nothing in this address</p>"
    end
  else
    halt 404, "<h1>You have still to put something here.</h1><p>This is <em>#{path}</em></p><blockquote>Good luck!</blockquote>"
  end
end

#coffee(template) ⇒ Object

Loads and parses a ‘coffee-script` template from the :views directory. Adds an etag if `etags?` is enabled and returns 404 and hints if it can’t find the .coffee file.



36
37
38
39
40
41
42
# File 'lib/sinatra/piano.rb', line 36

def coffee(template)
  file_name = "#{pwd}/#{template}.coffee"
  bad_luck file_name unless File.exists? file_name
  
  etag hash_for(template, :coffee) if etags?
  CoffeeScript.compile(File.read(file_name))
end

#data_for(template) ⇒ Object

Loads and parses the YAML data from the data directory



45
46
47
48
# File 'lib/sinatra/piano.rb', line 45

def data_for(template)
  file_name = "#{settings.data}/#{template}.yaml"
  YAML.load_file(file_name) if File.exists?(file_name)
end

#etags?Boolean

Shorthand to settings.etags == :on

Returns:

  • (Boolean)


126
127
128
129
130
131
132
# File 'lib/sinatra/piano.rb', line 126

def etags?
  if settings.respond_to? :etags
    settings.etags == :on
  else 
    true
  end
end

#extract(text, length = 80) ⇒ Object

Makes an extract out of the given text with the default length of 80 words

If an integer is passed as the second argument, the length of the result is adjusted properly:

extract "Hello World! Too much text is inconvenient", 2

returns

=> "Hello World!..."


101
102
103
104
105
# File 'lib/sinatra/piano.rb', line 101

def extract(text, length = 80)
  words = text.gsub(/<.+?>/, "").split
  return text if words.length <= length
  words[0..(length-1)].join(" ") + "..."
end

#flash(data = nil) ⇒ Object

If an argument is passed, it sets the ‘session` to the passed argument

Otherwise, returns ‘session` and removes `:flash` from the `session` hash to make it available for subsequent requests.



143
144
145
146
147
148
149
150
151
# File 'lib/sinatra/piano.rb', line 143

def flash data = nil
  if data
    session[:flash] = data
  else
    flash_text = session[:flash]
    session.delete :flash
    return flash_text
  end
end

#flash?Boolean

Returns the session if is defined, nil otherwise

Returns:

  • (Boolean)


135
136
137
# File 'lib/sinatra/piano.rb', line 135

def flash?  
  session[:flash]
end

#hash_for(name, type) ⇒ Object

Builds a hash for a file within the :views directory Note: I feel like this functionality should be private



85
86
87
# File 'lib/sinatra/piano.rb', line 85

def hash_for(name, type)
  "#{name}.#{type} - " + File.mtime("#{pwd}/#{name}.#{type}").to_s
end

Returns a url-friendly version of the given link, with a default maximum of 5 words. ‘link` strips any non-letter or special character, downcases the string and replaces whitespace with “-” For example:

link "This is a special text! This won't be shown"

returns

=> "this-is-a-special-text"

You can specify a word length in the second argument.



120
121
122
123
# File 'lib/sinatra/piano.rb', line 120

def link(text, length = 5)
  words = text.gsub(/<.+?>/, "").gsub(" ", "-").downcase.gsub(/[^a-z0-9\-]/, "").split("-")
  words[0..(length-1)].join("-")
end

#pwdObject

Returns the path to the :views directory



61
62
63
# File 'lib/sinatra/piano.rb', line 61

def pwd
  settings.views
end

#sass(template) ⇒ Object

Loads and parses a ‘sass` template from the :views directory. Adds an etag if `etags?` is enabled and returns 404 and hints if it can’t find the .sass file.



24
25
26
27
28
29
30
# File 'lib/sinatra/piano.rb', line 24

def sass(template)
  file_name = "#{pwd}/#{template}.sass"
  bad_luck file_name unless File.exists? file_name

  etag hash_for(template, :sass) if etags?
  Sass.compile File.read(file_name), :syntax => :sass
end

#script(path) ⇒ Object

Sugar: formats a javascript <script> tag with the input



56
57
58
# File 'lib/sinatra/piano.rb', line 56

def script(path)
  "<script type='text/javascript' src='#{path}'></script>"
end

#style(path) ⇒ Object

Sugar: formats a css stylesheet <link /> tag with the input



51
52
53
# File 'lib/sinatra/piano.rb', line 51

def style(path)
  "<link rel='stylesheet' type='text/css' href='#{path}' />"
end

#t(key) ⇒ Object

Non implemented yet



154
155
156
# File 'lib/sinatra/piano.rb', line 154

def t(key)
  I18n.translate key
end

#try_haml(template) ⇒ Object

Like Sinatra’s/Tilt’s ‘haml`, but adds etags and returns a 404 with some hints if the haml is not found



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sinatra/piano.rb', line 9

def try_haml(template)
  file_name = "#{pwd}/#{template}.haml"
  bad_luck file_name unless File.exists? file_name
 
  if etags?
    hash = hash_for template, :haml
    hash += hash_for "data/#{template}", :yaml if File.exists? "#{pwd}/data/#{template}.yaml"
    etag hash
  end
  haml template.to_sym
end