Class: APIVerve::Readingtime::Client

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

Overview

Client for the Reading Time Calculator API

Examples:

Basic usage

client = APIVerve::Readingtime::Client.new(api_key: "your_api_key")
response = client.execute({ text: "This is a sample article with multiple paragraphs. Reading time will be calculated based on word count. The average reading speed is 250 words per minute, which can be customized. This helps content creators provide accurate time estimates to their readers.", wpm: 250 })
puts response

See Also:

Constant Summary collapse

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

Validation rules for parameters

{
  'text' => { type: 'string', required: true, max_length: 500000 },
  'wpm' => { type: 'integer', required: false, min: 50, max: 1000 }
}
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