class Harvest::TimeLogger

Constants

BASE_URL
DEFAULT_USER_AGENT
WEB_DEVELOPMENT_TASK_ID

Attributes

config[R]

Public Class Methods

new(config) click to toggle source
# File lib/harvest/time_logger.rb, line 12
def initialize(config)
  @config = config
  config['task_id'] ||= WEB_DEVELOPMENT_TASK_ID
end

Public Instance Methods

log(task_title:, task_url: nil) click to toggle source
# File lib/harvest/time_logger.rb, line 32
def log(task_title:, task_url: nil)
  RestClient.post(
    "#{BASE_URL}/time_entries",
    time_log_payload(task_title, task_url).to_json,
    headers
  )
end
projects() click to toggle source
# File lib/harvest/time_logger.rb, line 17
def projects
  response = RestClient::Request.execute(
    method: :get,
    url: "#{BASE_URL}/users/me/project_assignments",
    headers: headers
  )
  
  JSON.parse(response.body)['project_assignments'].map do |project_assignment|
    {
      name: project_assignment['project']['name'],
      id: project_assignment['project']['id']
    }
  end
end

Private Instance Methods

headers() click to toggle source
# File lib/harvest/time_logger.rb, line 42
def headers
  {
    "Content-Type" => "application/json",
    "User-Agent"=> DEFAULT_USER_AGENT,
    "Authorization" => "Bearer #{config['access_token']}",
    "Harvest-Account-Id" => config['account_id']
  }
end
time_log_payload(task_title, task_url) click to toggle source
# File lib/harvest/time_logger.rb, line 51
def time_log_payload(task_title, task_url)
  {
    project_id: config['project_id'],
    task_id: config['task_id'],
    spent_date: Date.today.strftime('%Y-%m-%d'),
    notes: task_title,
    external_reference: {
      permalink: task_url
    }
  }
end