Class: ForestAdminAgent::Routes::Charts::ApiChartDatasource

Inherits:
AbstractAuthenticatedRoute show all
Defined in:
lib/forest_admin_agent/routes/charts/api_chart_datasource.rb

Instance Method Summary collapse

Methods inherited from AbstractAuthenticatedRoute

#build, #format_attributes

Methods inherited from AbstractRoute

#add_route, #build, #routes

Constructor Details

#initialize(chart_name) ⇒ ApiChartDatasource

Returns a new instance of ApiChartDatasource.



8
9
10
11
12
# File 'lib/forest_admin_agent/routes/charts/api_chart_datasource.rb', line 8

def initialize(chart_name)
  @chart_name = chart_name

  super()
end

Instance Method Details

#handle_api_chart(args = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/forest_admin_agent/routes/charts/api_chart_datasource.rb', line 39

def handle_api_chart(args = {})
  caller = Utils::QueryStringParser.parse_caller(args)
  datasource = ForestAdminAgent::Facades::Container.datasource

  {
    content: Serializer::ForestChartSerializer.serialize(
      datasource.render_chart(
        caller,
        @chart_name
      )
    )
  }
end

#handle_smart_chart(args = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/forest_admin_agent/routes/charts/api_chart_datasource.rb', line 53

def handle_smart_chart(args = {})
  caller = Utils::QueryStringParser.parse_caller(args)
  datasource = ForestAdminAgent::Facades::Container.datasource

  {
    content: datasource.render_chart(
      caller,
      @chart_name
    )
  }
end

#setup_routesObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/forest_admin_agent/routes/charts/api_chart_datasource.rb', line 14

def setup_routes
  # Mount both GET and POST, respectively for smart and api charts.
  slug = @chart_name.parameterize

  add_route(
    "forest_chart_get_#{slug}",
    'get',
    "/_charts/#{slug}",
    proc { |args| handle_smart_chart(args) }
  )

  add_route(
    "forest_chart_post_#{slug}",
    'post',
    "/_charts/#{slug}",
    proc { |args| handle_api_chart(args) }
  )

  unless Facades::Container.cache(:is_production)
    Facades::Container.logger.log('Debug', "Chart #{@chart_name} was mounted at /forest/_charts/#{slug}")
  end

  self
end