Module: Toast

Defined in:
lib/toast.rb,
lib/toast/engine.rb,
lib/toast/version.rb,
lib/toast/config_dsl.rb

Defined Under Namespace

Modules: ConfigDSL, Errors, RequestHelpers Classes: CanonicalRequest, CollectionRequest, ConfigError, Engine, HttpRange, NotInRequestContext, PluralAssocRequest, RackApp, SingleRequest, SingularAssocRequest

Constant Summary collapse

Sym =

The BREAD

"\xF0\x9F\x8D\x9E"
VERSION =
'1.1.10'
@@expositions =

config data of all expose blocks

[]
@@current_expose =

collects all configs of one expose block (DSL methods write to it)

nil
@@path_tree =

path tree to resolve the requested model

{}

Class Method Summary collapse

Class Method Details

.base_uriObject



95
96
97
98
99
100
101
102
# File 'lib/toast.rb', line 95

def self.base_uri
  return '' unless request

  port  = ":#{request.port}" unless request.port.in?([80,443])
  # remove recource path part form full path (namespace remains)
  path  = request.path.sub(request.path_parameters[:toast_path],'')
  (request.protocol + request.host + port.to_s + path).chomp('/')
end

.disable(message = '') ⇒ Object



66
67
68
69
70
71
# File 'lib/toast.rb', line 66

def self.disable message=''
  info "Disabeling resource exposition due to config errors."
  info message unless message.blank?

  @@expositions.clear
end

.info(str) ⇒ Object



60
61
62
63
64
# File 'lib/toast.rb', line 60

def self.info str
  if Rails.const_defined?('Server') # only on console server
    puts Toast::Sym+'  Toast: '+str
  end
end

.init(config_path = 'config/toast-api/*', settings_path = 'config/toast-api.rb') ⇒ Object

called once on boot via enigne.rb



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/toast.rb', line 24

def self.init config_path='config/toast-api/*', settings_path='config/toast-api.rb'

  # clean up
  Toast.expositions.clear
  Toast.settings = nil
  Toast.path_tree = {}

  settings = ''
  # read global settings
  if File.exist? settings_path
    open settings_path do |f|
      settings = f.read
    end
  else
    info "No global settings file found: `#{settings_path}', using defaults"
    settings_path = '[defaults]'
  end

  Toast::ConfigDSL.get_settings settings, settings_path

  # read configurations
  config_files = Dir[config_path]

  if config_files.empty?
    Toast.raise_config_error "No config files found in `#{config_path}`"
  else

    config_files.each do |fname|
      open fname do |f|
        config = f.read
        Toast::ConfigDSL.get_config(config, fname)
      end
    end
  end
end

.loggerObject



77
78
79
# File 'lib/toast.rb', line 77

def self.logger
  @@logger ||= Logger.new("#{Rails.root}/log/toast.log")
end

.raise_config_error(message) ⇒ Object

Raises:



73
74
75
# File 'lib/toast.rb', line 73

def self.raise_config_error message
  raise ConfigError.new("CONFIG ERROR: #{message}")
end

.represent(instance, base_uri = nil) ⇒ Object

get the representation (as Hash) by instance (w/o request) base_uri must be passed to be prepended in URIs



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/toast.rb', line 83

def self.represent instance, base_uri = nil

  # using RequestHelper#represent_one method with a mocked up object :-/
  obj = Object.new
  class << obj
    include Toast::RequestHelpers
    attr_accessor :base_uri
  end
  obj.base_uri = base_uri || Toast.base_uri
  obj.represent_one(instance, obj.get_config(instance.class) )
end