class LogStash::Inputs::Stripe

Generate a repeating message.

This plugin is intented only as an example.

Public Instance Methods

register() click to toggle source
# File lib/logstash/inputs/stripe.rb, line 28
def register
  Stripe.api_key = @secret_key
end
run(queue) click to toggle source
# File lib/logstash/inputs/stripe.rb, line 32
def run(queue)
  # we can abort the loop if stop? becomes true
  starting_after = nil
  while !stop?
    charges = get_charges(starting_after).to_hash
    charges.fetch(:data, []).each do |charge|
      logger.info charge.inspect
      event = LogStash::Event.new(charge)
      decorate(event)
      queue << event
      starting_after = charge[:id]
    end

    # if doc has been indexed wait 10s and start from begining
    if has_doc?(starting_after)
      starting_after = nil
      @interval = 15
    else
      @interval = 0.5
    end

    # because the sleep interval can be big, when shutdown happens
    # we want to be able to abort the sleep
    # Stud.stoppable_sleep will frequently evaluate the given block
    # and abort the sleep(@interval) if the return value is true
    Stud.stoppable_sleep(@interval) { stop? }
  end # loop
end
stop() click to toggle source
# File lib/logstash/inputs/stripe.rb, line 61
def stop
  # nothing to do in this case so it is not necessary to define stop
  # examples of common "stop" tasks:
  #  * close sockets (unblocking blocking reads/accepts)
  #  * cleanup temporary files
  #  * terminate spawned threads
end

Private Instance Methods

format_time(string) click to toggle source
# File lib/logstash/inputs/stripe.rb, line 74
def format_time(string)
  # salesforce can use different time formats so until we have a higher
  # performance requirement we can just use Time.parse
  # otherwise it's possible to use a sequence of DateTime.strptime, for example
  LogStash::Timestamp.new(Time.strptime(string, '%s'))
end
get_charges(starting_after = nil) click to toggle source
# File lib/logstash/inputs/stripe.rb, line 70
def get_charges(starting_after = nil)
  Stripe::Charge.list(limit: 100, starting_after: starting_after)
end
has_doc?(id) click to toggle source
# File lib/logstash/inputs/stripe.rb, line 81
def has_doc?(id)
  return false if id.blank?
  conn = Faraday.new(url: @es_query_url)
  conn.post '/_refresh'
  response = conn.post do |req|
    req.url '/_search'
    req.headers['Content-Type'] = 'application/json'
    req.body = %Q[{
      "query": {
        "terms": {
          "_id": [ "#{id}" ]
        }
      }
    }]
  end
  JSON.parse(response.body).fetch('hits', {}).fetch('total') == 1
end