Class: Apia::RequestEnvironment

Inherits:
Object
  • Object
show all
Includes:
EnvironmentErrorHandling
Defined in:
lib/apia/request_environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EnvironmentErrorHandling

#error_for_exception, #raise_error, #raise_exception

Constructor Details

#initialize(request, response) ⇒ RequestEnvironment

Returns a new instance of RequestEnvironment.



15
16
17
18
# File 'lib/apia/request_environment.rb', line 15

def initialize(request, response)
  @request = request
  @response = response
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



10
11
12
# File 'lib/apia/request_environment.rb', line 10

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



11
12
13
# File 'lib/apia/request_environment.rb', line 11

def response
  @response
end

Instance Method Details

#call(*args, &block) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/apia/request_environment.rb', line 20

def call(*args, &block)
  return unless block_given?

  instance_exec(@request, @response, *args, &block)
rescue ::StandardError => e
  raise_exception(e)
end

#corsObject



78
79
80
# File 'lib/apia/request_environment.rb', line 78

def cors
  @cors ||= CORS.new
end

#helper(name, *args) ⇒ Object?

Call a helper

Parameters:

  • name (Symbol)

Returns:



32
33
34
35
36
37
38
39
# File 'lib/apia/request_environment.rb', line 32

def helper(name, *args)
  helper = @request.controller.definition.helpers[name.to_sym]
  if helper.nil?
    raise InvalidHelperError, "No helper found with name #{name}"
  end

  instance_exec(*args, &helper)
end

#paginate(set, potentially_large_set: false) ⇒ void

This method returns an undefined value.

Set appropriate pagination for the given set based on the configuration specified for the endpoint

Parameters:

  • set (#limit, #count, #page, #per, #to_a, #total_pages, #current_page, #without_count)
  • large_set (Boolean)

    whether or not this is expected to be a large set



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/apia/request_environment.rb', line 47

def paginate(set, potentially_large_set: false)
  paginated_field = @request.endpoint.definition.paginated_field
  if paginated_field.nil?
    raise Apia::RuntimeError, 'Could not paginate response because no pagination has been configured for the endpoint'
  end

  paginated = set.page(@request.arguments[:page] || 1)
  paginated = paginated.per(@request.arguments[:per_page] || 30)

  large_set = false
  if potentially_large_set
    total_count = set.limit(1001).count
    if total_count > 1000
      large_set = true
      paginated = paginated.without_count
    end
  end

  @response.add_field paginated_field, paginated.to_a

  pagination_info = {}
  pagination_info[:current_page] = paginated.current_page
  pagination_info[:per_page] = paginated.limit_value
  pagination_info[:large_set] = large_set
  unless large_set
    pagination_info[:total] = paginated.total_count
    pagination_info[:total_pages] = paginated.total_pages
  end
  @response.add_field :pagination, pagination_info
end