Class: DTK::Common::Response

Inherits:
Hash
  • Object
show all
Includes:
ResponseTokens
Defined in:
lib/response.rb

Direct Known Subclasses

Error

Defined Under Namespace

Modules: ErrorMixin, Term Classes: Error, RestClientWrapper

Constant Summary

Constants included from ResponseTokens

DTK::Common::ResponseTokens::DataField, DTK::Common::ResponseTokens::ErrorsField, DTK::Common::ResponseTokens::ErrorsOriginalException, DTK::Common::ResponseTokens::ErrorsSubFieldCode, DTK::Common::ResponseTokens::GenericError, DTK::Common::ResponseTokens::StatusField, DTK::Common::ResponseTokens::StatusNotok, DTK::Common::ResponseTokens::StatusOK, DTK::Common::ResponseTokens::ValidationField

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Response

Returns a new instance of Response.



60
61
62
63
# File 'lib/response.rb', line 60

def initialize(hash={})
  super()
  replace(hash)
end

Instance Method Details

#add_data_value!(key, value) ⇒ Object



124
125
126
127
# File 'lib/response.rb', line 124

def add_data_value!(key,value)
  data()[key.to_s] = value
  self
end

#data(*data_keys) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/response.rb', line 84

def data(*data_keys)
  data = self[DataField]
  case data_keys.size
   when 0 then data||{}
   when 1 then data && data[internal_key_form(data_keys.first)]
   else data_keys.map{|key|data && data[internal_key_form(key)]}.compact
  end
end

#data_hash_form(*data_keys) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/response.rb', line 93

def data_hash_form(*data_keys)
  ret = Hash.new
  unless data = self[DataField]
    return ret
  end

  if data_keys.size == 0
    data.inject(Hash.new){|h,(k,v)|h.merge(external_key_form(k) => v)}
  else
    data_keys.each do |k|
      if v = data[internal_key_form(k)]
        ret.merge!(external_key_form(k) => v)
      end
    end
    ret
  end
end

#data_ret_and_remove!(*data_keys) ⇒ Object



119
120
121
122
# File 'lib/response.rb', line 119

def data_ret_and_remove!(*data_keys)
  data = data()
  data_keys.map{|key|data.delete(internal_key_form(key))}
end

#error_messageObject



76
77
78
# File 'lib/response.rb', line 76

def error_message
  self["errors"] ? (self["errors"].map { |e| e["message"]}).join(', ') : nil
end

#ok?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/response.rb', line 64

def ok?()
  self[StatusField] == StatusOK
end

#set_data(*data_values) ⇒ Object



111
112
113
# File 'lib/response.rb', line 111

def set_data(*data_values)
  self[DataField]=data_values
end

#set_data_hash(data_hash) ⇒ Object



115
116
117
# File 'lib/response.rb', line 115

def set_data_hash(data_hash)
  self[DataField]=data_hash
end

#validation_actionsObject



80
81
82
# File 'lib/response.rb', line 80

def validation_actions
  return self[ValidationField]['actions_needed']
end

#validation_messageObject



72
73
74
# File 'lib/response.rb', line 72

def validation_message
  self[ValidationField]['message']
end

#validation_response?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/response.rb', line 68

def validation_response?
  !self[ValidationField].nil?
end