Class: Firecrawlrb::Client
- Inherits:
-
Object
- Object
- Firecrawlrb::Client
- Defined in:
- lib/firecrawlrb/client.rb
Constant Summary collapse
- API_URL =
"https://api.firecrawl.dev/v2/scrape".freeze
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
Instance Method Summary collapse
-
#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: #scrape)
Extract JSON matching a schema using Firecrawl v2 API.
-
#initialize(api_key: nil, api_url: nil) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(api_key: nil, api_url: nil) ⇒ Client
Returns a new instance of Client.
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_key ⇒ Object (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_url ⇒ Object (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
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() 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 |