Class: APIVerve::Imagecaption::Client
- Inherits:
-
Object
- Object
- APIVerve::Imagecaption::Client
- Defined in:
- lib/apiverve/client.rb,
lib/apiverve_imagecaption/client.rb
Overview
Client for the Image Caption API
Constant Summary collapse
- BASE_URL =
"https://api.apiverve.com/v1/imagecaption"- DEFAULT_TIMEOUT =
30- VALIDATION_RULES =
Validation rules for parameters
{ 'image' => { 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
-
#execute(params = {}) ⇒ Hash
Execute the API request.
-
#execute_with_file(file_path, params = {}) ⇒ Hash
Execute the API request with a file upload.
-
#execute_with_url(url, params = {}) ⇒ Hash
Execute the API request with a file URL.
-
#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 |
#execute_with_file(file_path, params = {}) ⇒ Hash
Execute the API request with a file upload
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/apiverve/client.rb', line 83 def execute_with_file(file_path, params = {}) raise ArgumentError, "File not found: #{file_path}" unless File.exist?(file_path) log("Uploading file: #{file_path}") payload = { "image" => Faraday::Multipart::FilePart.new( file_path, mime_type_for(file_path), File.basename(file_path) ) } payload.merge!(params.transform_keys(&:to_s)) response = @connection.post do |req| req.headers.delete("Content-Type") # Let Faraday set it req.body = payload end handle_response(response) end |
#execute_with_url(url, params = {}) ⇒ Hash
Execute the API request with a file URL
111 112 113 114 115 116 |
# File 'lib/apiverve/client.rb', line 111 def execute_with_url(url, params = {}) raise ArgumentError, "URL must be a string" unless url.is_a?(String) raise ArgumentError, "Invalid URL format" unless url.match?(/^https?:\/\/.+/) execute(params.merge(url: url)) end |