Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Users API #9

Merged
merged 2 commits into from
Oct 19, 2018
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
4 changes: 4 additions & 0 deletions lib/harvesting/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def tasks(opts = {})
Harvesting::Models::Tasks.new(get("tasks", opts), client: self)
end

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

def create(entity)
url = "#{DEFAULT_HOST}/#{entity.path}"
uri = URI(url)
Expand Down
4 changes: 4 additions & 0 deletions lib/harvesting/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class User < Base
:updated_at,
:roles,
:avatar_url

def path
id.nil? ? "users" : "users/#{id}"
end
end
end
end
40 changes: 40 additions & 0 deletions lib/harvesting/models/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Harvesting
module Models
class Users < Base
include Harvesting::Enumerable
extend Forwardable

attributed :is_active,
:total_pages,
:total_entries,
:next_page,
:previous_page,
:page,
:links

attr_reader :entries

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

def page
@attributes['page']
end

def size
total_entries
end

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