Class: RubyRest::AbstractApplication

Inherits:
Object
  • Object
show all
Includes:
ApplicationLogger
Defined in:
lib/rubyrest/application.rb

Overview

Base Application class for all applications deployed with Ruby-on-Rest

Direct Known Subclasses

SecureApplication

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationLogger

#init_logger

Constructor Details

#initialize(config) ⇒ AbstractApplication

Returns a new instance of AbstractApplication.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubyrest/application.rb', line 45

def initialize( config )
  @config = config
  init_logger
  init_database
  if @config[:destroy]==true
    drop_schema
    create_schema
    load_initial_data if self.respond_to?( :load_initial_data )
  end
  register_resources
  register_formatters
  register_services
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



33
34
35
# File 'lib/rubyrest/application.rb', line 33

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



33
34
35
# File 'lib/rubyrest/application.rb', line 33

def logger
  @logger
end

Class Method Details

.resourcesObject

Returns the list of persistent resources



71
72
73
74
# File 'lib/rubyrest/application.rb', line 71

def self.resources
  @resources = [] if !@resources
  return @resources
end

.with_resources(*resources) ⇒ Object

Specifies the list of resources to be persisted



66
67
68
# File 'lib/rubyrest/application.rb', line 66

def self.with_resources *resources
  @resources = resources
end

Instance Method Details

#bind(object, params) ⇒ Object

Binds values found in the specified source in properties of the specified domain object Delegates to the formatter



174
175
176
# File 'lib/rubyrest/application.rb', line 174

def bind( object, params )
  formatter( params ).bind( object, params[:body], params )
end

#check_config(key) ⇒ Object

Checks the configuration and issues an error if the specified key was not found



61
62
63
# File 'lib/rubyrest/application.rb', line 61

def check_config( key )
  raise "missing configuration key: #{key}" if !@config[key]
end

#create_tablesObject

To be overriden



41
42
43
# File 'lib/rubyrest/application.rb', line 41

def create_tables
  
end

#formatter(params) ⇒ Object

Resolves the formatter to use in the returned representation



163
164
165
# File 'lib/rubyrest/application.rb', line 163

def formatter( params )
  @formatters[params[:format]]||@formatters[:yaml]
end

#init_databaseObject

To be overriden by applications that use databases



36
37
38
# File 'lib/rubyrest/application.rb', line 36

def init_database
  
end

#is_a_collection(model) ⇒ Object

Tells whether the specified model will be rendered as a feed or entry. To be subclassed



180
181
182
# File 'lib/rubyrest/application.rb', line 180

def is_a_collection( model )
  model == nil || model.is_a?( Array )
end

#is_a_service_doc(model) ⇒ Object

Implement me in subclasses



185
186
187
# File 'lib/rubyrest/application.rb', line 185

def is_a_service_doc( model )
  model != nil && model.is_a?( RubyRest::Atom::ServiceDocument )
end

#parse_request(params, request) ⇒ Object



154
155
156
157
158
# File 'lib/rubyrest/application.rb', line 154

def parse_request( params, request )
  data = formatter( params ).parse_request( request.body )
  @logger.info( data ) if @logger.info?
  return data
end

#register_formattersObject

Register all the formatters supported by the application For the moment, only the Atom representation of resources is supported



147
148
149
150
151
152
# File 'lib/rubyrest/application.rb', line 147

def register_formatters
  @formatters = { 
    :atom => RubyRest::Atom::Formatter.new( self ),
    :yaml => RubyRest::Format::Yaml.new( self )
  }
end

#register_resourcesObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/rubyrest/application.rb', line 76

def register_resources
  @resources = Hash.new
  self.class.resources.each{ |name|
    resource_klass = to_resource_class( name )
    resource = resource_klass.new( self, name )
    @resources[resource_klass.mount_point]=resource
    @resources[name]=resource
    @logger.info "resource mounted: #{resource} [OK]" if @logger.info?
  }
end

#register_service(service) ⇒ Object

Registers a single service



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rubyrest/application.rb', line 88

def register_service( service )
  module_key = "#{service}_module".intern
  module_key = :module if @config[module_key] == nil
  
  host_key = "#{service}_host".intern
  port_key = "#{service}_port".intern
  role_key = "#{service}_role".intern
  
  check_config( role_key )
  check_config( port_key )
   
  client_class = to_service_class( @config[module_key], service )
  @services[@config[role_key].intern] = client_class.new( @config[host_key]||"localhost", @config[port_key] )
  
  @logger.info "service registered #{service} as #{@config[role_key].intern}" if @logger.info?
end

#register_servicesObject

Registers all the services configured at startup



106
107
108
109
# File 'lib/rubyrest/application.rb', line 106

def register_services
  @services = Hash.new
  @config[:services].each{ |service| register_service( service ) } if @config[:services] != nil 
end

#render_model(params, model) ⇒ Object



167
168
169
# File 'lib/rubyrest/application.rb', line 167

def render_model( params, model )
  formatter( params ).format( model, params )
end

#resource(key) ⇒ Object



130
131
132
133
# File 'lib/rubyrest/application.rb', line 130

def resource( key )
  raise "no resource found at key: #{key}" if @resources.key?( key ) == false
  @resources[key]
end

#resource_by_path(path) ⇒ Object

Resolves the Ruby-on-Rest resource descriptor for the specified request path



123
124
125
126
127
128
# File 'lib/rubyrest/application.rb', line 123

def resource_by_path( path )
  mount_point = "/"
  path_tokens = path.split( "/" )
  mount_point = "/"+path_tokens[1] if path_tokens.length > 0
  resource(mount_point)
end

#service(name) ⇒ Object

Returns a service, by name



112
113
114
# File 'lib/rubyrest/application.rb', line 112

def service( name )
  @services.fetch( name )
end

#to_resource_class(name) ⇒ Object

Returns the resource class name for the given name



136
137
138
# File 'lib/rubyrest/application.rb', line 136

def to_resource_class( name )
  Class.by_name( @config[:module].to_s.capitalize + "::" + @config[:service].to_s.capitalize + "::" + name.to_s.capitalize )
end

#to_sObject

Returns the description and version



117
118
119
# File 'lib/rubyrest/application.rb', line 117

def to_s
  @config[:service] + " " + @config[:version]
end

#to_service_class(module_name, service_name) ⇒ Object



140
141
142
# File 'lib/rubyrest/application.rb', line 140

def to_service_class( module_name, service_name )
  Class.by_name( module_name.to_s.capitalize + "::" + service_name.to_s.capitalize + "::Client" )
end