Class: Harvesting::Client

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

Overview

A client for the Harvest API (version 2.0)

Constant Summary collapse

DEFAULT_HOST =
"https://api.harvestapp.com/v2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token: ENV['HARVEST_ACCESS_TOKEN'], account_id: ENV['HARVEST_ACCOUNT_ID']) ⇒ Client

Returns a new instance of `Client`

client = Client.new(access_token: "12345678", account_id: "98764")

Parameters:

  • opts (Hash)

    the options to create an API client


20
21
22
23
24
25
26
27
# File 'lib/harvesting/client.rb', line 20

def initialize(access_token: ENV['HARVEST_ACCESS_TOKEN'], account_id: ENV['HARVEST_ACCOUNT_ID'])
  @access_token = access_token.to_s
  @account_id = .to_s

  if @account_id.length == 0 || @access_token.length == 0
    raise ArgumentError.new("Access token and account id are required. Access token: '#{@access_token}'. Account ID: '#{@account_id}'.")
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token


11
12
13
# File 'lib/harvesting/client.rb', line 11

def access_token
  @access_token
end

#account_idObject

Returns the value of attribute account_id


11
12
13
# File 'lib/harvesting/client.rb', line 11

def 
  @account_id
end

Instance Method Details

#clients(opts = {}) ⇒ Harvesting::Models::Clients


35
36
37
# File 'lib/harvesting/client.rb', line 35

def clients(opts = {})
  Harvesting::Models::Clients.new(get("clients", opts), opts, harvest_client: self)
end

#contactsArray<Harvesting::Models::Contact>

Returns:


40
41
42
43
44
# File 'lib/harvesting/client.rb', line 40

def contacts
  get("contacts")["contacts"].map do |result|
    Harvesting::Models::Contact.new(result, harvest_client: self)
  end
end

#create(entity) ⇒ Harvesting::Models::Base

Creates an `entity` in your Harvest account.

Parameters:

Returns:


89
90
91
92
93
94
95
# File 'lib/harvesting/client.rb', line 89

def create(entity)
  url = "#{DEFAULT_HOST}/#{entity.path}"
  uri = URI(url)
  response = http_response(:post, uri, body: entity.to_hash)
  entity.attributes = JSON.parse(response.body)
  entity
end

#delete(entity) ⇒ Hash

It removes an `entity` from your Harvest account.

Parameters:

Returns:

  • (Hash)

Raises:


114
115
116
117
118
119
120
# File 'lib/harvesting/client.rb', line 114

def delete(entity)
  url = "#{DEFAULT_HOST}/#{entity.path}"
  uri = URI(url)
  response = http_response(:delete, uri)
  raise UnprocessableRequest(response.to_s) unless response.code.to_i == 200
  JSON.parse(response.body)
end

#get(path, opts = {}) ⇒ Hash

Performs a GET request and returned the parsed JSON as a Hash.

Parameters:

  • path (String)

    path to be combined with `DEFAULT_HOST`

  • opts (Hash) (defaults to: {})

    key/values will get passed as HTTP (GET) parameters

Returns:

  • (Hash)

127
128
129
130
131
132
133
# File 'lib/harvesting/client.rb', line 127

def get(path, opts = {})
  url = "#{DEFAULT_HOST}/#{path}"
  url += "?#{opts.map {|k, v| "#{k}=#{v}"}.join("&")}" if opts.any?
  uri = URI(url)
  response = http_response(:get, uri)
  JSON.parse(response.body)
end

#invoices(opts = {}) ⇒ Array<Harvesting::Models::Invoice>

Returns:


67
68
69
# File 'lib/harvesting/client.rb', line 67

def invoices(opts = {})
  Harvesting::Models::Invoices.new(get("invoices", opts), opts, harvest_client: self)
end

#meHarvesting::Models::User


30
31
32
# File 'lib/harvesting/client.rb', line 30

def me
  Harvesting::Models::User.new(get("users/me"), harvest_client: self)
end

#projects(opts = {}) ⇒ Harvesting::Models::Projects


52
53
54
# File 'lib/harvesting/client.rb', line 52

def projects(opts = {})
  Harvesting::Models::Projects.new(get("projects", opts), opts, harvest_client: self)
end

#task_assignments(opts = {}) ⇒ Harvesting::Models::ProjectTaskAssignments


79
80
81
82
83
# File 'lib/harvesting/client.rb', line 79

def task_assignments(opts = {})
  project_id = opts.delete(:project_id)
  path = project_id.nil? ? "task_assignments" : "projects/#{project_id}/task_assignments"
  Harvesting::Models::ProjectTaskAssignments.new(get(path, opts), opts, harvest_client: self)
end

#tasks(opts = {}) ⇒ Harvesting::Models::Tasks


57
58
59
# File 'lib/harvesting/client.rb', line 57

def tasks(opts = {})
  Harvesting::Models::Tasks.new(get("tasks", opts), opts, harvest_client: self)
end

#time_entries(opts = {}) ⇒ Harvesting::Models::TimeEntries


47
48
49
# File 'lib/harvesting/client.rb', line 47

def time_entries(opts = {})
  Harvesting::Models::TimeEntries.new(get("time_entries", opts), opts, harvest_client: self)
end

#update(entity) ⇒ Harvesting::Models::Base

Updates an `entity` in your Harvest account.

Parameters:

Returns:


101
102
103
104
105
106
107
# File 'lib/harvesting/client.rb', line 101

def update(entity)
  url = "#{DEFAULT_HOST}/#{entity.path}"
  uri = URI(url)
  response = http_response(:patch, uri, body: entity.to_hash)
  entity.attributes = JSON.parse(response.body)
  entity
end

#user_assignments(opts = {}) ⇒ Harvesting::Models::ProjectUserAssignments


72
73
74
75
76
# File 'lib/harvesting/client.rb', line 72

def user_assignments(opts = {})
  project_id = opts.delete(:project_id)
  path = project_id.nil? ? "user_assignments" : "projects/#{project_id}/user_assignments"
  Harvesting::Models::ProjectUserAssignments.new(get(path, opts), opts, harvest_client: self)
end

#users(opts = {}) ⇒ Harvesting::Models::Users


62
63
64
# File 'lib/harvesting/client.rb', line 62

def users(opts = {})
  Harvesting::Models::Users.new(get("users", opts), opts, harvest_client: self)
end