Class: Anakin::Client

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

Overview

Synchronous Ruby client for the Anakin web-scraping API.

client = Anakin::Client.new(api_key: 'ak-...')  # or set ANAKIN_API_KEY
doc = client.scrape('https://example.com')
puts doc['markdown']

Long-running endpoints (scrape / map / crawl / agentic_search / wire) poll internally — the call blocks until the job reaches a terminal status, then returns the final result. Safe for reuse across threads; underlying Net::HTTP connections are created per-request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, base_url: nil, timeout: 60, max_retries: 4, poll_interval: 1, poll_max_interval: 10, poll_timeout: 300) ⇒ Client

Returns a new instance of Client.

Raises:



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

def initialize(api_key: nil, base_url: nil, timeout: 60,
               max_retries: 4, poll_interval: 1, poll_max_interval: 10,
               poll_timeout: 300)
  @api_key = api_key || ENV['ANAKIN_API_KEY']
  raise Error, 'no API key — pass api_key: or set ANAKIN_API_KEY' if @api_key.nil? || @api_key.empty?

  @base_url = (base_url || DEFAULT_BASE_URL).chomp('/')
  @timeout = timeout
  @max_retries = max_retries
  @poll_interval = poll_interval
  @poll_max_interval = poll_max_interval
  @poll_timeout = poll_timeout
  @sessions = Sessions.new(self)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



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

def max_retries
  @max_retries
end

#poll_intervalObject (readonly)

Returns the value of attribute poll_interval.



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

def poll_interval
  @poll_interval
end

#poll_max_intervalObject (readonly)

Returns the value of attribute poll_max_interval.



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

def poll_max_interval
  @poll_max_interval
end

#poll_timeoutObject (readonly)

Returns the value of attribute poll_timeout.



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

def poll_timeout
  @poll_timeout
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Instance Method Details

#agentic_search(prompt, **opts) ⇒ Object

AI-synthesised agentic search. Polls until the job completes.



74
75
76
77
78
# File 'lib/anakin/client.rb', line 74

def agentic_search(prompt, **opts)
  submit = request_json(:post, '/agentic-search', body: { prompt: prompt }.merge(opts))
  poll = poll_job("/agentic-search/#{require_field(submit, 'job_id')}")
  poll['result']
end

#crawl(url, **opts) ⇒ Object

Crawl a site starting from a URL. Returns when the job completes.



62
63
64
65
66
# File 'lib/anakin/client.rb', line 62

def crawl(url, **opts)
  submit = request_json(:post, '/crawl', body: { url: url }.merge(opts))
  poll = poll_job("/crawl/#{require_field(submit, 'job_id')}")
  poll['result']
end

#map(url, **opts) ⇒ Object

Discover links for a domain. Returns when the job completes.



55
56
57
58
59
# File 'lib/anakin/client.rb', line 55

def map(url, **opts)
  submit = request_json(:post, '/map', body: { url: url }.merge(opts))
  poll = poll_job("/map/#{require_field(submit, 'job_id')}")
  poll['result']
end

#scrape(url, **opts) ⇒ Object

Scrape a single URL. Returns when the job reaches a terminal status.



48
49
50
51
52
# File 'lib/anakin/client.rb', line 48

def scrape(url, **opts)
  submit = request_json(:post, '/url-scraper', body: { url: url }.merge(opts))
  poll = poll_job("/url-scraper/#{require_field(submit, 'job_id')}")
  poll['result']
end

#search(query, **opts) ⇒ Object

Synchronous web search.



69
70
71
# File 'lib/anakin/client.rb', line 69

def search(query, **opts)
  request_json(:post, '/search', body: { prompt: query }.merge(opts))
end

#sessionsObject

Browser-session management endpoints.



43
44
45
# File 'lib/anakin/client.rb', line 43

def sessions
  @sessions
end

#wire(action_id, params = {}) ⇒ Object

Execute a Wire (Holocron) action by ID. Polls until the job completes.



81
82
83
84
85
86
87
# File 'lib/anakin/client.rb', line 81

def wire(action_id, params = {})
  body = { action_id: action_id }
  body[:params] = params if params && !params.empty?
  submit = request_json(:post, '/holocron/task', body: body)
  poll = poll_job("/holocron/task/#{require_field(submit, 'job_id')}")
  poll['result']
end