Class: ServiceResponse

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
TruncateToDbLimit
Defined in:
app/models/service_response.rb

Constant Summary collapse

MatchExact =

Constants for ‘match_reliability’ value.

'exact'
MatchUnsure =
'unsure'
@@built_in_fields =
[:display_text, :url, :notes, :response_key, :value_string, :value_alt_string, :value_text, :id]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TruncateToDbLimit

#do_truncate_to_db_limit!

Constructor Details

#initialize(*args) ⇒ ServiceResponse

MatchAltEdition = ‘edition’ MatchAltWork = ‘work’



186
187
188
189
# File 'app/models/service_response.rb', line 186

def initialize(*args)
  super
  self.service_data = {} unless self.service_data
end

Instance Attribute Details

#http_request_paramsObject

This value is not stored in db, but is set temporarily so the http request params can easily be passed around with a response object.



175
176
177
# File 'app/models/service_response.rb', line 175

def http_request_params
  @http_request_params
end

Class Method Details

.built_in_fieldsObject



268
269
270
# File 'app/models/service_response.rb', line 268

def self.built_in_fields
  @@built_in_fields
end

.create_from_hash(hash) ⇒ Object

Create from a hash of key/values, where some keys may be direct iVars, some may end up serialized in service_data, you don’t have to care, it will do the right thing.



194
195
196
197
198
# File 'app/models/service_response.rb', line 194

def self.create_from_hash(hash)
  r = ServiceResponse.new
  r.take_key_values(hash)
  return r
end

Instance Method Details

#serviceObject

Instantiates and returns a new Service associated with this response.



201
202
203
# File 'app/models/service_response.rb', line 201

def service
  @service ||= ServiceStore.instantiate_service!( self.service_id, nil )
end

#service_dataObject



205
206
207
208
209
210
211
212
213
# File 'app/models/service_response.rb', line 205

def service_data
  # Fix weird-ass char encoding bug with AR serialize and hashes.
  # https://github.com/rails/rails/issues/6538
  data = super
  if data.kind_of? Hash
    data.values.each {|v| v.force_encoding "UTF-8"  if v.respond_to? :force_encoding  }
  end
  return data
end

#service_type_valueObject



221
222
223
# File 'app/models/service_response.rb', line 221

def service_type_value
  ServiceTypeValue[self.service_type_value_name]
end

#service_type_value=(value) ⇒ Object

ServiceTypeValue object.



217
218
219
220
# File 'app/models/service_response.rb', line 217

def service_type_value=(value)
  value = ServiceTypeValue[value] unless value.kind_of?(ServiceTypeValue)        
  self.service_type_value_name = value.name   
end

#take_key_values(hash) ⇒ Object



227
228
229
230
231
232
233
234
235
236
# File 'app/models/service_response.rb', line 227

def take_key_values(hash)    
  hash.each_pair do |key, value|
    setter = "#{key.to_s}="      
    if ( self.respond_to?(setter))
      self.send(setter, value)
    else
      self.service_data[key] = value
    end
  end
end

#view_dataObject Also known as: data_values



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'app/models/service_response.rb', line 239

def view_data    
  unless (@data_values)  
    h = HashWithIndifferentAccess.new
    ServiceResponse.built_in_fields.each do |key|
      h[key] = self.send(key)
    end
    h.merge!(self.service_data.deep_dup)

    # add in service_type_value
    h[:service_type_value] = self.service_type_value_name

    # Handle requested i18n translations
    translate_simple_i18n!(h)

    # Optional additional transformation provided by service?
    if service.respond_to? :transform_view_data
      h = service.transform_view_data(h)
    end

    # Doesn't protect modifying nested structures, but
    # protects from some problems. 
    h.freeze 
    @data_values = h
  end
  return @data_values;
end