Class: Seekmodo::Sdk::Connector::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/seekmodo/sdk/connector/client.rb

Constant Summary collapse

DEFAULT_GATEWAY_URL =
"https://mcp.seekmodo.com"
INDEX_CHUNK_SIZE =
500
INDEX_HARD_CAP_PER_CALL =
1000
DEFAULT_REQUEST_TIMEOUT_MS =
1500
DEFAULT_CONNECT_TIMEOUT_MS =
250

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signer, gateway_url: DEFAULT_GATEWAY_URL, breaker: nil, user_agent: "seekmodo-ruby-sdk/0.5.0", storefront_host: "", connection: nil, timeout_ms: DEFAULT_REQUEST_TIMEOUT_MS, connect_timeout_ms: DEFAULT_CONNECT_TIMEOUT_MS) ⇒ Client

Returns a new instance of Client.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/seekmodo/sdk/connector/client.rb', line 25

def initialize(
  signer,
  gateway_url: DEFAULT_GATEWAY_URL,
  breaker: nil,
  user_agent: "seekmodo-ruby-sdk/0.5.0",
  storefront_host: "",
  connection: nil,
  timeout_ms: DEFAULT_REQUEST_TIMEOUT_MS,
  connect_timeout_ms: DEFAULT_CONNECT_TIMEOUT_MS
)
  @signer = signer
  @gateway_url = gateway_url.to_s.delete_suffix("/")
  @breaker = breaker
  @user_agent = user_agent
  @storefront_host = storefront_host.to_s.downcase.strip
  @owns_connection = connection.nil?
  @connection = connection || build_connection(timeout_ms, connect_timeout_ms)
end

Instance Attribute Details

#gateway_urlObject (readonly)

Returns the value of attribute gateway_url.



23
24
25
# File 'lib/seekmodo/sdk/connector/client.rb', line 23

def gateway_url
  @gateway_url
end

#signerObject (readonly)

Returns the value of attribute signer.



23
24
25
# File 'lib/seekmodo/sdk/connector/client.rb', line 23

def signer
  @signer
end

Instance Method Details

#browser_token(audience = nil) ⇒ Object



80
81
82
83
# File 'lib/seekmodo/sdk/connector/client.rb', line 80

def browser_token(audience = nil)
  body = audience.nil? ? {} : { "audience" => audience }
  post_json("/v1/tenants/token", body)
end

#closeObject



44
45
46
# File 'lib/seekmodo/sdk/connector/client.rb', line 44

def close
  @connection.close if @owns_connection && @connection.respond_to?(:close)
end

#events(events) ⇒ Object



68
69
70
# File 'lib/seekmodo/sdk/connector/client.rb', line 68

def events(events)
  post_json("/v1/events", { "events" => events })
end

#get_json(path, extra_headers = {}, signed: true) ⇒ Object



98
99
100
# File 'lib/seekmodo/sdk/connector/client.rb', line 98

def get_json(path, extra_headers = {}, signed: true)
  execute("GET", path, "", extra_headers, signed: signed)
end

#healthObject



89
90
91
# File 'lib/seekmodo/sdk/connector/client.rb', line 89

def health
  get_json("/v1/health", signed: false)
end

#index(documents, action: "upsert") ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/seekmodo/sdk/connector/client.rb', line 52

def index(documents, action: "upsert")
  if documents.length > INDEX_HARD_CAP_PER_CALL
    merged = { "ok" => true, "imported" => 0, "errors" => [] }
    documents.each_slice(INDEX_CHUNK_SIZE) do |chunk|
      res = post_json("/v1/index", { "documents" => chunk, "action" => action })
      merged["imported"] += res.fetch("imported", 0).to_i
      errors = res["errors"]
      merged["errors"] = merged["errors"] + errors if errors.is_a?(Array)
      merged["ok"] = false unless res.fetch("ok", true)
    end
    return merged
  end

  post_json("/v1/index", { "documents" => documents, "action" => action })
end

#post_json(path, body, extra_headers = {}) ⇒ Object



93
94
95
96
# File 'lib/seekmodo/sdk/connector/client.rb', line 93

def post_json(path, body, extra_headers = {})
  raw = encode_body(body)
  execute("POST", path, raw, extra_headers, signed: true)
end

#search(params) ⇒ Object



48
49
50
# File 'lib/seekmodo/sdk/connector/client.rb', line 48

def search(params)
  post_json("/v1/search", params)
end

#tenant_handshakeObject



72
73
74
# File 'lib/seekmodo/sdk/connector/client.rb', line 72

def tenant_handshake
  post_json("/v1/tenant/handshake", {})
end

#tenant_snapshotObject



76
77
78
# File 'lib/seekmodo/sdk/connector/client.rb', line 76

def tenant_snapshot
  post_json("/v1/tenant.snapshot", {})
end

#toolsObject



85
86
87
# File 'lib/seekmodo/sdk/connector/client.rb', line 85

def tools
  get_json("/v1/tools")
end