class Sinew::Response

A wrapper around Faraday::Response, with some parsing helpers.

Public Instance Methods

diskpath() click to toggle source

Return the cache diskpath for this response

# File lib/sinew/response.rb, line 52
def diskpath
  env[:httpdisk_diskpath]
end
html() click to toggle source

Like body, but tries to cleanup whitespace around HTML for easier parsing.

# File lib/sinew/response.rb, line 10
def html
  @html ||= body.dup.tap do
    # fix invalid utf8
    if _1.encoding == Encoding::UTF_8
      _1.encode!('UTF-8', invalid: :replace, undef: :replace, replace: '?')
    end

    # squish
    _1.strip!
    _1.gsub!(/\s+/, ' ')

    # kill whitespace around tags
    _1.gsub!(/ ?<([^>]+)> ?/, '<\\1>')
  end
end
json() click to toggle source

Return body as JSON

# File lib/sinew/response.rb, line 27
def json
  @json ||= JSON.parse(body, symbolize_names: true)
end
mash() click to toggle source

Return JSON body as Hashie::Mash

# File lib/sinew/response.rb, line 32
def mash
  @mash ||= Hashie::Mash.new(json)
end
noko() click to toggle source

Return body HTML as Nokogiri document

# File lib/sinew/response.rb, line 37
def noko
  @noko ||= Nokogiri::HTML(html)
end
uncache() click to toggle source

Remove cached response from disk, if any

# File lib/sinew/response.rb, line 57
def uncache
  File.unlink(diskpath) if File.exist?(diskpath)
end
url() click to toggle source

Return the final URI for the request, after redirects

# File lib/sinew/response.rb, line 47
def url
  env.url
end
xml() click to toggle source

Return body XML as Nokogiri document

# File lib/sinew/response.rb, line 42
def xml
  @xml ||= Nokogiri::XML(html)
end