class Fushin::Posts::Post

Attributes

url[R]

Public Class Methods

new(url) click to toggle source
# File lib/fushin/posts/post.rb, line 15
def initialize(url)
  @url = url
end

Public Instance Methods

attachements() click to toggle source
# File lib/fushin/posts/post.rb, line 53
def attachements
  []
end
btcs() click to toggle source
# File lib/fushin/posts/post.rb, line 31
def btcs
  @btcs ||= main.text.scan(/\b[13][a-km-zA-HJ-NP-Z0-9]{26,33}\b/).uniq.map do |address|
    Models::BTC.new(address)
  end
end
main() click to toggle source
# File lib/fushin/posts/post.rb, line 19
def main
  @main ||= [].tap do |out|
    body = doc.at_css(main_selector)

    main_cleanup_selectors.each do |selector|
      body.css(selector).each(&:remove)
    end

    out << body
  end.first
end
urls() click to toggle source
# File lib/fushin/posts/post.rb, line 37
def urls
  @urls ||= (urls_in_text + links).uniq.map do |url|
    next if whitelisted_domain?(url)

    Models::Website.new(url)
  end.compact.uniq(&:normalized_url)
end
urls_in_text() click to toggle source
# File lib/fushin/posts/post.rb, line 45
def urls_in_text
  @urls_in_text ||= main.text.scan(UrlRegex.get(scheme_required: true, mode: :parsing))
end

Private Instance Methods

body() click to toggle source
# File lib/fushin/posts/post.rb, line 82
def body
  res = HTTP.get(url)
  return nil unless res.code == 200

  detection = CharlockHolmes::EncodingDetector.detect(res.body.to_s)
  CharlockHolmes::Converter.convert(res.body.to_s, detection[:encoding], "UTF-8")
end
doc() click to toggle source
# File lib/fushin/posts/post.rb, line 78
def doc
  @doc ||= Oga.parse_html(body)
end
main_cleanup_selectors() click to toggle source
# File lib/fushin/posts/post.rb, line 63
def main_cleanup_selectors
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end
main_selector() click to toggle source
# File lib/fushin/posts/post.rb, line 59
def main_selector
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end
whitelisted_domain?(url) click to toggle source
# File lib/fushin/posts/post.rb, line 67
def whitelisted_domain?(url)
  uri = URI(url)
  whitelisted_domains.any? { |domain| uri.hostname.end_with? domain }
end
whitelisted_domains() click to toggle source
# File lib/fushin/posts/post.rb, line 72
def whitelisted_domains
  @whitelisted_domains ||= YAML.safe_load(
    File.read(File.expand_path("./../config/whitelisted_domains.yml", __dir__))
  )
end