Class: OpenAPIRest::QueryResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_rest/query_response.rb

Overview

Rest Query Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_builder) ⇒ QueryResponse

Returns a new instance of QueryResponse.



14
15
16
17
# File 'lib/openapi_rest/query_response.rb', line 14

def initialize(query_builder)
  @query_builder = query_builder
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



7
8
9
# File 'lib/openapi_rest/query_response.rb', line 7

def errors
  @errors
end

#query_builderObject (readonly)

Returns the value of attribute query_builder.



6
7
8
# File 'lib/openapi_rest/query_response.rb', line 6

def query_builder
  @query_builder
end

Instance Method Details

#create_resourceObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openapi_rest/query_response.rb', line 19

def create_resource
  @operation = :post

  api_params = OpenAPIRest::ApiParameters.new(api_model: @query_builder.api_model,
                                              params: @query_builder.params,
                                              openapi_path: @query_builder.openapi_path)
  unless api_params.valid?
    @errors = api_params.validation_errors
    return
  end

  create_params = api_params.allowed_params.merge!(@query_builder.query)
  @model = @query_builder.raw_model.new(create_params)

  return if @model.valid? && @model.save

  build_errors
end

#delete_resourceObject



57
58
59
60
61
62
63
# File 'lib/openapi_rest/query_response.rb', line 57

def delete_resource
  @operation = :delete
  @model = @query_builder.raw_model
  return if !@model.nil? && @model.destroy

  build_errors
end

#errors?Boolean

Returns:

  • (Boolean)


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

def errors?
  !@errors.empty?
end

#resultsObject



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

def results
  @model ||= @query_builder.raw_model
end

#results?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/openapi_rest/query_response.rb', line 65

def results?
  if single?
    !results.nil?
  else
    results.count > 0
  end
end

#update_resourceObject



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

def update_resource
  @operation = :patch

  api_params = OpenAPIRest::ApiParameters.new(api_model: @query_builder.api_model,
                                              params: @query_builder.params,
                                              openapi_path: @query_builder.openapi_path)

  unless api_params.valid?
    @errors = api_params.validation_errors
    return
  end

  @model = @query_builder.raw_model

  return if !@model.nil? && @model.update(api_params.allowed_params)

  build_errors
end