class OdeskJobnotifier

Fetches, filters and notifies about last jobs.

Constants

VERSION

Public Class Methods

new(params) click to toggle source
# File lib/odesk_jobnotifier.rb, line 14
def initialize(params)
  @account = params[:account]
  @queries = params[:queries]
  @interval = params[:interval]
  @ojf = OdeskJobfetch.new
end

Public Instance Methods

run() click to toggle source
# File lib/odesk_jobnotifier.rb, line 21
def run
  puts 'Authenticating...'
  @ojf.authorize(@account[:login], @account[:password])
  timestamps = last_timestamps
  puts 'Start looking for new jobs...'

  loop do
    jobs, timestamps = last_jobs(@queries, timestamps)
    jobs.each { |j| notify(j) }
    sleep(@interval)
  end
end

Private Instance Methods

fetch_last_jobs(query) click to toggle source
# File lib/odesk_jobnotifier.rb, line 55
def fetch_last_jobs(query)
  @ojf.fetch(query)
  rescue
    puts 'Error getting last jobs. Skipping...'
    []
end
filter_jobs(jobs, timestamp) click to toggle source
# File lib/odesk_jobnotifier.rb, line 62
def filter_jobs(jobs, timestamp)
  jobs.select do |j|
    DateTime.parse(j['publish_time']) > timestamp
  end
end
job_threads(queries) click to toggle source
# File lib/odesk_jobnotifier.rb, line 49
def job_threads(queries)
  queries.each_with_object([]) do |query, memo|
    memo << Thread.new { Thread.current[:jobs] = fetch_last_jobs(query) }
  end
end
last_job_timestamp(jobs) click to toggle source
# File lib/odesk_jobnotifier.rb, line 75
def last_job_timestamp(jobs)
  jobs.map { |j| DateTime.parse(j['publish_time']) }.max
end
last_jobs(queries, timestamps) click to toggle source
# File lib/odesk_jobnotifier.rb, line 36
def last_jobs(queries, timestamps)
  total_jobs = []
  threads = job_threads(queries)
  threads.each_with_index do |t, index|
    t.join
    jobs = filter_jobs(t[:jobs], timestamps[index])
    total_jobs += jobs
    timestamps[index] = last_job_timestamp(jobs) unless jobs.empty?
  end
  # Filter the same jobs that can be fetched by different queries.
  [total_jobs.uniq { |j| j['id'] }, timestamps]
end
last_timestamps() click to toggle source
# File lib/odesk_jobnotifier.rb, line 68
def last_timestamps
  @queries.map do |q|
    jobs = @ojf.fetch(q)
    last_job_timestamp(jobs)
  end
end
notify(job) click to toggle source
# File lib/odesk_jobnotifier.rb, line 79
def notify(job)
  params = {
    title: "$#{job['client']['total_spent']} | #{job['title']}",
    open: job['url']
  }

  TerminalNotifier.notify(job['snippet'], params)
end