Class: ActiveScraper::CachedResponse

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/active_scraper/cached_response.rb

Instance Method Summary collapse

Instance Method Details

#binary?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'app/models/active_scraper/cached_response.rb', line 20

def binary?
  content_type =~ /pdf|image/ || !text?
end

#bodyObject



44
45
46
47
48
49
50
51
# File 'app/models/active_scraper/cached_response.rb', line 44

def body
  b = read_attribute(:body)
  if b.present? && binary? && !body_changed?
    return Base64.decode64(b)
  else
    return b
  end
end

#body_changed?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/models/active_scraper/cached_response.rb', line 40

def body_changed?
  self.changed_attributes.keys.include?('body')
end

#html?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/models/active_scraper/cached_response.rb', line 28

def html?
  content_type =~ /html/
end

#json?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/models/active_scraper/cached_response.rb', line 24

def json?
  content_type =~ /json/
end

#parsed_bodyObject



53
54
55
56
57
58
59
60
61
# File 'app/models/active_scraper/cached_response.rb', line 53

def parsed_body
  @_parsedbody ||= if xml?
    Nokogiri::HTML(body)
  elsif json?
    JSON.parse(body)
  else
    body
  end
end

#text?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/models/active_scraper/cached_response.rb', line 36

def text?
  content_type =~ /text/ || xml? || json?
end

#to_fake_party_hashObject



11
12
13
14
15
16
17
# File 'app/models/active_scraper/cached_response.rb', line 11

def to_fake_party_hash
  [:body, :headers, :content_type, :code].inject(Hashie::Mash.new) do |hsh, att|
    hsh[att] = self.send(att)

    hsh
  end
end

#to_sObject



63
64
65
# File 'app/models/active_scraper/cached_response.rb', line 63

def to_s
  body
end

#xml?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/active_scraper/cached_response.rb', line 32

def xml?
  html? || content_type =~ /xml/
end