Module: Sinatra::Soxer::Helpers

Defined in:
lib/soxer/main.rb

Overview

Helper module

This module introduces several helper methods to Soxer application.

Instance Method Summary collapse

Instance Method Details

#atom(doc, options = {}) ⇒ Object

The atom feed generator

This method generates an atom entry from a document.

Accepts

doc => Hash

The document hash, read from a YAML source

options => hash

Additional options, passed to haml interpreter

Returns

=> String

Atom feed entry.



197
198
199
200
201
202
# File 'lib/soxer/main.rb', line 197

def atom doc, options={}
				template = File.read File.join( File.dirname(__FILE__), 'views', 'atom.haml' )
				haml template, options.merge!( { :layout => false, 
                               :format => :xhtml,
                               :locals => { :doc => doc } } )
end

The breadcrumb generator

This method generates a stylable breadcrumb based on document’s directory path.

Accepts

options => Hash

Additional options, passed to haml interpreter

Returns

=> String

Html string containing breadcrumb path with links.



180
181
182
183
# File 'lib/soxer/main.rb', line 180

def breadcrumb options={}
  template = File.read File.join( File.dirname(__FILE__), 'views', 'breadcrumb.haml' )
  haml( template, options.merge!( :layout => false ) )
end

#disqus(options = {}) ⇒ Object

Disquss helper method

Returns the code for disqus comments.

Calling this method without parameters generates javascript code needed at the end of each page containing disqus comments code.

Accepts

options => Hash

Hash element :locals containing the following symbols

:account = google ads client ID

Returns

=> String

Html code that includes Disquss comments.



237
238
239
240
# File 'lib/soxer/main.rb', line 237

def disqus options={}
  template = File.read File.join( File.dirname(__FILE__), 'views', 'disqus.haml' )
  haml( template, options.merge!( :layout => false ) )
end

#get_list(&block) ⇒ Object

Document list generator method

This method returns a list of documents filtered by &block statement.

Accepts

&block

A callback method for filtering all available file.

Returns

=> Array

Array of hashes representing yaml files.



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/soxer/main.rb', line 137

def get_list &block 
  fileset= Dir.glob File.join( options.origin, "**", "*.yaml" )
  fileset.delete_if {|d| (d=~/\/index.yaml$/ and fileset.include? d[0..-12]+'.yaml') }
  output = fileset.map! do |f|
    file = FileReader.new f, options
    if block_given?
      f = block.call file.get_content
    else
      f = file.get_content
    end
  end.compact
end

#get_page(url = ) ⇒ Object

Document reader

This method reads a yaml file from disk and returns a hash.

Accepts

url => String

The string url. If empty, url recieved is used.

Returns

=> Hash

A hash representation of yaml file.



123
124
125
# File 'lib/soxer/main.rb', line 123

def get_page url=params[:splat][0]
  UrlReader.new(url, options).get_content
end

Google ads generator method

Returns the google ads code, according to arguments.

Accepts

options => Hash

Hash element :locals containing the following symbols

:client = google ads client ID
:slot   = google ads ad slot
:width  = google ads element width
:height = google ads element height

Returns

=> String

Html partial for the particular google ad.



218
219
220
221
222
# File 'lib/soxer/main.rb', line 218

def google_ads options={}
  template = File.read File.join( File.dirname(__FILE__), 'views', 'google_ads.haml' )
  pattern = File.join( settings.origin, "**", "*.yaml" )
  haml( template, options.merge!( :layout => false ) )
end

#google_analytics(options = {}) ⇒ Object

Google analytics helper method

Returns the basic google analytics asynchronous tracking code.

Accepts

options => Hash

Hash element :locals containing the following symbols

:tracker = google analytics tracker token

Returns

=> String

Google analytics asynchronous tracking code.



253
254
255
256
# File 'lib/soxer/main.rb', line 253

def google_analytics options={}
  template = File.read File.join( File.dirname(__FILE__), 'views', 'google_analytics.haml' )
  haml( template, options.merge!( :layout => false ) )
end

Link tag generator method

Generates Html <a href=“#”></a> tag accordng to RoR convention.

Accepts

test => string

Visible content of the link (the linked text).

url => string

Url the linked text points to.

Returns

=> String

Html link tag.



289
290
291
292
# File 'lib/soxer/main.rb', line 289

def link_to text, url="/#{text.downcase.gsub(/\s/,'_')}"
  url.gsub!(/^\//, '') if url =~ /.+:\/\//
  "<a href=\"#{url}\"> #{text}</a>"
end

#obfuscate(str = nil) ⇒ Object

String obfuscator method

Converts strings into a stream of html character codes.

Accepts

str => string

String of arbitrary length.

Returns

=> String

Html character string.



304
305
306
307
308
# File 'lib/soxer/main.rb', line 304

def obfuscate str=nil
  out = []
  str.each_byte {|c| out << "&##{c};" }
  out.join
end

#partial(snippet, options = {}) ⇒ Object

Partial snippet generator method

Returns a partial from ./views directory as specified in sinatra’s manual.

Accepts

snippet => Symbol

Symbol, representing a partial in the ./views directory (as specified by Sinatra) Note, that the name of the file follows a RoR convention: :some_file maps to ./views/_some_file.haml

options => hash

Additional options, passed to haml interpreter

Returns

=> String

Html partial.



273
274
275
# File 'lib/soxer/main.rb', line 273

def partial(snippet, options={})
  haml ('_'+snippet.to_s).to_sym, options.merge!(:layout => false)
end

#sitemapObject

Sitemap generator method

This generated an XML sitemap.

Accepts

This method does not accept arguments.

Returns

=> String

An XML representing the contents of the web site.



164
165
166
167
168
# File 'lib/soxer/main.rb', line 164

def sitemap
  template = File.read File.join( File.dirname(__FILE__), 'views', 'sitemap.haml' )
  out = '<?xml version="1.0" encoding="UTF-8"?>'+"\n"
  out << haml( template, :layout => false )
end