Sha256: b668532ed184886054b54deb87a78b0f529d2a83b3c5ca448fc123a6fdb9b9c1

Contents?: true

Size: 1.33 KB

Versions: 30

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module ZohoHub
  class Response
    def initialize(params)
      @params = params || {}
    end

    def invalid_data?
      if data.is_a?(Array)
        return data.first[:code] == 'MANDATORY_NOT_FOUND'
      end

      data[:code] == 'INVALID_DATA'
    end

    # {:code=>"INVALID_TOKEN", :details=>{}, :message=>"invalid oauth token", :status=>"error"}
    def invalid_token?
      return false if data.is_a?(Array)

      data[:code] == 'INVALID_TOKEN'
    end

    def authentication_failure?
      return false if data.is_a?(Array)

      data[:code] == 'AUTHENTICATION_FAILURE'
    end

    def empty?
      @params.empty?
    end

    def data
      data = @params[:data] if @params.dig(:data)
      data ||= @params

      return data.first if data.is_a?(Array) && data.size == 1

      data
    end

    def msg
      msg = data[:message]

      if data.dig(:code) == 'INVALID_DATA'
        msg << ", error in #{data.dig(:details, :api_name)}"
      end

      if data.dig(:details, :expected_data_type)
        expected = data.dig(:details, :expected_data_type)
        field = data.dig(:details, :api_name)
        parent_api_name = data.dig(:details, :parent_api_name)

        msg << ", expected #{expected} for '#{field}'"
        msg << " in #{parent_api_name}" if parent_api_name
      end

      msg
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
zoho_hub-0.1.47 lib/zoho_hub/response.rb
zoho_hub-0.1.46 lib/zoho_hub/response.rb
zoho_hub-0.1.45 lib/zoho_hub/response.rb
zoho_hub-0.1.44 lib/zoho_hub/response.rb
zoho_hub-0.1.43 lib/zoho_hub/response.rb
zoho_hub-0.1.42 lib/zoho_hub/response.rb
zoho_hub-0.1.41 lib/zoho_hub/response.rb
zoho_hub-0.1.40 lib/zoho_hub/response.rb
zoho_hub-0.1.39 lib/zoho_hub/response.rb
zoho_hub-0.1.38 lib/zoho_hub/response.rb
zoho_hub-0.1.37 lib/zoho_hub/response.rb
zoho_hub-0.1.36 lib/zoho_hub/response.rb
zoho_hub-0.1.35 lib/zoho_hub/response.rb
zoho_hub-0.1.34 lib/zoho_hub/response.rb
zoho_hub-0.1.33 lib/zoho_hub/response.rb
zoho_hub-0.1.32 lib/zoho_hub/response.rb
zoho_hub-0.1.30 lib/zoho_hub/response.rb
zoho_hub-0.1.29 lib/zoho_hub/response.rb
zoho_hub-0.1.28 lib/zoho_hub/response.rb
zoho_hub-0.1.27 lib/zoho_hub/response.rb