Class: Harvesting::Client
- Inherits:
-
Object
- Object
- Harvesting::Client
- 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
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#account_id ⇒ Object
Returns the value of attribute account_id.
Instance Method Summary collapse
- #clients ⇒ Array<Harvesting::Models::Client>
- #contacts ⇒ Array<Harvesting::Models::Contact>
-
#create(entity) ⇒ Harvesting::Models::Base
Creates an ‘entity` in your Harvest account.
-
#delete(entity) ⇒ Hash
It removes an ‘entity` from your Harvest account.
-
#get(path, opts = {}) ⇒ Hash
Performs a GET request and returned the parsed JSON as a Hash.
-
#initialize(access_token: ENV['HARVEST_ACCESS_TOKEN'], account_id: ENV['HARVEST_ACCOUNT_ID']) ⇒ Client
constructor
Returns a new instance of ‘Client`.
- #invoices(opts = {}) ⇒ Array<Harvesting::Models::Invoice>
- #me ⇒ Harvesting::Models::User
- #projects(opts = {}) ⇒ Harvesting::Models::Projects
- #task_assignments(opts = {}) ⇒ Harvesting::Models::ProjectTaskAssignments
- #tasks(opts = {}) ⇒ Harvesting::Models::Tasks
- #time_entries(opts = {}) ⇒ Harvesting::Models::TimeEntries
-
#update(entity) ⇒ Harvesting::Models::Base
Updates an ‘entity` in your Harvest account.
- #user_assignments(opts = {}) ⇒ Harvesting::Models::ProjectUserAssignments
- #users(opts = {}) ⇒ Harvesting::Models::Users
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")
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 = 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_token ⇒ Object
Returns the value of attribute access_token.
11 12 13 |
# File 'lib/harvesting/client.rb', line 11 def access_token @access_token end |
#account_id ⇒ Object
Returns the value of attribute account_id.
11 12 13 |
# File 'lib/harvesting/client.rb', line 11 def account_id @account_id end |
Instance Method Details
#clients ⇒ Array<Harvesting::Models::Client>
35 36 37 38 39 |
# File 'lib/harvesting/client.rb', line 35 def clients get("clients")["clients"].map do |result| Harvesting::Models::Client.new(result, harvest_client: self) end end |
#contacts ⇒ Array<Harvesting::Models::Contact>
42 43 44 45 46 |
# File 'lib/harvesting/client.rb', line 42 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.
91 92 93 94 95 96 97 |
# File 'lib/harvesting/client.rb', line 91 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.
116 117 118 119 120 121 122 |
# File 'lib/harvesting/client.rb', line 116 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.
129 130 131 132 133 134 135 |
# File 'lib/harvesting/client.rb', line 129 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>
69 70 71 |
# File 'lib/harvesting/client.rb', line 69 def invoices(opts = {}) Harvesting::Models::Invoices.new(get("invoices", opts), opts, harvest_client: self) end |
#me ⇒ Harvesting::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
54 55 56 |
# File 'lib/harvesting/client.rb', line 54 def projects(opts = {}) Harvesting::Models::Projects.new(get("projects", opts), opts, harvest_client: self) end |
#task_assignments(opts = {}) ⇒ Harvesting::Models::ProjectTaskAssignments
81 82 83 84 85 |
# File 'lib/harvesting/client.rb', line 81 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
59 60 61 |
# File 'lib/harvesting/client.rb', line 59 def tasks(opts = {}) Harvesting::Models::Tasks.new(get("tasks", opts), opts, harvest_client: self) end |
#time_entries(opts = {}) ⇒ Harvesting::Models::TimeEntries
49 50 51 |
# File 'lib/harvesting/client.rb', line 49 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.
103 104 105 106 107 108 109 |
# File 'lib/harvesting/client.rb', line 103 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
74 75 76 77 78 |
# File 'lib/harvesting/client.rb', line 74 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
64 65 66 |
# File 'lib/harvesting/client.rb', line 64 def users(opts = {}) Harvesting::Models::Users.new(get("users", opts), opts, harvest_client: self) end |