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_UNAUTHORIZED, 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.



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

def initialize
  super

  @resources_definitions  = []
  @root_path              ||= ''
  @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

#path_prefixObject

Returns the value of attribute path_prefix.



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

def path_prefix
  @path_prefix
end

#root_pathObject

Returns the value of attribute root_path.



37
38
39
# File 'lib/angus/base.rb', line 37

def root_path
  @root_path
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



57
58
59
60
61
62
# File 'lib/angus/base.rb', line 57

def after_configure
  super

  register_base_routes
  register_resources_routes
end

#base_pathObject



126
127
128
# File 'lib/angus/base.rb', line 126

def base_path
  "/#@path_prefix"
end

#configureObject



90
91
92
# File 'lib/angus/base.rb', line 90

def configure
  warn 'Empty configuration for service.'
end

#configure!Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/angus/base.rb', line 94

def configure!
  raise 'Already configured' if configured?

  definitions_path = if @root_path.empty?
                       'definitions'
                     else
                       File.join(@root_path, 'definitions')
                     end

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

  self.path_prefix ||= @definitions.code_name

  configure

  @configured = true
end

#configured?Boolean

Returns:

  • (Boolean)


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

def configured?
  @configured
end

#development?Boolean

Returns:

  • (Boolean)


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

def development?
  environment_name.to_sym == DEVELOPMENT_ENV
end

#environment_nameObject



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

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

#production?Boolean

Returns:

  • (Boolean)


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

def production?
  environment_name.to_sym == PRODUCTION_ENV
end

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



120
121
122
123
124
# File 'lib/angus/base.rb', line 120

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

  @resources_definitions << resource_definition
end

#register_resource_routes(resource_definition) ⇒ Object



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
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/angus/base.rb', line 130

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



64
65
66
67
68
# File 'lib/angus/base.rb', line 64

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

#service_code_nameObject



112
113
114
# File 'lib/angus/base.rb', line 112

def service_code_name
  @definitions.code_name
end

#service_versionObject



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

def service_version
  @definitions.version
end

#test?Boolean

Returns:

  • (Boolean)


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

def test?
  environment_name.to_sym == TEST_ENV
end