Class: ZohoHub::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/zoho_hub/response.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Response

Returns a new instance of Response.



5
6
7
# File 'lib/zoho_hub/response.rb', line 5

def initialize(params)
  @params = params || {}
end

Instance Method Details

#authentication_failure?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/zoho_hub/response.rb', line 21

def authentication_failure?
  error_code?('AUTHENTICATION_FAILURE')
end

#dataObject



45
46
47
48
# File 'lib/zoho_hub/response.rb', line 45

def data
  data = @params[:data] if @params[:data]
  data || @params
end

#empty?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/zoho_hub/response.rb', line 41

def empty?
  @params.empty?
end

#error_code?(code) ⇒ Boolean

Error response examples: “data”:[{“code”:“INVALID_DATA”,“details”:{,“message”:“the id given…”,“status”:“error”}]} :details=>{, :message=>“invalid oauth token”, :status=>“error”}

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
# File 'lib/zoho_hub/response.rb', line 69

def error_code?(code)
  if data.is_a?(Array)
    return false if data.size > 1

    return data.first[:code] == code
  end

  data[:code] == code
end

#internal_error?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/zoho_hub/response.rb', line 17

def internal_error?
  error_code?('INTERNAL_ERROR')
end

#invalid_data?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/zoho_hub/response.rb', line 9

def invalid_data?
  error_code?('INVALID_DATA')
end

#invalid_module?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/zoho_hub/response.rb', line 25

def invalid_module?
  error_code?('INVALID_MODULE')
end

#invalid_token?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/zoho_hub/response.rb', line 13

def invalid_token?
  error_code?('INVALID_TOKEN')
end

#mandatory_not_found?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/zoho_hub/response.rb', line 33

def mandatory_not_found?
  error_code?('MANDATORY_NOT_FOUND')
end

#msgObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/zoho_hub/response.rb', line 50

def msg
  first_data = data.is_a?(Array) ? data.first : data
  msg = first_data[:message]

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

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

  msg
end

#no_permission?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/zoho_hub/response.rb', line 29

def no_permission?
  error_code?('NO_PERMISSION')
end

#record_in_blueprint?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/zoho_hub/response.rb', line 37

def record_in_blueprint?
  error_code?('RECORD_IN_BLUEPRINT')
end