website_agent.rb 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. require 'nokogiri'
  2. require 'date'
  3. module Agents
  4. class WebsiteAgent < Agent
  5. include WebRequestConcern
  6. can_dry_run!
  7. can_order_created_events!
  8. no_bulk_receive!
  9. default_schedule "every_12h"
  10. UNIQUENESS_LOOK_BACK = 200
  11. UNIQUENESS_FACTOR = 3
  12. description <<-MD
  13. The Website Agent scrapes a website, XML document, or JSON feed and creates Events based on the results.
  14. Specify a `url` and select a `mode` for when to create Events based on the scraped data, either `all`, `on_change`, or `merge` (if fetching based on an Event, see below).
  15. The `url` option can be a single url, or an array of urls (for example, for multiple pages with the exact same structure but different content to scrape).
  16. The WebsiteAgent can also scrape based on incoming events.
  17. * Set the `url_from_event` option to a Liquid template to generate the url to access based on the Event. (To fetch the url in the Event's `url` key, for example, set `url_from_event` to `{{ url }}`.)
  18. * Alternatively, set `data_from_event` to a Liquid template to use data directly without fetching any URL. (For example, set it to `{{ html }}` to use HTML contained in the `html` key of the incoming Event.)
  19. * If you specify `merge` for the `mode` option, Huginn will retain the old payload and update it with new values.
  20. If a created Event has a key named `url` containing a relative URL, it is automatically resolved using the request URL as base.
  21. # Supported Document Types
  22. The `type` value can be `xml`, `html`, `json`, or `text`.
  23. To tell the Agent how to parse the content, specify `extract` as a hash with keys naming the extractions and values of hashes.
  24. Note that for all of the formats, whatever you extract MUST have the same number of matches for each extractor except when it has `repeat` set to true. E.g., if you're extracting rows, all extractors must match all rows. For generating CSS selectors, something like [SelectorGadget](http://selectorgadget.com) may be helpful.
  25. For extractors with `repeat` set to true, their first matches will be included in all extracts. This is useful such as when you want to include the title of a page in all events created from the page.
  26. # Scraping HTML and XML
  27. When parsing HTML or XML, these sub-hashes specify how each extraction should be done. The Agent first selects a node set from the document for each extraction key by evaluating either a CSS selector in `css` or an XPath expression in `xpath`. It then evaluates an XPath expression in `value` (default: `.`) on each node in the node set, converting the result into a string. Here's an example:
  28. "extract": {
  29. "url": { "css": "#comic img", "value": "@src" },
  30. "title": { "css": "#comic img", "value": "@title" },
  31. "body_text": { "css": "div.main", "value": "string(.)" },
  32. "page_title": { "css": "title", "value": "string(.)", "repeat": true }
  33. }
  34. "@_attr_" is the XPath expression to extract the value of an attribute named _attr_ from a node, and `string(.)` gives a string with all the enclosed text nodes concatenated without entity escaping (such as `&amp;`). To extract the innerHTML, use `./node()`; and to extract the outer HTML, use `.`.
  35. You can also use [XPath functions](https://www.w3.org/TR/xpath/#section-String-Functions) like `normalize-space` to strip and squeeze whitespace, `substring-after` to extract part of a text, and `translate` to remove commas from formatted numbers, etc. Instead of passing `string(.)` to these functions, you can just pass `.` like `normalize-space(.)` and `translate(., ',', '')`.
  36. Beware that when parsing an XML document (i.e. `type` is `xml`) using `xpath` expressions, all namespaces are stripped from the document unless the top-level option `use_namespaces` is set to `true`.
  37. # Scraping JSON
  38. When parsing JSON, these sub-hashes specify [JSONPaths](http://goessner.net/articles/JsonPath/) to the values that you care about. For example:
  39. "extract": {
  40. "title": { "path": "results.data[*].title" },
  41. "description": { "path": "results.data[*].description" }
  42. }
  43. The `extract` option can be skipped for the JSON type, causing the full JSON response to be returned.
  44. # Scraping Text
  45. When parsing text, each sub-hash should contain a `regexp` and `index`. Output text is matched against the regular expression repeatedly from the beginning through to the end, collecting a captured group specified by `index` in each match. Each index should be either an integer or a string name which corresponds to <code>(?&lt;<em>name</em>&gt;...)</code>. For example, to parse lines of <code><em>word</em>: <em>definition</em></code>, the following should work:
  46. "extract": {
  47. "word": { "regexp": "^(.+?): (.+)$", index: 1 },
  48. "definition": { "regexp": "^(.+?): (.+)$", index: 2 }
  49. }
  50. Or if you prefer names to numbers for index:
  51. "extract": {
  52. "word": { "regexp": "^(?<word>.+?): (?<definition>.+)$", index: 'word' },
  53. "definition": { "regexp": "^(?<word>.+?): (?<definition>.+)$", index: 'definition' }
  54. }
  55. To extract the whole content as one event:
  56. "extract": {
  57. "content": { "regexp": "\A(?m:.)*\z", index: 0 }
  58. }
  59. Beware that `.` does not match the newline character (LF) unless the `m` flag is in effect, and `^`/`$` basically match every line beginning/end. See [this document](http://ruby-doc.org/core-#{RUBY_VERSION}/doc/regexp_rdoc.html) to learn the regular expression variant used in this service.
  60. # General Options
  61. Can be configured to use HTTP basic auth by including the `basic_auth` parameter with `"username:password"`, or `["username", "password"]`.
  62. Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent. This is only used to set the "working" status.
  63. Set `uniqueness_look_back` to limit the number of events checked for uniqueness (typically for performance). This defaults to the larger of #{UNIQUENESS_LOOK_BACK} or #{UNIQUENESS_FACTOR}x the number of detected received results.
  64. Set `force_encoding` to an encoding name (such as `UTF-8` and `ISO-8859-1`) if the website is known to respond with a missing, invalid, or wrong charset in the Content-Type header. Below are the steps used by Huginn to detect the encoding of fetched content:
  65. 1. If `force_encoding` is given, that value is used.
  66. 2. If the Content-Type header contains a charset parameter, that value is used.
  67. 3. When `type` is `html` or `xml`, Huginn checks for the presence of a BOM, XML declaration with attribute "encoding", or an HTML meta tag with charset information, and uses that if found.
  68. 4. Huginn falls back to UTF-8 (not ISO-8859-1).
  69. Set `user_agent` to a custom User-Agent name if the website does not like the default value (`#{default_user_agent}`).
  70. The `headers` field is optional. When present, it should be a hash of headers to send with the request.
  71. Set `disable_ssl_verification` to `true` to disable ssl verification.
  72. Set `unzip` to `gzip` to inflate the resource using gzip.
  73. Set `http_success_codes` to an array of status codes (e.g., `[404, 422]`) to treat HTTP response codes beyond 200 as successes.
  74. If a `template` option is given, it is used as a Liquid template for each event created by this Agent, instead of directly emitting the results of extraction as events. In the template, keys of extracted data can be interpolated, and some additional variables are also available as explained in the next section. For example:
  75. "template": {
  76. "url": "{{ url }}",
  77. "title": "{{ title }}",
  78. "description": "{{ body_text }}",
  79. "last_modified": "{{ _response_.headers.Last-Modified | date: '%FT%T' }}"
  80. }
  81. In the `on_change` mode, change is detected based on the resulted event payload after applying this option. If you want to add some keys to each event but ignore any change in them, set `mode` to `all` and put a DeDuplicationAgent downstream.
  82. # Liquid Templating
  83. In Liquid templating, the following variables are available except when invoked by `data_from_event`:
  84. * `_url_`: The URL specified to fetch the content from.
  85. * `_response_`: A response object with the following keys:
  86. * `status`: HTTP status as integer. (Almost always 200)
  87. * `headers`: Response headers; for example, `{{ _response_.headers.Content-Type }}` expands to the value of the Content-Type header. Keys are insensitive to cases and -/_.
  88. * `url`: The final URL of the fetched page, following redirects. Using this in the `template` option, you can resolve relative URLs extracted from a document like `{{ link | to_uri: _request_.url }}` and `{{ content | rebase_hrefs: _request_.url }}`.
  89. # Ordering Events
  90. #{description_events_order}
  91. MD
  92. event_description do
  93. if keys = event_keys
  94. "Events will have the following fields:\n\n %s" % [
  95. Utils.pretty_print(Hash[event_keys.map { |key|
  96. [key, "..."]
  97. }])
  98. ]
  99. else
  100. "Events will be the raw JSON returned by the URL."
  101. end
  102. end
  103. def event_keys
  104. (options['template'].presence || options['extract']).try(:keys)
  105. end
  106. def working?
  107. event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs?
  108. end
  109. def default_options
  110. {
  111. 'expected_update_period_in_days' => "2",
  112. 'url' => "https://xkcd.com",
  113. 'type' => "html",
  114. 'mode' => "on_change",
  115. 'extract' => {
  116. 'url' => { 'css' => "#comic img", 'value' => "@src" },
  117. 'title' => { 'css' => "#comic img", 'value' => "@alt" },
  118. 'hovertext' => { 'css' => "#comic img", 'value' => "@title" }
  119. }
  120. }
  121. end
  122. def validate_options
  123. # Check for required fields
  124. errors.add(:base, "either url, url_from_event, or data_from_event are required") unless options['url'].present? || options['url_from_event'].present? || options['data_from_event'].present?
  125. errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
  126. validate_extract_options!
  127. validate_template_options!
  128. validate_http_success_codes!
  129. # Check for optional fields
  130. if options['mode'].present?
  131. errors.add(:base, "mode must be set to on_change, all or merge") unless %w[on_change all merge].include?(options['mode'])
  132. end
  133. if options['expected_update_period_in_days'].present?
  134. errors.add(:base, "Invalid expected_update_period_in_days format") unless is_positive_integer?(options['expected_update_period_in_days'])
  135. end
  136. if options['uniqueness_look_back'].present?
  137. errors.add(:base, "Invalid uniqueness_look_back format") unless is_positive_integer?(options['uniqueness_look_back'])
  138. end
  139. validate_web_request_options!
  140. end
  141. def validate_http_success_codes!
  142. consider_success = options["http_success_codes"]
  143. if consider_success.present?
  144. if (consider_success.class != Array)
  145. errors.add(:http_success_codes, "must be an array and specify at least one status code")
  146. else
  147. if consider_success.uniq.count != consider_success.count
  148. errors.add(:http_success_codes, "duplicate http code found")
  149. else
  150. if consider_success.any?{|e| e.to_s !~ /^\d+$/ }
  151. errors.add(:http_success_codes, "please make sure to use only numeric values for code, ex 404, or \"404\"")
  152. end
  153. end
  154. end
  155. end
  156. end
  157. def validate_extract_options!
  158. extraction_type = (extraction_type() rescue extraction_type(options))
  159. case extract = options['extract']
  160. when Hash
  161. if extract.each_value.any? { |value| !value.is_a?(Hash) }
  162. errors.add(:base, 'extract must be a hash of hashes.')
  163. else
  164. case extraction_type
  165. when 'html', 'xml'
  166. extract.each do |name, details|
  167. case details['css']
  168. when String
  169. # ok
  170. when nil
  171. case details['xpath']
  172. when String
  173. # ok
  174. when nil
  175. errors.add(:base, "When type is html or xml, all extractions must have a css or xpath attribute (bad extraction details for #{name.inspect})")
  176. else
  177. errors.add(:base, "Wrong type of \"xpath\" value in extraction details for #{name.inspect}")
  178. end
  179. else
  180. errors.add(:base, "Wrong type of \"css\" value in extraction details for #{name.inspect}")
  181. end
  182. case details['value']
  183. when String, nil
  184. # ok
  185. else
  186. errors.add(:base, "Wrong type of \"value\" value in extraction details for #{name.inspect}")
  187. end
  188. end
  189. when 'json'
  190. extract.each do |name, details|
  191. case details['path']
  192. when String
  193. # ok
  194. when nil
  195. errors.add(:base, "When type is json, all extractions must have a path attribute (bad extraction details for #{name.inspect})")
  196. else
  197. errors.add(:base, "Wrong type of \"path\" value in extraction details for #{name.inspect}")
  198. end
  199. end
  200. when 'text'
  201. extract.each do |name, details|
  202. case regexp = details['regexp']
  203. when String
  204. begin
  205. re = Regexp.new(regexp)
  206. rescue => e
  207. errors.add(:base, "invalid regexp for #{name.inspect}: #{e.message}")
  208. end
  209. when nil
  210. errors.add(:base, "When type is text, all extractions must have a regexp attribute (bad extraction details for #{name.inspect})")
  211. else
  212. errors.add(:base, "Wrong type of \"regexp\" value in extraction details for #{name.inspect}")
  213. end
  214. case index = details['index']
  215. when Integer, /\A\d+\z/
  216. # ok
  217. when String
  218. if re && !re.names.include?(index)
  219. errors.add(:base, "no named capture #{index.inspect} found in regexp for #{name.inspect})")
  220. end
  221. when nil
  222. errors.add(:base, "When type is text, all extractions must have an index attribute (bad extraction details for #{name.inspect})")
  223. else
  224. errors.add(:base, "Wrong type of \"index\" value in extraction details for #{name.inspect}")
  225. end
  226. end
  227. when /\{/
  228. # Liquid templating
  229. else
  230. errors.add(:base, "Unknown extraction type #{extraction_type.inspect}")
  231. end
  232. end
  233. when nil
  234. unless extraction_type == 'json'
  235. errors.add(:base, 'extract is required for all types except json')
  236. end
  237. else
  238. errors.add(:base, 'extract must be a hash')
  239. end
  240. end
  241. def validate_template_options!
  242. template = options['template'].presence or return
  243. unless Hash === template &&
  244. template.each_pair.all? { |key, value| String === value }
  245. errors.add(:base, 'template must be a hash of strings.')
  246. end
  247. end
  248. def check
  249. check_urls(interpolated['url'])
  250. end
  251. def check_urls(in_url, existing_payload = {})
  252. return unless in_url.present?
  253. Array(in_url).each do |url|
  254. check_url(url, existing_payload)
  255. end
  256. end
  257. def check_url(url, existing_payload = {})
  258. unless /\Ahttps?:\/\//i === url
  259. error "Ignoring a non-HTTP url: #{url.inspect}"
  260. return
  261. end
  262. uri = Utils.normalize_uri(url)
  263. log "Fetching #{uri}"
  264. response = faraday.get(uri)
  265. raise "Failed: #{response.inspect}" unless consider_response_successful?(response)
  266. interpolation_context.stack {
  267. interpolation_context['_url_'] = uri.to_s
  268. interpolation_context['_response_'] = ResponseDrop.new(response)
  269. handle_data(response.body, response.env[:url], existing_payload)
  270. }
  271. rescue => e
  272. error "Error when fetching url: #{e.message}\n#{e.backtrace.join("\n")}"
  273. end
  274. def default_encoding
  275. case extraction_type
  276. when 'html', 'xml'
  277. # Let Nokogiri detect the encoding
  278. nil
  279. else
  280. super
  281. end
  282. end
  283. def handle_data(body, url, existing_payload)
  284. doc = parse(body)
  285. if extract_full_json?
  286. if store_payload!(previous_payloads(1), doc)
  287. log "Storing new result for '#{name}': #{doc.inspect}"
  288. create_event payload: existing_payload.merge(doc)
  289. end
  290. return
  291. end
  292. output =
  293. case extraction_type
  294. when 'json'
  295. extract_json(doc)
  296. when 'text'
  297. extract_text(doc)
  298. else
  299. extract_xml(doc)
  300. end
  301. num_tuples = output.each_value.inject(nil) { |num, value|
  302. case size = value.size
  303. when Float::INFINITY
  304. num
  305. when Integer
  306. if num && num != size
  307. raise "Got an uneven number of matches for #{interpolated['name']}: #{interpolated['extract'].inspect}"
  308. end
  309. size
  310. end
  311. } or raise "At least one non-repeat key is required"
  312. old_events = previous_payloads num_tuples
  313. template = options['template'].presence
  314. num_tuples.times.zip(*output.values) do |index, *values|
  315. extracted = output.each_key.lazy.zip(values).to_h
  316. result =
  317. if template
  318. interpolate_with(extracted) do
  319. interpolate_options(template)
  320. end
  321. else
  322. extracted
  323. end
  324. # url may be URI, string or nil
  325. if (payload_url = result['url'].presence) && (url = url.presence)
  326. begin
  327. result['url'] = (Utils.normalize_uri(url) + Utils.normalize_uri(payload_url)).to_s
  328. rescue URI::Error
  329. error "Cannot resolve url: <#{payload_url}> on <#{url}>"
  330. end
  331. end
  332. if store_payload!(old_events, result)
  333. log "Storing new parsed result for '#{name}': #{result.inspect}"
  334. create_event payload: existing_payload.merge(result)
  335. end
  336. end
  337. end
  338. def receive(incoming_events)
  339. incoming_events.each do |event|
  340. interpolate_with(event) do
  341. existing_payload = interpolated['mode'].to_s == "merge" ? event.payload : {}
  342. if data_from_event = options['data_from_event'].presence
  343. data = interpolate_options(data_from_event)
  344. if data.present?
  345. handle_event_data(data, event, existing_payload)
  346. else
  347. error "No data was found in the Event payload using the template #{data_from_event}", inbound_event: event
  348. end
  349. else
  350. url_to_scrape =
  351. if url_template = options['url_from_event'].presence
  352. interpolate_options(url_template)
  353. else
  354. interpolated['url']
  355. end
  356. check_urls(url_to_scrape, existing_payload)
  357. end
  358. end
  359. end
  360. end
  361. private
  362. def consider_response_successful?(response)
  363. response.success? || begin
  364. consider_success = options["http_success_codes"]
  365. consider_success.present? && (consider_success.include?(response.status.to_s) || consider_success.include?(response.status))
  366. end
  367. end
  368. def handle_event_data(data, event, existing_payload)
  369. handle_data(data, event.payload['url'], existing_payload)
  370. rescue => e
  371. error "Error when handling event data: #{e.message}\n#{e.backtrace.join("\n")}", inbound_event: event
  372. end
  373. # This method returns true if the result should be stored as a new event.
  374. # If mode is set to 'on_change', this method may return false and update an existing
  375. # event to expire further in the future.
  376. def store_payload!(old_events, result)
  377. case interpolated['mode'].presence
  378. when 'on_change'
  379. result_json = result.to_json
  380. if found = old_events.find { |event| event.payload.to_json == result_json }
  381. found.update!(expires_at: new_event_expiration_date)
  382. false
  383. else
  384. true
  385. end
  386. when 'all', 'merge', ''
  387. true
  388. else
  389. raise "Illegal options[mode]: #{interpolated['mode']}"
  390. end
  391. end
  392. def previous_payloads(num_events)
  393. if interpolated['uniqueness_look_back'].present?
  394. look_back = interpolated['uniqueness_look_back'].to_i
  395. else
  396. # Larger of UNIQUENESS_FACTOR * num_events and UNIQUENESS_LOOK_BACK
  397. look_back = UNIQUENESS_FACTOR * num_events
  398. if look_back < UNIQUENESS_LOOK_BACK
  399. look_back = UNIQUENESS_LOOK_BACK
  400. end
  401. end
  402. events.order("id desc").limit(look_back) if interpolated['mode'] == "on_change"
  403. end
  404. def extract_full_json?
  405. !interpolated['extract'].present? && extraction_type == "json"
  406. end
  407. def extraction_type(interpolated = interpolated())
  408. (interpolated['type'] || begin
  409. case interpolated['url']
  410. when /\.(rss|xml)$/i
  411. "xml"
  412. when /\.json$/i
  413. "json"
  414. when /\.(txt|text)$/i
  415. "text"
  416. else
  417. "html"
  418. end
  419. end).to_s
  420. end
  421. def use_namespaces?
  422. if interpolated.key?('use_namespaces')
  423. boolify(interpolated['use_namespaces'])
  424. else
  425. interpolated['extract'].none? { |name, extraction_details|
  426. extraction_details.key?('xpath')
  427. }
  428. end
  429. end
  430. def extract_each(&block)
  431. interpolated['extract'].each_with_object({}) { |(name, extraction_details), output|
  432. if boolify(extraction_details['repeat'])
  433. values = Repeater.new { |repeater|
  434. block.call(extraction_details, repeater)
  435. }
  436. else
  437. values = []
  438. block.call(extraction_details, values)
  439. end
  440. log "Values extracted: #{values}"
  441. output[name] = values
  442. }
  443. end
  444. def extract_json(doc)
  445. extract_each { |extraction_details, values|
  446. log "Extracting #{extraction_type} at #{extraction_details['path']}"
  447. Utils.values_at(doc, extraction_details['path']).each { |value|
  448. values << value
  449. }
  450. }
  451. end
  452. def extract_text(doc)
  453. extract_each { |extraction_details, values|
  454. regexp = Regexp.new(extraction_details['regexp'])
  455. log "Extracting #{extraction_type} with #{regexp}"
  456. case index = extraction_details['index']
  457. when /\A\d+\z/
  458. index = index.to_i
  459. end
  460. doc.scan(regexp) {
  461. values << Regexp.last_match[index]
  462. }
  463. }
  464. end
  465. def extract_xml(doc)
  466. extract_each { |extraction_details, values|
  467. case
  468. when css = extraction_details['css']
  469. nodes = doc.css(css)
  470. when xpath = extraction_details['xpath']
  471. nodes = doc.xpath(xpath)
  472. else
  473. raise '"css" or "xpath" is required for HTML or XML extraction'
  474. end
  475. log "Extracting #{extraction_type} at #{xpath || css}"
  476. case nodes
  477. when Nokogiri::XML::NodeSet
  478. nodes.each { |node|
  479. case value = node.xpath(extraction_details['value'] || '.')
  480. when Float
  481. # Node#xpath() returns any numeric value as float;
  482. # convert it to integer as appropriate.
  483. value = value.to_i if value.to_i == value
  484. end
  485. values << value.to_s
  486. }
  487. else
  488. raise "The result of HTML/XML extraction was not a NodeSet"
  489. end
  490. }
  491. end
  492. def parse(data)
  493. case type = extraction_type
  494. when "xml"
  495. doc = Nokogiri::XML(data)
  496. # ignore xmlns, useful when parsing atom feeds
  497. doc.remove_namespaces! unless use_namespaces?
  498. doc
  499. when "json"
  500. JSON.parse(data)
  501. when "html"
  502. Nokogiri::HTML(data)
  503. when "text"
  504. data
  505. else
  506. raise "Unknown extraction type: #{type}"
  507. end
  508. end
  509. def is_positive_integer?(value)
  510. Integer(value) >= 0
  511. rescue
  512. false
  513. end
  514. class Repeater < Enumerator
  515. # Repeater.new { |y|
  516. # # ...
  517. # y << value
  518. # } #=> [value, ...]
  519. def initialize(&block)
  520. @value = nil
  521. super(Float::INFINITY) { |y|
  522. loop { y << @value }
  523. }
  524. catch(@done = Object.new) {
  525. block.call(self)
  526. }
  527. end
  528. def <<(value)
  529. @value = value
  530. throw @done
  531. end
  532. def to_s
  533. "[#{@value.inspect}, ...]"
  534. end
  535. end
  536. # Wraps Faraday::Response
  537. class ResponseDrop < LiquidDroppable::Drop
  538. def headers
  539. HeaderDrop.new(@object.headers)
  540. end
  541. # Integer value of HTTP status
  542. def status
  543. @object.status
  544. end
  545. # The URL
  546. def url
  547. @object.env.url.to_s
  548. end
  549. end
  550. # Wraps Faraday::Utils::Headers
  551. class HeaderDrop < LiquidDroppable::Drop
  552. def before_method(name)
  553. @object[name.tr('_', '-')]
  554. end
  555. end
  556. end
  557. end