Class: APIVerve::Textstyler::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/apiverve/client.rb,
lib/apiverve_textstyler/client.rb

Overview

Client for the Text Styler API

Examples:

Basic usage

client = APIVerve::Textstyler::Client.new(api_key: "your_api_key")
response = client.execute({ text: "This is a very stylish piece of text", style: "bubbles" })
puts response

See Also:

Constant Summary collapse

BASE_URL =
"https://api.apiverve.com/v1/textstyler"
DEFAULT_TIMEOUT =
30
VALIDATION_RULES =

Validation rules for parameters

{
  'text' => { type: 'string', required: true },
  'style' => { type: 'string', required: true }
}
FORMAT_PATTERNS =

Format validation patterns

{
  'email' => /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
  'url' => /^https?:\/\/.+/,
  'ip' => /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/,
  'date' => /^\d{4}-\d{2}-\d{2}$/,
  'hexColor' => /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, timeout: DEFAULT_TIMEOUT, debug: false) ⇒ Client

Initialize the client

Parameters:

  • api_key (String)

    Your APIVerve API key

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    Request timeout in seconds (default: 30)

  • debug (Boolean) (defaults to: false)

    Enable debug logging (default: false)

Raises:

  • (ArgumentError)

    If API key is invalid



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/apiverve/client.rb', line 39

def initialize(api_key:, timeout: DEFAULT_TIMEOUT, debug: false)
  validate_api_key!(api_key)

  @api_key = api_key
  @timeout = timeout
  @debug = debug

  @connection = Faraday.new(url: BASE_URL) do |conn|
    conn.request :multipart
    conn.request :url_encoded
    conn.adapter Faraday.default_adapter
    conn.options.timeout = @timeout
    conn.headers["x-api-key"] = @api_key
    conn.headers["auth-mode"] = "rubygems-package"
    conn.headers["Content-Type"] = "application/json"
  end
end

Instance Method Details

#execute(params = {}) ⇒ Hash

Execute the API request

Parameters:

  • params (Hash) (defaults to: {})

    Query parameters or request body

Returns:

  • (Hash)

    API response

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/apiverve/client.rb', line 63

def execute(params = {})
  validate_params!(params)

  log("Making POST request to #{BASE_URL}")
  log("Parameters: #{params.inspect}") if params.any?

  response = @connection.post do |req|
    req.body = params.to_json
  end

  handle_response(response)
end