Module: Pdsdk
- Defined in:
- lib/pdsdk/base.rb,
lib/pdsdk/logger.rb,
lib/pdsdk/version.rb,
lib/pdsdk/clients/base.rb
Defined Under Namespace
Constant Summary collapse
- ENV_KEY_PREFIX =
TODO share these strings in some json config file across all SDKs?
"PD_"
- ENV_SECRET_KEY_KEY =
"#{ENV_KEY_PREFIX}SECRET_KEY"
- VERSION =
"0.7.4"
Class Method Summary collapse
- .bootstrap! ⇒ Object
- .load_metadata(metadata_path, merge_data) ⇒ Object
- .logger ⇒ Object
- .logger=(v) ⇒ Object
-
.send_event(api_key, raw_event, opts = {}, include_response = false) ⇒ Object
XXX self.send_message for string and becomes { message } ?.
Class Method Details
.bootstrap! ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/pdsdk/base.rb', line 23 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 end |
.load_metadata(metadata_path, merge_data) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pdsdk/base.rb', line 68 def (, merge_data) = {} begin = JSON.parse(File.read()) rescue # assume the service is not METADATA'ed, hence LOCAL development branch = `git rev-parse --abbrev-ref HEAD`.strip rescue nil [:branch] = branch if !branch.blank? git_ref = `git rev-parse HEAD`.strip rescue nil [:git_ref] = git_ref if !git_ref.blank? end [:pdsdk_version] = Pdsdk::VERSION [:started_at] = Time.now.to_f [:name] = ENV["PD_METADATA_NAME"] || `hostname`.strip .merge!(merge_data) # XXX deep? end |
.logger ⇒ Object
15 16 17 |
# File 'lib/pdsdk/base.rb', line 15 def logger @logger ||= Logger.new(STDOUT) end |
.logger=(v) ⇒ Object
19 20 21 |
# File 'lib/pdsdk/base.rb', line 19 def logger=(v) @logger = v end |
.send_event(api_key, raw_event, opts = {}, include_response = false) ⇒ Object
XXX self.send_message for string and becomes { message } ?
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/pdsdk/base.rb', line 33 def send_event(api_key, raw_event, opts={}, include_response = false) hostname = ENV["PD_SDK_HOST"] || "sdk.m.pipedream.net" proto = ENV["PD_SDK_PROTO"] || "https" event = opts[:exports] || {} event[:raw_event] = raw_event 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) use_ssl = uri.scheme == "https" # TODO clean up old connections # TODO ensure reconnects if client disconnects @http_connection ||= Concurrent::ThreadLocalVar.new { Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl, open_timeout: 1) } logger.info "going to send event: #{event} to #{api_key}" # TODO remove 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_connection.value.request(req) logger.info "received response: #{resp}" # TODO remove if include_response { 'code' => resp.code.to_i, 'body' => resp.body } else { 'code' => resp.code.to_i } end end |