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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/angus/base.rb', line 34

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

  configure!

  after_configure
end

Instance Attribute Details

#default_doc_languageObject

Returns the value of attribute default_doc_language.



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

def default_doc_language
  @default_doc_language
end

#definitionsObject (readonly)

Returns the value of attribute definitions.



30
31
32
# File 'lib/angus/base.rb', line 30

def definitions
  @definitions
end

Instance Method Details

#after_configureObject



50
51
52
53
54
55
# File 'lib/angus/base.rb', line 50

def after_configure
  super

  register_base_routes
  register_resources_routes
end

#base_pathObject



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

def base_path
  "/#{service_code_name}"
end

#configureObject



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

def configure
  warn 'Empty configuration for service.'
end

#configure!Object



87
88
89
90
91
92
93
94
95
# File 'lib/angus/base.rb', line 87

def configure!
  raise 'Already configured' if configured?

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

  configure

  @configured = true
end

#configured?Boolean

Returns:

  • (Boolean)


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

def configured?
  @configured
end

#development?Boolean

Returns:

  • (Boolean)


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

def development?
  environment_name.to_sym == DEVELOPMENT_ENV
end

#environment_nameObject



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

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

#production?Boolean

Returns:

  • (Boolean)


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

def production?
  environment_name.to_sym == PRODUCTION_ENV
end

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



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

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

  @resources_definitions << resource_definition
end

#register_resource_routes(resource_definition) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/angus/base.rb', line 115

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)

      op_response = resource.send(operation.code_name)
      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)

      response
    end
  end
end

#register_resources_routesObject



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

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

#service_code_nameObject



97
98
99
# File 'lib/angus/base.rb', line 97

def service_code_name
  @definitions.code_name
end

#service_versionObject



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

def service_version
  @definitions.version
end

#test?Boolean

Returns:

  • (Boolean)


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

def test?
  environment_name.to_sym == TEST_ENV
end