diff --git a/.gitignore b/.gitignore index 2f491e3..4eae14e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .yardoc doc Gemfile.lock +tags diff --git a/lib/namely/collection.rb b/lib/namely/collection.rb index 3c93e53..0febaf5 100644 --- a/lib/namely/collection.rb +++ b/lib/namely/collection.rb @@ -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 diff --git a/lib/namely/connection.rb b/lib/namely/connection.rb index 68e0b6b..f20bca6 100644 --- a/lib/namely/connection.rb +++ b/lib/namely/connection.rb @@ -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. @@ -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 @@ -90,7 +102,7 @@ def gateway(endpoint, options = {}) ResourceGateway.new(options.merge( access_token: access_token, endpoint: endpoint, - subdomain: subdomain, + subdomain: subdomain )) end end diff --git a/lib/namely/resource_gateway.rb b/lib/namely/resource_gateway.rb index f578b51..cf9d967 100644 --- a/lib/namely/resource_gateway.rb +++ b/lib/namely/resource_gateway.rb @@ -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 @@ -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 @@ -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, @@ -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 diff --git a/namely.gemspec b/namely.gemspec index 3533b4b..5ad2cd7 100644 --- a/namely.gemspec +++ b/namely.gemspec @@ -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"