Class: RouteNGNClient::Record

Inherits:
RemoteModel show all
Defined in:
lib/routengn_client/models/record.rb

Constant Summary

Constants inherited from RemoteModel

RouteNGNClient::RemoteModel::DEFAULT_RESOURCE_ATTRIBUTES

Instance Attribute Summary

Attributes included from RouteNGNClient::RemoteModel::ClassMethods

#resource_attributes

Attributes inherited from Model

#attributes, #request_account_id

Attributes included from Model::ClassMethods

#children, #only_children, #parents

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RouteNGNClient::RemoteModel::ClassMethods

#all, #collection_name, #find, #index_method, #path_base, #path_ext, #set_resource_attributes, #where

Methods inherited from Model

#to_hash, #to_json, #to_log

Methods included from Model::ClassMethods

#belongs_to, #from_hash, #from_json, #has_many, #has_one

Methods included from Logging

#init_logger, #logger

Constructor Details

#initialize(attributes = {}) ⇒ Record

Returns a new instance of Record.



12
13
14
# File 'lib/routengn_client/models/record.rb', line 12

def initialize(attributes = {})
  super
end

Class Method Details

.values_to_hash(headers, values) ⇒ Object



183
184
185
186
# File 'lib/routengn_client/models/record.rb', line 183

def self.values_to_hash(headers, values)
  values.each_with_index {| v,i| headers[i] = [headers[i].to_sym, v] }
  Hash[headers]
end

Instance Method Details

#add_response(r) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/routengn_client/models/record.rb', line 131

def add_response(r)
  return unless r

  self.attributes.responses ||= []
  self.attributes.responses << r
  self.append_log "Sending response code: #{(r.sip_response.blank? || r.sip_response.status_code.blank?) ? 'Unknown' : r.sip_response.status_code}"
  self.append_log "#{r.sip_response.data if r.sip_response}"
end

#add_route(route) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/routengn_client/models/record.rb', line 109

def add_route(route)
  return unless route

  self.append_log "Added route:"
  self.append_log route.to_log, :append_timestamp => false
  self.attributes.routes ||= []
  self.attributes.routes << route
end

#add_route_table(route_table) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/routengn_client/models/record.rb', line 118

def add_route_table(route_table)
  return unless route_table

  self.append_log "Using route_table:"
  self.append_log route_table.to_log, :append_timestamp => false
  self.attributes.route_tables ||= []
  self.attributes.route_tables << route_table
end

#append_log(s, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/routengn_client/models/record.rb', line 46

def append_log(s, options = {})
  append_timestamp = options.has_key?(:append_timestamp) ? options[:append_timestamp] : true

  self.attributes.logs ||= []

  if append_timestamp
    age = self.attributes.request ? self.attributes.request.age.round(5) : nil
    self.attributes.logs << "#{age}: #{s}"
  else
    self.attributes.logs << "#{s}"
  end
end

#as_cdr(delimiter = ',') ⇒ Object



179
180
181
# File 'lib/routengn_client/models/record.rb', line 179

def as_cdr(delimiter = ',')
  CSV.generate_line self.to_comma, :force_quotes => true, :col_sep => delimiter, :row_sep => nil
end

#cache_prep!Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/routengn_client/models/record.rb', line 168

def cache_prep!
  attrs = Hashie::Mash.new
  attrs.call_detail_record = self.call_detail_record_hash
  attrs. = self. || (self..id if self.)
  attrs.inbound_end_point_group_id = self.inbound_end_point_group.id if self.inbound_end_point_group
  attrs.requested_at_float = self.requested_at_float || (self.request.requested_at_float if self.request)
  attrs.response_code = self.final_response.response_code if self.final_response
  attrs.logs = self.logs
  self.attributes = attrs
end

#call_detail_recordObject



30
31
32
# File 'lib/routengn_client/models/record.rb', line 30

def call_detail_record
  self.attributes.call_detail_record
end

#call_detail_record_hashObject



34
35
36
# File 'lib/routengn_client/models/record.rb', line 34

def call_detail_record_hash
  self.class.values_to_hash(self.to_comma_headers, self.to_comma)
end

#create!(attributes = {}, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/routengn_client/models/record.rb', line 16

