module Pdsdk

Constants

ENV_KEY_PREFIX

TODO share these strings in some json config file across all SDKs?

ENV_SECRET_KEY_KEY
VERSION

Public Class Methods

bootstrap!() click to toggle source
# File lib/pdsdk/base.rb, line 22
def bootstrap!
  logger.info "bootstrapping..."

  @secret_key = ENV[ENV_SECRET_KEY_KEY]
  if !@secret_key
    logger.warn "no $#{ENV_SECRET_KEY_KEY} detected, will not sign payloads"
  end

  @hostname = ENV["PD_SDK_HOST"] || "sdk.m.pipedream.net"
  @proto = ENV["PD_SDK_PROTO"] || "https"
end
load_metadata(metadata_path, merge_data) click to toggle source
# File lib/pdsdk/base.rb, line 74
def load_metadata(metadata_path, merge_data)
  metadata = {}
  begin
    metadata = JSON.parse(File.read(metadata_path))
  rescue
    # assume the service is not METADATA'ed, hence LOCAL development
    branch = `git rev-parse --abbrev-ref HEAD`.strip rescue nil
    metadata[:branch] = branch if !branch.blank?
    git_ref = `git rev-parse HEAD`.strip rescue nil
    metadata[:git_ref] = git_ref if !git_ref.blank?
  end
  metadata[:pdsdk_version] = Pdsdk::VERSION
  metadata[:started_at] = Time.now.to_f
  metadata[:name] = ENV["PD_METADATA_NAME"] || `hostname`.strip
  metadata.merge!(merge_data) # XXX deep?
  metadata
end
logger() click to toggle source
# File lib/pdsdk/base.rb, line 14
def logger
  @logger ||= Logger.new(STDOUT)
end
logger=(v) click to toggle source
# File lib/pdsdk/base.rb, line 18
def logger=(v)
  @logger = v
end
send_event(api_key, raw_event, opts={}) click to toggle source

XXX self.send_message for string and becomes { message } ?

# File lib/pdsdk/base.rb, line 66
def send_event(api_key, raw_event, opts={})
  # TODO make a proper worker with queue in separate thread rather than making new every time
  # ... mostly so we can connect http client once
  Thread.new do
    sync_send_event(api_key, raw_event, opts)
  end
end
sync_send_event(api_key, raw_event, opts={}, include_response=false) click to toggle source
# File lib/pdsdk/base.rb, line 34
def sync_send_event(api_key, raw_event, opts={}, include_response=false)
  event = opts[:exports] || {}
  event[:raw_event] = raw_event
  # logger.info "going to send event: #{event} to #{api_key}"
  if opts[:deployment]
    _uri = "#{@proto}://#{@hostname}/pipelines/#{api_key}/deployments/#{opts[:deployment]}/events"
  else
    _uri = "#{@proto}://#{@hostname}/pipelines/#{api_key}/events"
  end
  uri = URI(_uri)
  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: 1) do |http|
    payload = event.to_json
    headers = {
      "user-agent" => "pipedream-sdk:ruby/1",
      "content-type" => "application/json",
      "accept" => "application/json",
      "x-pd-sdk-version" => Pdsdk::VERSION,
    }
    headers["x-pd-sig"] = "sha256=#{OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @secret_key, payload)}" if @secret_key
    req = Net::HTTP::Post.new(uri.request_uri, headers)
    req.body = payload
    resp = http.request(req)
    # logger.info "received response: #{resp}"
    if include_response
      { 'code' => resp.code.to_i, 'body' => resp.body }
    else
      { 'code' => resp.code.to_i }
    end
  end
end