From a0231ea2890169c752014dbe141026a2bdce3de4 Mon Sep 17 00:00:00 2001 From: Sixto Martin Date: Sat, 7 Jul 2018 19:32:55 +0200 Subject: [PATCH] Fix #4. Add to Cursor error and error_description attributes based on response --- lib/onelogin/api/client.rb | 16 +--------------- lib/onelogin/api/cursor.rb | 25 ++++++++++++++++++------- lib/onelogin/api/util.rb | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/lib/onelogin/api/client.rb b/lib/onelogin/api/client.rb index a412ceb..7c5f531 100644 --- a/lib/onelogin/api/client.rb +++ b/lib/onelogin/api/client.rb @@ -53,22 +53,8 @@ def clean_error @error_description = nil end - def extract_error_message_from_response(response) - message = '' - content = JSON.parse(response.body) - if content && content.has_key?('status') - status = content['status'] - if status.has_key?('message') - message = status['message'] - elsif status.has_key?('type') - message = status['type'] - end - end - message - end - def expired? - Time.now.utc > @expiration + (!@expiration.nil?) && Time.now.utc > @expiration end def prepare_token diff --git a/lib/onelogin/api/cursor.rb b/lib/onelogin/api/cursor.rb index e117a67..3a38612 100644 --- a/lib/onelogin/api/cursor.rb +++ b/lib/onelogin/api/cursor.rb @@ -5,6 +5,9 @@ # Returns an enumerable object class Cursor include Enumerable + include OneLogin::Api::Util + + attr_accessor :error, :error_description # Create a new instance of the Cursor. # @@ -14,6 +17,8 @@ class Cursor def initialize(url, options = {}) @url = url @options = options + @error = nil + @error_description = nil @model = options[:model] @headers = options[:headers] || {} @@ -55,16 +60,21 @@ def fetch_next_page query: @params ) - json = response.parsed_response - results = json['data'].flatten + if response.code == 200 + json = response.parsed_response + results = json['data'].flatten + + @collection += if results_remaining < results.size + results.slice(0, results_remaining) + else + results + end - @collection += if results_remaining < results.size - results.slice(0, results_remaining) + @after_cursor = after_cursor(json) else - results + @error = response.code.to_s + @error_description = extract_error_message_from_response(response) end - - @after_cursor = after_cursor(json) @last_cursor_empty = @after_cursor.nil? end @@ -87,4 +97,5 @@ def fetch_completed? def last? @last_cursor_empty || fetch_completed? end + end diff --git a/lib/onelogin/api/util.rb b/lib/onelogin/api/util.rb index 8240cea..b1016d4 100644 --- a/lib/onelogin/api/util.rb +++ b/lib/onelogin/api/util.rb @@ -6,6 +6,20 @@ module Api module Util include OneLogin::Api::Util::Constants include OneLogin::Api::Util::UrlBuilder + + def extract_error_message_from_response(response) + message = '' + content = JSON.parse(response.body) + if content && content.has_key?('status') + status = content['status'] + if status.has_key?('message') + message = status['message'] + elsif status.has_key?('type') + message = status['type'] + end + end + message + end end end end \ No newline at end of file