Class: Angus::Base

Inherits:
RequestHandler show all
Includes:
BaseActions, ProxyActions
Defined in:
lib/angus/base.rb

Constant Summary collapse

FIRST_VERSION =
'0.1'
PRODUCTION_ENV =
:production
DEVELOPMENT_ENV =
:development
TEST_ENV =
:test
DEFAULT_ENV =
DEVELOPMENT_ENV
DEFAULT_DOC_LANGUAGE =
:en

Constants inherited from RequestHandler

RequestHandler::DEFAULT_RENDER

Constants included from StatusCodes

StatusCodes::HTTP_STATUS_CODE_CONFLICT, StatusCodes::HTTP_STATUS_CODE_FORBIDDEN, StatusCodes::HTTP_STATUS_CODE_INTERNAL_SERVER_ERROR, StatusCodes::HTTP_STATUS_CODE_NOT_FOUND, StatusCodes::HTTP_STATUS_CODE_OK, StatusCodes::HTTP_STATUS_CODE_UNPROCESSABLE_ENTITY

Instance Attribute Summary collapse

Attributes inherited from RequestHandler

#middleware

Instance Method Summary collapse

Methods included from ProxyActions

#build_proxy_client, #get_proxy_client, #proxy_clients, #register_proxy_actions, #register_proxy_route, #register_proxy_routes, remote_operation

Methods included from BaseActions

#api_path, #discover_paths, #doc_path, #register_base_routes

Methods inherited from RequestHandler

#call, #call!, #disuse, #render, #router, #to_app, #use, #use_after, #use_before

Methods included from Responses

#build_data_response, #build_message, #build_messages, #build_success_response, #get_message_definition, #json

Methods included from StatusCodes

included

Constructor Details

#initializeBase

Returns a new instance of Base.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/angus/base.rb', line 37

def initialize
  super

  @resources_definitions  = []
  @version                = FIRST_VERSION
  @name                   = self.class.name.downcase
  @configured             = false
  @definitions            = nil
  @logger                 = Logger.new(STDOUT)
  @default_doc_language   = DEFAULT_DOC_LANGUAGE
  @validate_params        = :return_error

  configure!

  after_configure
end

Instance Attribute Details

#default_doc_languageObject

Returns the value of attribute default_doc_language.



34
35
36
# File 'lib/angus/base.rb', line 34

def default_doc_language
  @default_doc_language
end

#definitionsObject (readonly)

Returns the value of attribute definitions.



32
33
34
# File 'lib/angus/base.rb', line 32

def definitions
  @definitions
end

#validate_paramsObject

Returns the value of attribute validate_params.



35
36
37
# File 'lib/angus/base.rb', line 35

def validate_params
  @validate_params
end

Instance Method Details

#after_configureObject



54
55
56
57
58
59
# File 'lib/angus/base.rb', line 54

def after_configure
  super

  register_base_routes
  register_resources_routes
end

#base_pathObject



115
116
117
# File 'lib/angus/base.rb', line 115

def base_path
  "/#{service_code_name}"
end

#configureObject



87
88
89
# File 'lib/angus/base.rb', line 87

def configure
  warn 'Empty configuration for service.'
end

#configure!Object



91
92
93
94
95
96
97
98
99
# File 'lib/angus/base.rb', line 91

def configure!
  raise 'Already configured' if configured?

  @definitions = Angus::SDoc::DefinitionsReader.service_definition('definitions')

  configure

  @configured = true
end

#configured?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/angus/base.rb', line 83

def configured?
  @configured
end

#development?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/angus/base.rb', line 71

def development?
  environment_name.to_sym == DEVELOPMENT_ENV
end

#environment_nameObject



79
80
81
# File 'lib/angus/base.rb', line 79

def environment_name
  ENV['RACK_ENV'] || DEFAULT_ENV
end

#production?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/angus/base.rb', line 67

def production?
  environment_name.to_sym == PRODUCTION_ENV
end

#register(resource_name, options = {}) ⇒ Object



109
110
111
112
113
# File 'lib/angus/base.rb', line 109

def register(resource_name, options = {})
  resource_definition = ResourceDefinition.new(resource_name, @definitions)

  @resources_definitions << resource_definition
end

#register_resource_routes(resource_definition) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/angus/base.rb', line 119

def register_resource_routes(resource_definition)
  resource_definition.operations.each do |operation|
    method  = operation.http_method.to_sym
    op_path = "#{api_path}#{operation.path}"

     = resource_definition.(operation.response_elements)

    router.on(method, op_path) do |env, params|
      request  = Rack::Request.new(env)
      params   = Params.indifferent_params(params)
      response = Response.new

      resource = resource_definition.resource_class.new(request, params, operation)

      if validate_params == :return_error
        resource.run_validations!
      end

      resource.run_before_filters

      begin
        op_response = resource.send(operation.code_name)
      rescue Exception
        resource.run_after_filters(response)
        raise
      end

      op_response = {} unless op_response.is_a?(Hash)

      messages = op_response.delete(:messages)

      op_response = build_data_response(op_response, , messages)

      response.write(op_response)

      resource.run_after_filters(response)

      response
    end
  end
end

#register_resources_routesObject



61
62
63
64
65
# File 'lib/angus/base.rb', line 61

def register_resources_routes
  @resources_definitions.each do |resource|
    register_resource_routes(resource)
  end
end

#service_code_nameObject



101
102
103
# File 'lib/angus/base.rb', line 101

def service_code_name
  @definitions.code_name
end

#service_versionObject



105
106
107
# File 'lib/angus/base.rb', line 105

def service_version
  @definitions.version
end

#test?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/angus/base.rb', line 75

def test?
  environment_name.to_sym == TEST_ENV
end