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

Add code to query groups and group_types from API #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.yardoc
doc
Gemfile.lock
tags
6 changes: 6 additions & 0 deletions lib/namely/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def find(id)
raise NoSuchModelError, "Can't find any #{endpoint} with id \"#{id}\""
end

def find_from(id, find_from=nil)
resource_gateway.json_find_from(id, find_from).map { |model| build(model) }
rescue RestClient::ResourceNotFound
raise NoSuchModelError, "Can't find any #{endpoint} with id \"#{id}\""
end

private

attr_reader :resource_gateway
Expand Down
36 changes: 24 additions & 12 deletions lib/namely/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Connection
#
# @example
# Namely.configure do |config|
# config.access_token = "your_access_token"
# config.subdomain = "your-organization"
# config.access_token = 'your_access_token'
# config.subdomain = 'your-organization'
# end
#
# @raise [KeyError] if access_token and subdomain aren't provided.
Expand All @@ -19,63 +19,75 @@ def initialize(options)
@access_token = options.fetch(:access_token)
@subdomain = options.fetch(:subdomain)
rescue KeyError
raise ArgumentError, "Please supply an access_token and subdomain."
raise ArgumentError, 'Please supply an access_token and subdomain.'
end

# Return a Collection of countries.
#
# @return [Collection]
def countries
collection("countries")
collection('countries')
end

# Return a Collection of currency types.
#
# @return [Collection]
def currency_types
collection("currency_types")
collection('currency_types')
end

# Return a Collection of countries.
#
# @return [Collection]
def events
collection("events")
collection('events')
end

# Return a Collection of profile fields.
#
# @return [Collection]
def fields
collection("profiles/fields")
collection('profiles/fields')
end

# Return a Collection of job tiers.
#
# @return [Collection]
def job_tiers
collection("job_tiers")
collection('job_tiers')
end

# Return a Collection of job titles.
#
# @return [Collection]
def job_titles
collection("job_titles")
collection('job_titles')
end

# Return a Collection of profiles.
#
# @return [Collection]
def profiles
collection("profiles", paged: true)
collection('profiles', paged: true)
end

# Return a Collection of reports.
#
# @return [Collection]
def reports
collection("reports")
collection('reports')
end

def group_types
collection('group_types')
end

def groups
collection('groups', paged: true)
end

def groups_by_group_type(group_type_id)
group_types.find_from(group_type_id, 'groups')
end

private
Expand All @@ -90,7 +102,7 @@ def gateway(endpoint, options = {})
ResourceGateway.new(options.merge(
access_token: access_token,
endpoint: endpoint,
subdomain: subdomain,
subdomain: subdomain
))
end
end
Expand Down
12 changes: 10 additions & 2 deletions lib/namely/resource_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def json_index
paged ? json_index_paged : json_index_all
end

def json_find_from(id, find_from)
get("/#{endpoint}/#{id}/#{find_from}")[find_from]
end

def json_show(id)
get("/#{endpoint}/#{id}")[resource_name].first
end
Expand Down Expand Up @@ -40,16 +44,19 @@ def json_index_all
end

def json_index_paged
last_id = nil
Enumerator.new do |y|
params = {}

loop do
objects = with_retry { get("/#{endpoint}", params)[resource_name] }
break if objects.empty?
break if last_id == objects.last['id']

objects.each { |o| y << o }
last_id = objects.last['id']

params[:after] = objects.last["id"]
params[:after] = last_id
end
end
end
Expand All @@ -65,7 +72,7 @@ def url(path)
end

def extract_id(response)
JSON.parse(response)[endpoint].first["id"]
JSON.parse(response)[endpoint].first['id']
rescue StandardError => e
raise(
FailedRequestError,
Expand All @@ -78,6 +85,7 @@ def with_retry
yield
rescue RestClient::Exception => e
raise unless Namely.configuration.http_codes_to_retry.include?(e.http_code)

retry if (retries += 1) < Namely.configuration.retries
raise
end
Expand Down
2 changes: 1 addition & 1 deletion namely.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "rest-client"

spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "bundler"
spec.add_development_dependency "dotenv"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
Expand Down