Class: Firecrawlrb::Client

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

Constant Summary collapse

API_URL =
"https://api.firecrawl.dev/v2/scrape".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, api_url: nil) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


13
14
15
16
17
# File 'lib/firecrawlrb/client.rb', line 13

def initialize(api_key: nil, api_url: nil)
  @api_key = api_key || ENV['FIRECRAWL_API_KEY'] || Firecrawlrb.configuration.api_key
  @api_url = api_url || Firecrawlrb.configuration.api_url || API_URL
  raise ArgumentError, "API key is required. Set ENV['FIRECRAWL_API_KEY'], use Firecrawlrb.configure, or pass it to Client.new" if @api_key.nil? || @api_key.empty?
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



11
12
13
# File 'lib/firecrawlrb/client.rb', line 11

def api_key
  @api_key
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



11
12
13
# File 'lib/firecrawlrb/client.rb', line 11

def api_url
  @api_url
end

Instance Method Details

#extract(url_or_kw = nil, schema_arg = nil, prompt_arg = nil, url: nil, schema: nil, prompt: nil, only_main_content: true, max_age: 172800000, options: {}) ⇒ Hash, ... Also known as: scrape

Extract JSON matching a schema using Firecrawl v2 API

Parameters:

  • url_or_kw (String, nil) (defaults to: nil)

    Target URL if positional argument

  • schema_arg (Hash, Array, nil) (defaults to: nil)

    Schema if positional argument

  • prompt_arg (String, nil) (defaults to: nil)

    Prompt if positional argument

  • url (String, nil) (defaults to: nil)

    Target URL if keyword argument

  • schema (Hash, Array, nil) (defaults to: nil)

    Schema if keyword argument

  • prompt (String, nil) (defaults to: nil)

    Prompt if keyword argument

  • only_main_content (Boolean) (defaults to: true)

    Whether to extract main content only (default true)

  • max_age (Integer) (defaults to: 172800000)

    Cache max age in ms (default 172800000)

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

    Additional options to merge into payload

Returns:

  • (Hash, Array, nil)

    The parsed extracted JSON

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/firecrawlrb/client.rb', line 31

def extract(url_or_kw = nil, schema_arg = nil, prompt_arg = nil, url: nil, schema: nil, prompt: nil, only_main_content: true, max_age: 172800000, options: {})
  target_url = url || url_or_kw
  target_schema = schema || schema_arg
  target_prompt = prompt || prompt_arg

  raise ArgumentError, "url is required" if target_url.nil? || target_url.to_s.empty?
  raise ArgumentError, "schema is required" if target_schema.nil?

  normalized_schema = Firecrawlrb::Schema.normalize(target_schema)

  format_hash = {
    type: "json",
    schema: normalized_schema
  }
  format_hash[:prompt] = target_prompt if target_prompt && !target_prompt.to_s.empty?

  payload = {
    url: target_url.to_s,
    onlyMainContent: only_main_content,
    maxAge: max_age,
    parsers: [],
    formats: [format_hash]
  }.merge(options)

  response = request(payload)
  parsed = parse_response(response)

  if parsed.is_a?(Hash) && parsed.key?("data") && parsed["data"].is_a?(Hash) && parsed["data"].key?("json")
    parsed["data"]["json"]
  elsif parsed.is_a?(Hash) && parsed.key?("json")
    parsed["json"]
  else
    parsed
  end
end