Class: Sprites::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sprites/client.rb

Overview

HTTP client for the Sprites API.

Examples:

client = Sprites::Client.new(token: "your-token")
sprite = client.sprites.create(name: "my-sprite")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token: nil, base_url: nil) ⇒ Client

Create a new client.

Parameters:

  • token (String, nil) (defaults to: nil)

    API token (defaults to Sprites.configuration.token)

  • base_url (String, nil) (defaults to: nil)

    API base URL (defaults to api.sprites.dev)



21
22
23
24
# File 'lib/sprites/client.rb', line 21

def initialize(token: nil, base_url: nil)
  @token = token || Sprites.configuration.token
  @base_url = base_url || Sprites.configuration.base_url
end

Instance Attribute Details

#tokenString (readonly)

Returns the API token.

Returns:

  • (String)

    the API token



15
16
17
# File 'lib/sprites/client.rb', line 15

def token
  @token
end

Instance Method Details

#auth_headersArray<Array<String>>

Returns authorization headers for WebSocket connections.

Returns:

  • (Array<Array<String>>)

    authorization headers for WebSocket connections



60
61
62
# File 'lib/sprites/client.rb', line 60

def auth_headers
  [["authorization", "Bearer #{@token}"]]
end

#checkpointsResources::Checkpoints

Access checkpoint operations.



36
37
38
# File 'lib/sprites/client.rb', line 36

def checkpoints
  Resources::Checkpoints.new(self)
end

#delete(path) ⇒ Object



70
# File 'lib/sprites/client.rb', line 70

def delete(path) = handle_response(connection.delete(path))

#execResources::Exec

Access command execution operations.

Returns:



50
51
52
# File 'lib/sprites/client.rb', line 50

def exec
  Resources::Exec.new(self)
end

#get(path) ⇒ Object



64
# File 'lib/sprites/client.rb', line 64

def get(path) = handle_response(connection.get(path))

#policiesResources::Policies

Access network policy operations.

Returns:



43
44
45
# File 'lib/sprites/client.rb', line 43

def policies
  Resources::Policies.new(self)
end

#post(path, body) ⇒ Object



66
# File 'lib/sprites/client.rb', line 66

def post(path, body) = handle_response(connection.post(path, body.to_json, "Content-Type" => "application/json"))

#post_stream(path, body, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sprites/client.rb', line 72

def post_stream(path, body, &block)
  response = connection.post(path, body.to_json, "Content-Type" => "application/json")

  raise_error(response.body) unless response.success?

  events = parse_ndjson(response.body)

  if (error_event = events.find { |e| e[:type] == "error" })
    raise Error, error_event[:error]
  end

  block_given? ? events.each(&block) : events
end

#put(path, body) ⇒ Object



68
# File 'lib/sprites/client.rb', line 68

def put(path, body) = handle_response(connection.put(path, body.to_json, "Content-Type" => "application/json"))

#spritesResources::Sprites

Access sprite operations.

Returns:



29
30
31
# File 'lib/sprites/client.rb', line 29

def sprites
  Resources::Sprites.new(self)
end

#websocket_urlString

Returns WebSocket URL derived from base URL.

Returns:

  • (String)

    WebSocket URL derived from base URL



55
56
57
# File 'lib/sprites/client.rb', line 55

def websocket_url
  @base_url.sub(/^http(s?)/) { "ws#{$1}" }
end