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.

9 changes: 4 additions & 5 deletions lib/harvesting/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true

require "http"
require "json"

Expand Down Expand Up @@ -38,20 +37,20 @@ def contacts
end

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

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

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


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

def invoices
Expand Down
2 changes: 1 addition & 1 deletion lib/harvesting/models/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.modeled(opts = {})
opts.each do |attribute_name, model|
attribute_name_string = attribute_name.to_s
Harvesting::Models::Base.send :define_method, attribute_name_string do
@models[attribute_name_string] ||= model.new(@attributes[attribute_name_string] || {}, client: @client)
@models[attribute_name_string] ||= model.new(@attributes[attribute_name_string] || {}, client: harvest_client)
end
end
end
Expand Down
12 changes: 8 additions & 4 deletions lib/harvesting/models/projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class Projects < Base

attr_reader :entries

def initialize(attrs, opts = {})
def initialize(attrs, query_opts = {}, opts = {})
super(attrs.reject {|k,v| k == "projects" }, opts)
@query_opts = query_opts
@api_page = attrs
@entries = attrs["projects"].map do |entry|
Project.new(entry, client: opts[:client])
Expand All @@ -35,10 +36,13 @@ def size
total_entries
end

def next_page_query_opts
@query_opts.merge(page: page + 1)
end

def fetch_next_page
new_page = page + 1
@entries += harvest_client.projects(page: new_page).entries
@attributes['page'] = new_page
@entries += harvest_client.projects(next_page_query_opts).entries
@attributes['page'] = page + 1
end
end
end
Expand Down
12 changes: 8 additions & 4 deletions lib/harvesting/models/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class Tasks < Base

attr_reader :entries

def initialize(attrs, opts = {})
def initialize(attrs, query_opts = {}, opts = {})
super(attrs.reject {|k,v| k == "tasks" }, opts)
@query_opts = query_opts
@api_page = attrs
@entries = attrs["tasks"].map do |entry|
Task.new(entry, client: opts[:client])
Expand All @@ -35,10 +36,13 @@ def size
total_entries
end

def next_page_query_opts
@query_opts.merge(page: page + 1)
end

def fetch_next_page
new_page = page + 1
@entries += client.tasks(page: new_page).entries
@attributes['page'] = new_page
@entries += harvest_client.tasks(next_page_query_opts).entries
@attributes['page'] = page + 1
end
end
end
Expand Down
17 changes: 8 additions & 9 deletions lib/harvesting/models/time_entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ class TimeEntries < Base

attr_reader :entries

def initialize(attrs, opts = {})
def initialize(attrs, query_opts = {}, opts = {})
super(attrs.reject {|k,v| k == "time_entries" }, opts)
@query_opts = query_opts
@api_page = attrs
@entries = attrs["time_entries"].map do |entry|
TimeEntry.new(entry, client: opts[:client])
end
end

# def each
# @entries.each_with_index do |time_entry, index|
# yield(time_entry)
# end
# end
def page
@attributes['page']
end
Expand All @@ -35,10 +31,13 @@ def size
total_entries
end

def next_page_query_opts
@query_opts.merge(page: page + 1)
end

def fetch_next_page
new_page = page + 1
@entries += harvest_client.time_entries(page: new_page).entries
@attributes['page'] = new_page
@entries += harvest_client.time_entries(next_page_query_opts).entries
@attributes['page'] = page + 1
end
end
end
Expand Down
12 changes: 8 additions & 4 deletions lib/harvesting/models/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class Users < Base

attr_reader :entries

def initialize(attrs, opts = {})
def initialize(attrs, query_opts = {}, opts = {})
super(attrs.reject {|k,v| k == "users" }, opts)
@query_opts = query_opts
@api_page = attrs
@entries = attrs["users"].map do |entry|
User.new(entry, client: opts[:client])
Expand All @@ -30,10 +31,13 @@ def size
total_entries
end

def next_page_query_opts
@query_opts.merge(page: page + 1)
end

def fetch_next_page
new_page = page + 1
@entries += client.users(page: new_page).entries
@attributes['page'] = new_page
@entries += harvest_client.users(next_page_query_opts).entries
@attributes['page'] = page + 1
end
end
end
Expand Down
Loading