Class: FloopFloop::Client

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

Overview

Main entry point. Thread-safe (each request opens its own Net::HTTP session); cheap to construct. See README for the full resource map.

Typical use:

client = FloopFloop::Client.new(api_key: ENV.fetch("FLOOP_API_KEY"))
project = client.projects.create(prompt: "a crypto RSI dashboard")
live    = client.projects.wait_for_live(project.fetch("project").fetch("id"))

Constant Summary collapse

DEFAULT_BASE_URL =
"https://www.floopfloop.com"
DEFAULT_TIMEOUT =
30
USER_AGENT_PREFIX =
"floopfloop-ruby-sdk/#{VERSION}".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, user_agent_suffix: nil) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/floopfloop/client.rb', line 26

def initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, user_agent_suffix: nil)
  raise ArgumentError, "api_key is required" if api_key.nil? || api_key.empty?

  @api_key    = api_key
  @base_url   = base_url.to_s.sub(%r{/+\z}, "")
  @timeout    = timeout
  @user_agent =
    if user_agent_suffix && !user_agent_suffix.to_s.empty?
      "#{USER_AGENT_PREFIX} #{user_agent_suffix}"
    else
      USER_AGENT_PREFIX.dup
    end
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



24
25
26
# File 'lib/floopfloop/client.rb', line 24

def base_url
  @base_url
end

Instance Method Details

#api_keysObject



48
# File 'lib/floopfloop/client.rb', line 48

def api_keys;      @api_keys      ||= ApiKeys.new(self);       end

#libraryObject



45
# File 'lib/floopfloop/client.rb', line 45

def library;    @library    ||= Library.new(self);    end

#projectsObject

── Resource accessors ──────────────────────────────────────────



42
# File 'lib/floopfloop/client.rb', line 42

def projects;   @projects   ||= Projects.new(self);   end

#raw_put(url, body_bytes, content_type:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raw PUT used by the Uploads two-step flow to push bytes to the presigned S3 URL. Does NOT send the bearer token and does NOT go through the data:… envelope — S3 returns empty body on success, XML on error.

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/floopfloop/client.rb', line 80

def raw_put(url, body_bytes, content_type:)
  uri = URI.parse(url)
  req = Net::HTTP::Put.new(uri.request_uri)
  req["Content-Type"] = content_type
  req["Content-Length"] = body_bytes.bytesize.to_s
  req.body = body_bytes

  response = open_http(uri).request(req)
  return if response.code.to_i < 400

  snippet = (response.body || "")[0, 512]
  raise FloopFloop::Error.new(
    code: "UNKNOWN",
    message: "uploads: S3 rejected PUT (#{response.code}): #{snippet}",
    status: response.code.to_i,
  )
end

#request(method, path, body: nil, query: nil, raw_body: nil, headers: {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Performs the HTTP request, parses the data:… envelope, and raises FloopFloop::Error on non-2xx. Resources call this; user code should not.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/floopfloop/client.rb', line 58

def request(method, path, body: nil, query: nil, raw_body: nil, headers: {})
  uri = URI.parse("#{@base_url}#{path}")
  uri.query = URI.encode_www_form(query) if query && !query.empty?

  req = build_request(method, uri, body, raw_body, headers)
  response = perform(uri, req)

  body_text = response.body || ""
  request_id = response["x-request-id"]

  if response.code.to_i >= 400
    raise parse_error(body_text, response.code.to_i, request_id, response["retry-after"])
  end

  parse_success(body_text)
end

#secretsObject



44
# File 'lib/floopfloop/client.rb', line 44

def secrets;    @secrets    ||= Secrets.new(self);    end

#subdomainsObject



43
# File 'lib/floopfloop/client.rb', line 43

def subdomains; @subdomains ||= Subdomains.new(self); end

#subscriptionsObject



47
# File 'lib/floopfloop/client.rb', line 47

def subscriptions; @subscriptions ||= Subscriptions.new(self); end

#uploadsObject



49
# File 'lib/floopfloop/client.rb', line 49

def uploads;    @uploads    ||= Uploads.new(self);    end

#usageObject



46
# File 'lib/floopfloop/client.rb', line 46

def usage;         @usage         ||= Usage.new(self);         end

#userObject



50
# File 'lib/floopfloop/client.rb', line 50

def user;       @user       ||= UserApi.new(self);    end