lib/chart_mogul/client.rb in chart_mogul-0.0.1 vs lib/chart_mogul/client.rb in chart_mogul-0.1.0

- old
+ new

@@ -28,18 +28,24 @@ builder.basic_auth(account_token, secret_key) builder.adapter Faraday.default_adapter end end + def credentials? + account_token && secret_key + end + def ping? response = connection.get("/v1/ping") preprocess_response(response)[:data] == 'pong!' end def paged_get(path, params, data_field) begin - result = preprocess_response(connection.get(path, params)) + params[:page_number] = 1 unless params[:page_number] + response = connection.get(path, params) + result = preprocess_response(response) yield result[data_field] params[:page_number] = result[:current_page] end while params[:page_number] < result[:total_pages] end @@ -49,26 +55,36 @@ JSON.parse(response.body, symbolize_names: true) when 401 raise UnauthorizedError.new when 422 result = JSON.parse(response.body, symbolize_names: true) - raise ValidationError.new(result[:errors]) + raise ValidationError.new(result) else puts response.inspect raise StandardError.new("#{response.status} #{response.body.slice(0,50)}") end end + def format_datetime(value) + value.strftime("%Y-%m-%d %H:%M:%S") + end + class UnauthorizedError < StandardError end class ValidationError < StandardError attr_reader :errors + attr_reader :body - def initialize(errors) - @errors = errors - super("validation errors for #{errors.keys.join(', ')}") + def initialize(result) + @body = result + if result[:errors] + @errors = result[:errors] + super("validation errors for #{@errors.keys.join(', ')}") + else + super("validation errors #{@body}") + end end end end end \ No newline at end of file