Class: Supai::API

Inherits:
Object
  • Object
show all
Defined in:
lib/supai/api.rb

Defined Under Namespace

Classes: Error, Response

Constant Summary collapse

DEFAULT_HOST =
ENV.fetch("SUPAI_HOST", "api-un76stg.shop.staging.theunitedfamily.com")

Instance Method Summary collapse

Constructor Details

#initialize(host = DEFAULT_HOST) ⇒ API

Returns a new instance of API.



45
46
47
48
# File 'lib/supai/api.rb', line 45

def initialize(host = DEFAULT_HOST)
  host ||= DEFAULT_HOST # in case we are given nil
  @host = sanitize_host(host)
end

Instance Method Details

#request(method, endpoint, body: nil, params: {}, headers: {}, token: nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/supai/api.rb', line 56

def request(method, endpoint, body: nil, params: {}, headers: {}, token: nil)
  uri = build_uri(endpoint, params)
  http_start(uri) { |http|
    http.request(
      build_request(
        method,
        uri,
        body: body,
        headers: headers,
        token: token,
      )
    )
  }
end

#sanitize_host(host) ⇒ Object



50
51
52
53
54
# File 'lib/supai/api.rb', line 50

def sanitize_host(host)
  host = host.split("://", 2).last
  host = host[0..-2] if host.end_with?("/")
  host
end