class Goal::FreshbooksCalculator

Attributes

project_id[R]
start_day[R]
token[R]
username[R]

Public Class Methods

new(freshbooks_username, token, project_id, start_day) click to toggle source
# File lib/goal/freshbooks_calculator.rb, line 5
def initialize(freshbooks_username, token, project_id, start_day)
  @username   = freshbooks_username
  @token      = token
  @project_id = project_id
  @start_day  = start_day
end

Public Instance Methods

hours() click to toggle source
# File lib/goal/freshbooks_calculator.rb, line 12
def hours
  entries_for_month.inject(0) { |sum, entry| sum + entry['hours'].to_f }
end

Private Instance Methods

client_url() click to toggle source
# File lib/goal/freshbooks_calculator.rb, line 46
def client_url
  "#{username}.freshbooks.com"
end
connection() click to toggle source
# File lib/goal/freshbooks_calculator.rb, line 50
def connection
  @connection ||= FreshBooks::Client.new(client_url, token)
end
date_from() click to toggle source
# File lib/goal/freshbooks_calculator.rb, line 34
def date_from
  Date.new Date.today.year, Date.today.month, start_day
end
date_to() click to toggle source
# File lib/goal/freshbooks_calculator.rb, line 38
def date_to
  date_from + 30
end
entries_for_month() click to toggle source
# File lib/goal/freshbooks_calculator.rb, line 20
def entries_for_month
  entries = connection.time_entry.list(date_from: date_from, date_to: date_to, project_id: project_id, per_page: per_page)

  if entries.has_key?('time_entries') &&entries['time_entries'].has_key?('time_entry')
    if entries['time_entries']['time_entry'].is_a?(Array)
      entries['time_entries']['time_entry']
    else
      [] << entries['time_entries']['time_entry']
    end
  else
    []
  end
end
per_page() click to toggle source
# File lib/goal/freshbooks_calculator.rb, line 42
def per_page
  1000
end