def create!(attributes = {}, options = {})
  options = options.merge({ :account_id => (self. || (self..id if self.)) })

  attrs = attributes.merge({
            :call_detail_record => (self.call_detail_record || self.call_detail_record_hash),
            :inbound_end_point_group_id => (self.inbound_end_point_group_id || (self.inbound_end_point_group.id if self.inbound_end_point_group)),
            :requested_at_float => (self.requested_at_float || (self.request.requested_at_float if self.request)),
            :response_code => (self.response_code || (self.final_response.response_code if self.final_response)),
            :logs => self.logs
          })

  self.class.create!(attrs, options)
end

#destination_codeObject



156
157
158
# File 'lib/routengn_client/models/record.rb', line 156

def destination_code
  self.attributes.destination_code
end

#final_responseObject



140
141
142
# File 'lib/routengn_client/models/record.rb', line 140

def final_response
  self.attributes.responses.blank? ? nil : self.attributes.responses.last
end

#final_route_tableObject



127
128
129
# File 'lib/routengn_client/models/record.rb', line 127

def final_route_table
  self.attributes.route_tables.blank? ? nil : self.attributes.route_tables.last
end

#inbound_rateObject



160
161
162
# File 'lib/routengn_client/models/record.rb', line 160

def inbound_rate
  self.attributes.inbound_rate
end

#origination_codeObject



152
153
154
# File 'lib/routengn_client/models/record.rb', line 152

def origination_code
  self.attributes.origination_code
end

#requestObject



144
145
146
# File 'lib/routengn_client/models/record.rb', line 144

def request
  self.attributes.request
end

#routes_csvs(delimiter = ';') ⇒ Object



38
39
40
# File 'lib/routengn_client/models/record.rb', line 38

def routes_csvs(delimiter = ';')
  (self.routes || []).map { |r| CSV.generate_line(r.to_comma, :force_quotes => true, :col_sep => delimiter, :quote_char => '\'').strip! }
end

#routes_string(delimiter = '|') ⇒ Object



42
43
44
# File 'lib/routengn_client/models/record.rb', line 42

def routes_string(delimiter = '|')
  self.routes_csvs.join(delimiter)
end

#set_destination_code(code) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/routengn_client/models/record.rb', line 94

def set_destination_code(code)
  return unless code

  self.append_log "Using destination_code:"
  self.append_log code.to_log, :append_timestamp => false
  self.attributes.destination_code = code
end

#set_inbound_end_point_group(iepg) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/routengn_client/models/record.rb', line 69

def set_inbound_end_point_group(iepg)
  return unless iepg

  self.append_log "Using inbound_end_point_group:"
  self.append_log iepg.to_log, :append_timestamp => false
  self.attributes.inbound_end_point_group = iepg
  self.attributes. = iepg. if iepg
end

#set_inbound_rate(rate) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/routengn_client/models/record.rb', line 78

def set_inbound_rate(rate)
  return unless rate

  self.append_log "Using inbound_rate:"
  self.append_log rate.to_log, :append_timestamp => false
  self.attributes.inbound_rate = rate
end

#set_origination_code(code) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/routengn_client/models/record.rb', line 86

def set_origination_code(code)
  return unless code

  self.append_log "Using origination_code:"
  self.append_log code.to_log, :append_timestamp => false
  self.attributes.origination_code = code
end

#set_rate_type(type) ⇒ Object



102
103
104
105
106
107
# File 'lib/routengn_client/models/record.rb', line 102

def set_rate_type(type)
  return unless type

  self.append_log "Using rate_type: #{type}"
  self.attributes.rate_type = type
end

#set_request(r) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/routengn_client/models/record.rb', line 59

def set_request(r)
  return unless r

  self.attributes.request = r
  self.requested_at_float = r.requested_at_float
  self.attributes.sip_request = r.sip_request
  self.append_log "Processing request: #{(r.sip_request.blank? || r.sip_request.method.blank?) ? 'Unknown' : r.sip_request.method} from #{r.peer_ip.blank? ? 'Unknown' : r.peer_ip}"
  self.append_log "#{r.sip_request.data if r.sip_request}"
end

#sip_redirector_versionObject



164
165
166
# File 'lib/routengn_client/models/record.rb', line 164

def sip_redirector_version
  self.attributes.sip_redirector_version
end

#sip_requestObject



148
149
150
# File 'lib/routengn_client/models/record.rb', line 148

def sip_request
  self.attributes.sip_request
end