Class: TellMeWhen::TellMeWhen

Inherits:
Object
  • Object
show all
Defined in:
lib/tell_me_when.rb

Constant Summary collapse

VERSION =
"0.0.1"
DOC =
"\nUsage:\n  tell_me_when [--no-gui] [-c | -x] [-a | -d ] [--matches=<regex>] <site> <path_to_element>\n  tell_me_when -h | --help\n  tell_me_when -v | --version\n\nOptions:\n  -h --help                      Show this screen.\n  -v --version                   Show version.\n  -c --css                       Search by css [default: true]\n  -x --xpath                     Search by xpath\n  -a --appears                   Display when value appears [default: true]\n  -d --disappears                Display when value disappears\n  -m=<regex> --matches=<regex>   Display only if the element matches the regex\n  --no-gui                       Does not display the gui\n\n"

Instance Method Summary collapse

Constructor Details

#initialize(arguments, stdin) ⇒ TellMeWhen

Returns a new instance of TellMeWhen.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tell_me_when.rb', line 30

def initialize(arguments, stdin)
  begin
    opts = Docopt::docopt(DOC, {:argv => arguments})
    if opts["--version"]
      puts VERSION
      return VERSION
    end
    @site = opts["<site>"]
    @path_to_element = opts["<path_to_element>"]
    tell_me_when(opts)
  rescue Docopt::Exit => e
    puts e.message
    return e.message
  end
end

Instance Method Details

#css_opts(doc, opts) ⇒ Object



55
56
57
58
# File 'lib/tell_me_when.rb', line 55

def css_opts(doc, opts)
  arr = doc.css(@path_to_element)
  handle_response(arr, opts)
end

#handle_response(response, opts) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tell_me_when.rb', line 65

def handle_response(response, opts)
  test = (regex = opts["--matches"])? response.text.match(regex) : !response.empty?
  test = opts["--appears"]? test : !test
  if test
    if opts["--no-gui"]
      puts "woohoo"
    else
      alert " You told me to tell you when \#{@path_to_element} \#{opts[\"--appears\"]? \"appears\" : \"disappears\"} \#{if !(opts[\"--matches\"].nil?); \"and matches \#{opts[\"--matches\"]} \" end}on \#{@site}\"\n"
    end
  else
    puts "nope"
  end
end

#tell_me_when(opts) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/tell_me_when.rb', line 46

def tell_me_when(opts)
  doc = Nokogiri::HTML(open(@site))
  if opts["--css"]
    css_opts(doc, opts)
  else
    xpath_opts(doc, opts)
  end
end

#xpath_opts(doc, opts) ⇒ Object



60
61
62
63
# File 'lib/tell_me_when.rb', line 60

def xpath_opts(doc, opts)
  arr = doc.xpath(@path_to_element)
  handle_response(arr, opts)
end