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.5"
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 = {}) ⇒ Object
XXX self.send_message for string and becomes { message } ?.
- .sync_send_event(api_key, raw_event, opts = {}, include_response = false) ⇒ Object
Class Method Details
.bootstrap! ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# 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) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pdsdk/base.rb', line 74 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
14 15 16 |
# File 'lib/pdsdk/base.rb', line 14 def logger @logger ||= Logger.new(STDOUT) end |
.logger=(v) ⇒ Object
18 19 20 |
# File 'lib/pdsdk/base.rb', line 18 def logger=(v) @logger = v end |
.send_event(api_key, raw_event, opts = {}) ⇒ Object
XXX self.send_message for string and becomes { message } ?
66 67 68 69 70 71 72 |
# 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) ⇒ Object
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 |
# 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 |