Class: Mode::Sdk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mode/sdk/client.rb,
lib/mode/sdk/client/request.rb,
lib/mode/sdk/client/response.rb

Overview

The Client class provides an interface for common Mode API requests

Defined Under Namespace

Classes: AuthenticationError, AuthorizationError, Request, Response, ResponseError

Class Method Summary collapse

Class Method Details

.accountMode::Sdk::Client::Response

Get the Mode API representation of the authenticated account


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

def 
  get('/api/account', expect: [200]).body
end

.authenticated?true, false

Test authentication credentials


81
82
83
# File 'lib/mode/sdk/client.rb', line 81

def authenticated?
  get('/api/account', expect: [200, 401]).code == 200
end

.build_path(base, params = {}) ⇒ String

Build a path with optional query string from hash


92
93
94
95
96
# File 'lib/mode/sdk/client.rb', line 92

def build_path(base, params = {})
  uri = URI(base)
  uri.query = URI.encode_www_form(params) if params.any?
  uri.to_s
end

.get(path, options = {}) ⇒ Mode::Sdk::Client::Response

Make an HTTP GET request with the Mode API


33
34
35
# File 'lib/mode/sdk/client.rb', line 33

def get(path, options = {})
  request(Net::HTTP::Get.new(path), nil, options)
end

.head(path, options = {}) ⇒ Mode::Sdk::Client::Response

Make an HTTP HEAD request with the Mode API


21
22
23
# File 'lib/mode/sdk/client.rb', line 21

def head(path, options = {})
  request(Net::HTTP::Head.new(path), nil, options)
end

.post(path, body, options = {}) ⇒ Mode::Sdk::Client::Response

Make an HTTP POST request with the Mode API

The body param may be either:

  • a String containing raw CSV, or
  • an IO-like object containing the raw CSV for streaming requests

51
52
53
# File 'lib/mode/sdk/client.rb', line 51

def post(path, body, options = {})
  request(Net::HTTP::Post.new(path), body, options)
end

.put(path, body, options = {}) ⇒ Mode::Sdk::Client::Response

Make an HTTP PUT request with the Mode API


64
65
66
# File 'lib/mode/sdk/client.rb', line 64

def put(path, body, options = {})
  request(Net::HTTP::Put.new(path), body, options)
end