module Plezi

Plezi is amazing. Read the {README}

Constants

VERSION

Attributes

app_name[RW]

Get / set the application name, which is used to create and identify the application's global pub/sub channel when using Redis.

assets[RW]

Get / set the assets folder for the `:assets` route (the root for `Plezi.route '/assets/path'`, :assets).

templates[RW]

Get / set the template folder for the {Controller#render} function.

Protected Class Methods

plezi_initialize() click to toggle source
# File lib/plezi/activation.rb, line 13
def self.plezi_initialize
   if @plezi_initialize.nil?
      @plezi_initialize = true
      self.hash_proc_4symstr # creates the Proc object used for request params
      @plezi_autostart = true if @plezi_autostart.nil?
      Iodine.patch_rack
      if((ENV['PL_REDIS_URL'.freeze] ||= ENV['REDIS_URL'.freeze]))
        ping = ENV['PL_REDIS_TIMEOUT'.freeze] || ENV['REDIS_TIMEOUT'.freeze]
        ping = ping.to_i if ping
        Iodine::PubSub.default = Iodine::PubSub::RedisEngine.new(ENV['PL_REDIS_URL'.freeze], ping: ping)
        Iodine::PubSub.default = Iodine::PubSub::CLUSTER unless Iodine::PubSub.default
      end
      at_exit do
         next if @plezi_autostart == false
         ::Iodine.listen2http app: ::Plezi.app
         ::Iodine.start
      end
   end
   true
end

Public Instance Methods

app() click to toggle source

Returns the Plezi Rack application

# File lib/plezi/api.rb, line 34
def app
   no_autostart
   puts "Running Plezi version: #{::Plezi::VERSION}"
   Plezi::Base::Router.call_method
end
hash_proc_4symstr() click to toggle source

Catches String/Symbol mixups. Add this to any Hash using Hash#default_proc=

# File lib/plezi/helpers.rb, line 48
def hash_proc_4symstr
   @hash_proc_4symstr ||= proc do |hash, key|
      case key
      when String
         tmp = key.to_sym
         hash.key?(tmp) ? hash[tmp] : nil
      when Symbol
         tmp = key.to_s
         hash.key?(tmp) ? hash[tmp] : nil
      end
   end
end
new(app, *_args) click to toggle source

@private Allows Plezi to be used as middleware.

# File lib/plezi/api.rb, line 29
def new(app, *_args)
   Plezi::Base::Router.new(app)
end
no_autostart() click to toggle source

Disables the autostart feature

# File lib/plezi/api.rb, line 23
def no_autostart
   @plezi_autostart = false
end
route(path, controller) click to toggle source

Will add a route to the Plezi application.

The order of route creation is the order of precedence.

path

the HTTP path for the route. Inline parameters and optional parameters are supported. i.e.

Plezi.route '/fixed/path', controller
Plezi.route '/fixed/path/:required_param/(:optional_param)', controller
Plezi.route '/(:format)', /^(html|json|xml)$/ # a rewrite route, usally on top.
Plezi.route '*', controller # catch all
controller

A Controller class or one of the included controllers: `false` for rewrite routes; `:client` for the Javascript Auto Dispatch client; `:assets` for a missing asset baker controller (bakes any unbaked assets into the public folder file).

# File lib/plezi/api.rb, line 53
def route(path, controller)
   plezi_initialize
   Plezi::Base::Router.route path, controller
end
rubyfy(hash) click to toggle source

Sanitizes hash data, attempting to “rubyfy” any Hash Strings to reversible Ruby objects. i.e:

{"go" => "<tag>home</tag>", "now" => "false", "count" => "8", "float" => "0.7", "empty" => ""}

Will become:

{go: "&lt;tag>home&lt;/tag&gt;", now: false, count: 8, float: "0.7", empty: nil}

As you may notice, float Strings aren't “rubyfied”. Any “rubyfied” object will revert back to the same String form using `to_s`.

# File lib/plezi/helpers.rb, line 23
def rubyfy(hash)
   case hash
   when Hash
      cpy = Hash.new(&hash_proc_4symstr)
      hash.each { |k, v| cpy[k.is_a?(String) ? k.to_sym : k] = rubyfy(v) }
      hash = cpy
   when String
      hash = if hash.empty?
                nil
             elsif hash.to_i.to_s == hash
                hash.to_i
             elsif hash == 'true'.freeze
                true
             elsif hash == 'false'.freeze
                false
             else
                ERB::Util.h try_utf8!(hash)
             end
   when Array
      hash = hash.map { |i| rubyfy(i) }
   end
   hash
end
try_utf8!(string, encoding = ::Encoding::UTF_8) click to toggle source

attempts to convert a string's encoding to UTF-8, but only when the string is a valid UTF-8 encoded string.

# File lib/plezi/helpers.rb, line 8
def try_utf8!(string, encoding = ::Encoding::UTF_8)
   return nil unless string
   string.force_encoding(::Encoding::ASCII_8BIT) unless string.force_encoding(encoding).valid_encoding?
   string
end
url_for(controller, method_sym, params = {}) click to toggle source

Will make a weak attempt to retrive a string representing a valid URL for the requested Controller's function. False positives (invalid URL strings) are possible (i.e., when requesting a URL of a method that doesn't exist).

# File lib/plezi/api.rb, line 60
def url_for(controller, method_sym, params = {})
   Plezi::Base::Router.url_for controller, method_sym, params
end

Protected Instance Methods

plezi_finalize() click to toggle source
# File lib/plezi/activation.rb, line 6
def plezi_finalize
   if @plezi_finalize.nil?
      @plezi_finalize = true
      @plezi_finalize = 1
   end
end