Class: Extracare2of::Runner

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

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



5
6
7
8
9
10
11
# File 'lib/ExtraCare2OF/runner.rb', line 5

def initialize(*)
  @db       = Extracare2of::Database.new
  @browser  = Extracare2of::Authentication.new
  @settings = Settings.new
  @count = 0
  # p @rewards_source
end

Instance Method Details

#async_response(url) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/ExtraCare2OF/runner.rb', line 17

def async_response(url)
  request_id = Time.now.to_i
  @browser.request(url, request_id)
  until @browser.hash[request_id][:ready]
    sleep 0.1
  end
  @browser.hash[request_id]
end

#get_couponsObject

fix
  • rewards scanner not picking up all deals

todo
  • add extrabucks support



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

def get_coupons
  @rewards_source = async_response("https://m.cvs.com/mt/www.cvs.com/extracare/landing.jsp")[:body]
  if $debug
    puts @rewards_source
    exit
  end
  @deals_array = Array.new
  rewards = @rewards_source.scan(/<div class="un_exEntry">\n.+\>(.+)\<.div><.div.+$\n.+\>(.+)\<.div.+$\n.+\>(.+)\<.div.+$\n.+$/)
  rewards.each do |deal|
    name = deal[0]
    due_date = deal[1]
    note = deal[2]
    # defer_date = deal[3]
    @deals_array.push({name: name,due_date: parse_date(due_date), defer_date: Time.now, note: note})
  end
  @deals_array
end

#initObject



13
14
15
# File 'lib/ExtraCare2OF/runner.rb', line 13

def init
  @browser.authenticate
end

#parse_date(datestring) ⇒ Object

borrowed from ttscoff’s otask



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ExtraCare2OF/runner.rb', line 61

def parse_date(datestring)
  days = 0
  if datestring =~ /^\+(\d+)$/
    days = (60 * 60 * 24 * $1.to_i)
    newdate = Time.now + days
  else
    newdate = Chronic.parse(datestring, {context: :future, ambiguous_time_range: 8})
  end
  # parsed = newdate.strftime('%D %l:%M%p').gsub(/\s+/,' ');
  # return parsed =~ /1969/ ? false : parsed
  return newdate
end

#process_coupon(coupon) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ExtraCare2OF/runner.rb', line 74

def process_coupon(coupon)
  # puts " - Sending #{get_coupons.size} tasks to OF"
  return false if @db.coupon_exists?(coupon[:name])
  @db.add_coupon(name: coupon[:name], due_date: coupon[:due_date], defer_date: coupon[:defer_date])
  puts "----"
  puts " Title: #{coupon[:name]}"
  puts " - Due Date: #{coupon[:due_date]}"
  puts " - Start Date: #{coupon[:defer_date]}"
  puts " - Note: #{coupon[:note]}"
  CreateTask::OmniFocus.new(coupon.to_hash) if @settings.use_omnifocus
  CreateTask::Reminders.new(coupon.to_hash) if @settings.use_reminders
  CreateTask::Things.new(coupon.to_hash) if @settings.use_things
  CreateTask::DueApp.new(coupon.to_hash) if @settings.use_dueapp
  # Services::Reminders.new(coupon.to_hash)
end

#runObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/ExtraCare2OF/runner.rb', line 91

def run
  puts "Looking for coupons..."
  @result = get_coupons
  @result.each {|coupon| process_coupon(coupon)}
  if @count > 0
    puts "Sent #{@count} coupons to OmniFocus"
  else
    puts "No new coupons found."
  end
end

#send_bucks_to_cardObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ExtraCare2OF/runner.rb', line 46

def send_bucks_to_card
  bucks = @rewards_source.scan(regex)
  bucks.each do |buck|
    link = buck.match(url)
    link_page = @browser.request(link)
    confirmation = @browser.request(button)
    if confirmation == regex
      puts "Extra Bucks sent to card"
    else
      puts "Error: Unable to send to card"
    end
  end
end