Module: PageRankr::Tracker

Included in:
Backlink, Index, Rank, Social
Defined in:
lib/page_rankr/tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



12
13
14
# File 'lib/page_rankr/tracker.rb', line 12

def body
  @body
end

#rawObject

Returns the value of attribute raw.



11
12
13
# File 'lib/page_rankr/tracker.rb', line 11

def raw
  @raw
end

#trackedObject

Returns the value of attribute tracked.



10
11
12
# File 'lib/page_rankr/tracker.rb', line 10

def tracked
  @tracked
end

Instance Method Details

#clean(content) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/page_rankr/tracker.rb', line 64

def clean(content)
  cleaned_content = content.to_s.gsub(/\D/, '')

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

#content(body) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/page_rankr/tracker.rb', line 52

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

#initialize(site, options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/page_rankr/tracker.rb', line 14

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

  # Initialize proxy, so threads don't need to synchronize the proxy service.
  proxy
end

#methodObject



34
35
36
# File 'lib/page_rankr/tracker.rb', line 34

def method
  :get
end

#nameObject



74
75
76
# File 'lib/page_rankr/tracker.rb', line 74

def name
  raise PageRankr::MethodRequired, "name is undefined for #{self.class.name}"
end

#proxyObject



38
39
40
# File 'lib/page_rankr/tracker.rb', line 38

def proxy
  @proxy ||= URI.parse(PageRankr.proxy_service.proxy(name, @site)) if PageRankr.proxy_service
end

#runObject



42
43
44
45
46
47
48
49
50
# File 'lib/page_rankr/tracker.rb', line 42

def run
  PageRankr::Request.new(self, @options).perform do |body|
    self.body = body
    self.raw = content(body)
    self.tracked = clean(raw)
  end

  tracked
end

#supported_componentsObject



30
31
32
# File 'lib/page_rankr/tracker.rb', line 30

def supported_components
  [:subdomain]
end

#tracked_urlObject



26
27
28
# File 'lib/page_rankr/tracker.rb', line 26

def tracked_url
  @site.url(supported_components)
end

#urlObject



22
23
24
# File 'lib/page_rankr/tracker.rb', line 22

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