Class: RemoteResource::DocumentWrapper

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/remote_resource/document_wrapper.rb

Instance Method Summary collapse

Instance Method Details

#abs_url(base, path) ⇒ Object

abs_url(‘example.com’, ‘path?param=1’)

> ‘example.com/path?param=1

abs_url(‘sub.domain.dev/other_path?other_param=2’, ‘path?param=1#anchor’)

> ‘sub.domain.dev/path?param=1#anchor



41
42
43
44
45
46
47
# File 'lib/remote_resource/document_wrapper.rb', line 41

def abs_url(base, path)
  abs = URI(base)
  rel = URI(path)
  rel.scheme = abs.scheme
  rel.host   = abs.host
  rel.to_s
end

#css_content(selector) ⇒ Object Also known as: c



2
3
4
5
6
7
# File 'lib/remote_resource/document_wrapper.rb', line 2

def css_content(selector)
  selected = at_css(selector).try(:clone)
  return unless selected
  selected.css('br').each{ |br| br.replace "\n" }
  selected.try(:content).to_s.strip
end

#css_self_content(selector) ⇒ Object Also known as: c_self



17
18
19
# File 'lib/remote_resource/document_wrapper.rb', line 17

def css_self_content(selector)
  at_css(selector).try(:xpath, 'text()').to_s.strip
end

#parse_date(date, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/remote_resource/document_wrapper.rb', line 23

def parse_date(date, options={})
  if options.any? && options.delete(:chronic) != false
    require 'chronic'
    Chronic.parse(date.to_s, {guess: :begin, context: :past}.merge(options))
  else
    begin
      Time.zone.parse(date.to_s)
    rescue ArgumentError
      nil
    end
  end
end

#tag_attribute(attribute, selector) ⇒ Object Also known as: a



11
12
13
# File 'lib/remote_resource/document_wrapper.rb', line 11

def tag_attribute(attribute, selector)
  at_css(selector).try(:[], attribute).to_s
end