Class: APIVerve::Syllablecounter::Client
- Inherits:
-
Object
- Object
- APIVerve::Syllablecounter::Client
- Defined in:
- lib/apiverve/client.rb,
lib/apiverve_syllablecounter/client.rb
Overview
Client for the Syllable Counter API
Constant Summary collapse
- BASE_URL =
"https://api.apiverve.com/v1/syllablecounter"- DEFAULT_TIMEOUT =
30- VALIDATION_RULES =
Validation rules for parameters
{ 'text' => { type: 'string', required: true, max_length: 500 } }
- 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
-
#execute(params = {}) ⇒ Hash
Execute the API request.
-
#initialize(api_key:, timeout: DEFAULT_TIMEOUT, debug: false) ⇒ Client
constructor
Initialize the client.
Constructor Details
#initialize(api_key:, timeout: DEFAULT_TIMEOUT, debug: false) ⇒ Client
Initialize the client
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..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
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 |