class XpRubyClient::Requester

Public Class Methods

new(conn) click to toggle source
# File lib/xp_ruby_client/requester.rb, line 6
def initialize(conn)
  @sess = Patron::Session.new do |patron|
    patron.timeout = 30
    patron.base_url = conn.base_url
    patron.headers = {"User-Agent" => "XpRubyClient",
                      "Content-Type" => "application/json"}
  end
  # @sess.enable_debug "/tmp/patron.debug"
  build_payload(conn.to_h)
end

Public Instance Methods

build_payload(segments: {}, assigned: [], **) click to toggle source
# File lib/xp_ruby_client/requester.rb, line 17
def build_payload(segments: {}, assigned: [], **)
  # TODO: log not used arguemnts
  @payload = JSON.dump({segments: segments, assigned: assigned})
end
retrieve(req_payload = nil) click to toggle source
# File lib/xp_ruby_client/requester.rb, line 22
def retrieve(req_payload = nil)
  build_payload(req_payload) if req_payload
  resp = @sess.post("/experiments", @payload)
  parse_body(resp)
end

Private Instance Methods

parse_body(resp) click to toggle source
# File lib/xp_ruby_client/requester.rb, line 30
def parse_body(resp)
  JSON.parse(resp.body) if resp.status < 400
  # we don't wat to panic anyhow, so always return nil
  # TODO: log it and/or send to an error catcher
rescue => e
  nil
end