Class: Harvesting::Client

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

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.

Parameters:

  • opts


15
16
17
18
19
20
21
22
# File 'lib/harvesting/client.rb', line 15

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.



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

def access_token
  @access_token
end

#account_idObject

Returns the value of attribute account_id.



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

def 
  @account_id
end

Instance Method Details

#clientsObject



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

def clients
  get("clients")["clients"].map do |result|
    Harvesting::Models::Client.new(result, client: self)
  end
end

#contactsObject



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

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

#create(entity) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/harvesting/client.rb', line 52

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

#meObject



24
25
26
# File 'lib/harvesting/client.rb', line 24

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

#projects(opts = {}) ⇒ Object



44
45
46
# File 'lib/harvesting/client.rb', line 44

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

#tasks(opts = {}) ⇒ Object



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

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

#time_entries(opts = {}) ⇒ Object



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

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

#update(entity) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/harvesting/client.rb', line 60

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