Module: Octo::Helpers::ApiHelper

Includes:
Stats
Defined in:
lib/octocore-cassandra/helpers/api_helper.rb

Constant Summary collapse

KONG_HEADERS =
%w(HTTP_X_CONSUMER_ID HTTP_X_CONSUMER_CUSTOM_ID HTTP_X_CONSUMER_USERNAME)

Instance Method Summary collapse

Methods included from Stats

#instrument, #stats

Instance Method Details

#enterprise_detailsHash

Get enterprise details from the HTTP headers that Kong sets

Returns:

  • (Hash)

    The hash of enterprise details



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/octocore-cassandra/helpers/api_helper.rb', line 15

def enterprise_details
  kong_config = Octo.get_config :kong
  if kong_config[:enabled]
    KONG_HEADERS.inject({}) do |r, header|
      key = header.gsub('HTTP_X_CONSUMER_', '').downcase
      r[key] = request.env.fetch(header, nil)
      r
    end
  else
    apikey = request.env.fetch('HTTP_APIKEY')
    {custom_id: settings.redis.get(apikey)}
  end
end

#post_paramsHash

Gets the POSTed parameters from rack env

Returns:

  • (Hash)

    A hash of POSTed parameters



31
32
33
34
35
# File 'lib/octocore-cassandra/helpers/api_helper.rb', line 31

def post_params
  instrument(:json_parse) do
   JSON.parse(request.env['rack.input'].read)
  end
end

#process_request(event_name) ⇒ JSON

Process an incoming request

Parameters:

  • event_name (String)

    The name of the event

Returns:

  • (JSON)

    The json return value after processing



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/octocore-cassandra/helpers/api_helper.rb', line 46

def process_request(event_name)
  postparams = post_params
  opts = {
    event_name: event_name,
    enterprise: enterprise_details,
    uuid: uuid
  }
  postparams.merge!(opts)
  kafka_config = Octo.get_config :kafka
  if kafka_config[:enabled]
    settings.kafka_bridge.push(postparams)
  else
    settings.queue.push(JSON.dump(postparams))
  end
  { eventId: opts[:uuid] }.to_json
end

#uuidString

Generate a UUID for each response

Returns:



39
40
41
# File 'lib/octocore-cassandra/helpers/api_helper.rb', line 39

def uuid
  SecureRandom.uuid
end