Sha256: 3b97a652f918bbfc9f39b55ef9aa03712aa965d9d49acefe87b94701f11cc4f9

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

require 'typhoeus'
require 'nokogiri'
require 'json'
require 'jsonpath'
require File.expand_path('../site', __FILE__)

module PageRankr
  module Tracker
    attr_accessor :tracked
    attr_accessor :raw
    attr_accessor :body

    def initialize(site, options = {})
      @site = PageRankr::Site(site)

      @options = {:method => method, :headers => {'User-Agent' => 'Page Rankr'}}
      @options[:params] = params if respond_to? :params
      @options.merge!(options)
      
      request.on_complete do |response|
        self.body = response.body
        self.raw = content(body)
        self.tracked = clean(raw)
      end
    end

    def request
      @request ||= Typhoeus::Request.new(url, @options)
    end

    def url
      raise PageRankr::MethodRequired, "A url method defining the url to the service with the value you wish to extract must be defined."
    end

    def tracked_url
      @site.url(supported_components)
    end

    def supported_components
      [:subdomain]
    end

    def method
      :get
    end

    def run
      hydra = Typhoeus::Hydra.new
      hydra.queue request
      hydra.run

      tracked
    end

    def content(body)
      if respond_to? :xpath
        Nokogiri::HTML(body).at(xpath)
      elsif respond_to? :jsonpath
        JsonPath.new(jsonpath).first(JSON.parse(body))
      elsif respond_to? :regex
        body =~ regex ? $1 : nil
      else
        raise PageRankr::MethodRequired, "A method for extracting the value must be defined. Either xpath, jsonpath, or regex."
      end.to_s
    end
    
    def clean(content)
      cleaned_content = content.to_s.gsub(/\D/, '')

      if cleaned_content.strip == ''
        nil
      else
        cleaned_content.to_i
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
PageRankr-3.1.2 lib/page_rankr/tracker.rb
PageRankr-3.1.1 lib/page_rankr/tracker.rb