Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/harvesting/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ def delete(entity)
raise UnprocessableRequest(response.to_s) unless response.code.to_i == 200
end

private

def get(path, opts = {})
url = "#{DEFAULT_HOST}/#{path}"
url += "?#{opts.map {|k, v| "#{k}=#{v}"}.join("&")}" if opts.any?
Expand All @@ -90,6 +88,8 @@ def get(path, opts = {})
JSON.parse(response.body)
end

private

def http_response(method, uri, opts = {})
response = nil

Expand Down
13 changes: 13 additions & 0 deletions lib/harvesting/models/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ def to_hash
@attributes
end

def fetch
self.class.new(@harvest_client.get(path), client: @harvest_client)
end

# Retrieves an instance of the object by ID
#
# @param id [Integer] the id of the object to retrieve
# @param opts [Hash] options to pass along to the `Harvesting::Client`
# instance
def self.get(id, opts = {})
client = opts[:client] || Harvesting::Client.new(opts)
self.new({ 'id' => id }, opts).fetch
end
end
end
end
2 changes: 1 addition & 1 deletion lib/harvesting/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Client < HarvestRecord
:currency

def path
id.nil? ? "clients" : "clients/#{id}"
@attributes['id'].nil? ? "clients" : "clients/#{@attributes['id']}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/harvesting/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Contact < HarvestRecord
modeled client: Client

def path
id.nil? ? "contacts" : "contacts/#{id}"
@attributes['id'].nil? ? "contacts" : "contacts/#{@attributes['id']}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/harvesting/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Project < HarvestRecord
modeled client: Client

def path
id.nil? ? "projects" : "projects/#{id}"
@attributes['id'].nil? ? "projects" : "projects/#{@attributes['id']}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/harvesting/models/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Task < HarvestRecord
:updated_at

def path
id.nil? ? "tasks" : "tasks/#{id}"
@attributes['id'].nil? ? "tasks" : "tasks/#{@attributes['id']}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/harvesting/models/time_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TimeEntry < HarvestRecord


def path
id.nil? ? "time_entries" : "time_entries/#{id}"
@attributes['id'].nil? ? "time_entries" : "time_entries/#{@attributes['id']}"
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/harvesting/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class User < HarvestRecord
:updated_at

def path
id.nil? ? "users" : "users/#{id}"
@attributes['id'].nil? ? "users" : "users/#{@attributes['id']}"
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/harvesting/models/contact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
let(:attrs) { Hash.new }
let(:contact) { Harvesting::Models::Contact.new(attrs) }

include_context "harvest data setup"

describe '.new' do
context 'with client attributes in attrs' do
let(:contact_id) { '1235' }
Expand All @@ -18,4 +20,14 @@
end
end

describe '.get' do
it 'provides direct access to a specific contact' do
contact = Harvesting::Models::Contact.get(contact_jon_snow.id)
expect(contact.id).to eq(contact_jon_snow.id)
expect(contact.first_name).to eq(contact_jon_snow.first_name)
expect(contact.last_name).to eq(contact_jon_snow.last_name)
expect(contact.client.id).to eq(contact_jon_snow.client.id)
end
end

end