Class: ActiveScraper::CachedResponse
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ActiveScraper::CachedResponse
- Defined in:
- app/models/active_scraper/cached_response.rb
Instance Method Summary collapse
- #binary? ⇒ Boolean
- #body ⇒ Object
- #body_changed? ⇒ Boolean
- #html? ⇒ Boolean
- #json? ⇒ Boolean
- #parsed_body ⇒ Object
- #text? ⇒ Boolean
- #to_fake_party_hash ⇒ Object
- #to_s ⇒ Object
- #xml? ⇒ Boolean
Instance Method Details
#binary? ⇒ Boolean
20 21 22 |
# File 'app/models/active_scraper/cached_response.rb', line 20 def binary? content_type =~ /pdf|image/ || !text? end |
#body ⇒ Object
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
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
28 29 30 |
# File 'app/models/active_scraper/cached_response.rb', line 28 def html? content_type =~ /html/ end |
#json? ⇒ Boolean
24 25 26 |
# File 'app/models/active_scraper/cached_response.rb', line 24 def json? content_type =~ /json/ end |
#parsed_body ⇒ Object
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
36 37 38 |
# File 'app/models/active_scraper/cached_response.rb', line 36 def text? content_type =~ /text/ || xml? || json? end |
#to_fake_party_hash ⇒ Object
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_s ⇒ Object
63 64 65 |
# File 'app/models/active_scraper/cached_response.rb', line 63 def to_s body end |
#xml? ⇒ Boolean
32 33 34 |
# File 'app/models/active_scraper/cached_response.rb', line 32 def xml? html? || content_type =~ /xml/ end |