routengn-client 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +34 -0
  7. data/Rakefile +1 -0
  8. data/lib/routengn_client/connection.rb +77 -0
  9. data/lib/routengn_client/exceptions.rb +8 -0
  10. data/lib/routengn_client/logging.rb +23 -0
  11. data/lib/routengn_client/middleware/routengn_response_middleware.rb +23 -0
  12. data/lib/routengn_client/model.rb +177 -0
  13. data/lib/routengn_client/models/account.rb +23 -0
  14. data/lib/routengn_client/models/allow_list.rb +38 -0
  15. data/lib/routengn_client/models/carrier.rb +14 -0
  16. data/lib/routengn_client/models/code.rb +32 -0
  17. data/lib/routengn_client/models/code_sheet.rb +11 -0
  18. data/lib/routengn_client/models/contact.rb +124 -0
  19. data/lib/routengn_client/models/end_point.rb +18 -0
  20. data/lib/routengn_client/models/inbound_end_point_group.rb +70 -0
  21. data/lib/routengn_client/models/inbound_rate.rb +13 -0
  22. data/lib/routengn_client/models/outbound_end_point_group.rb +51 -0
  23. data/lib/routengn_client/models/outbound_rate.rb +13 -0
  24. data/lib/routengn_client/models/rate_sheet.rb +13 -0
  25. data/lib/routengn_client/models/record.rb +231 -0
  26. data/lib/routengn_client/models/route.rb +42 -0
  27. data/lib/routengn_client/models/route_block.rb +7 -0
  28. data/lib/routengn_client/models/route_container.rb +18 -0
  29. data/lib/routengn_client/models/route_ordering_override.rb +7 -0
  30. data/lib/routengn_client/models/route_table.rb +30 -0
  31. data/lib/routengn_client/models/system_admin_account.rb +5 -0
  32. data/lib/routengn_client/models/uri_matcher.rb +72 -0
  33. data/lib/routengn_client/models/user.rb +20 -0
  34. data/lib/routengn_client/remote_model.rb +69 -0
  35. data/lib/routengn_client/request.rb +34 -0
  36. data/lib/routengn_client/response.rb +108 -0
  37. data/lib/routengn_client/utils.rb +20 -0
  38. data/lib/routengn_client/version.rb +3 -0
  39. data/lib/routengn_client.rb +72 -0
  40. data/routengn-client.gemspec +37 -0
  41. data/spec/lib/routengn_client/connection_spec.rb +44 -0
  42. data/spec/lib/routengn_client/logging_spec.rb +9 -0
  43. data/spec/lib/routengn_client/model_spec.rb +82 -0
  44. data/spec/lib/routengn_client/models/account_spec.rb +28 -0
  45. data/spec/lib/routengn_client/models/carrier_spec.rb +20 -0
  46. data/spec/lib/routengn_client/models/code_spec.rb +32 -0
  47. data/spec/lib/routengn_client/models/contact_spec.rb +57 -0
  48. data/spec/lib/routengn_client/models/inbound_end_point_group_spec.rb +33 -0
  49. data/spec/lib/routengn_client/models/inbound_rate_spec.rb +10 -0
  50. data/spec/lib/routengn_client/models/outbound_end_point_group_spec.rb +52 -0
  51. data/spec/lib/routengn_client/models/outbound_rate_spec.rb +10 -0
  52. data/spec/lib/routengn_client/models/rate_sheet_spec.rb +0 -0
  53. data/spec/lib/routengn_client/models/record_spec.rb +59 -0
  54. data/spec/lib/routengn_client/models/route_container_spec.rb +10 -0
  55. data/spec/lib/routengn_client/models/route_spec.rb +228 -0
  56. data/spec/lib/routengn_client/models/route_table_spec.rb +87 -0
  57. data/spec/lib/routengn_client/remote_model_spec.rb +60 -0
  58. data/spec/lib/routengn_client/request_spec.rb +14 -0
  59. data/spec/lib/routengn_client/response_spec.rb +29 -0
  60. data/spec/lib/routengn_client_spec.rb +5 -0
  61. data/spec/spec_helper.rb +34 -0
  62. data/spec/support/blueprints.rb +157 -0
  63. metadata +351 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 806113a37cf70dcb69d16ae7855f7d0bb002aa17
4
+ data.tar.gz: 4e958c370005c926a951c4f6618b0a07421d209f
5
+ SHA512:
6
+ metadata.gz: 1ff754329b66454609e84401e491a934b8a790d35aab88a68be7f74dc01b3fd21384e9fcf64b0f22516933020a7358da2189cc935b85664f1c0448dccd52f09c
7
+ data.tar.gz: 025fb4be299d2efc8055934a7941da4b9aa85808d2eb67e49f1d573803c68f3e397cda6c85ad02165b511142439f2057751fbe70174a1178bc8f3d27cccc5e84
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in routengn-client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # RouteNGNClient
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'routengn-client'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install routengn-client
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ MultiJson.use :yajl # set your prefered JSON library
21
+ Faraday.default_adapter = :excon # set your preferred HTTP adapter
22
+
23
+ RouteNGNClient.connect :uri => 'http://web.clusterx.routengn.com', :api_key => 'yourkey' # connect to RouteNGN
24
+
25
+ carriers = Carrier.all # profit!
26
+ ```
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,77 @@
1
+ module RouteNGNClient
2
+ class Connection
3
+
4
+ USER_AGENT = "routengn-client v#{RouteNGNClient::VERSION}"
5
+
6
+ attr_accessor :options, :faraday
7
+
8
+ def initialize(options = {})
9
+ @options = options
10
+ @faraday = self.create_faraday(@options[:uri], @options[:api_key]) if @options[:uri]
11
+ end
12
+
13
+ def create_faraday(uri, api_key = nil)
14
+ @faraday = Faraday.new uri do |c|
15
+ c.headers['User-Agent'] = USER_AGENT
16
+
17
+ c.request :oauth2, api_key, :param_name => 'api_key' if api_key
18
+ c.request :multipart
19
+ c.request :url_encoded
20
+ c.request :multi_json
21
+
22
+ c.response :xml, :content_type => /\bxml$/
23
+ c.response :multi_json, symbolize_keys: true, :content_type => /\bjson$/
24
+ c.response :routengn_response
25
+ c.response :logger, RouteNGNClient.logger
26
+
27
+ c.use :instrumentation
28
+ c.adapter Faraday.default_adapter
29
+ end
30
+ end
31
+
32
+ def request(method, path, params, options, &callback)
33
+ sent_at = nil
34
+
35
+ response = @faraday.send(method) { |request|
36
+ sent_at = Time.now
37
+ request = config_request(request, method, path, params, options)
38
+ }.on_complete { |env|
39
+ env[:total_time] = Time.now.utc.to_f - sent_at.utc.to_f if sent_at
40
+ env[:request_params] = params
41
+ env[:request_options] = options
42
+ callback.call(env) if callback
43
+ }
44
+
45
+ response
46
+ end
47
+
48
+ def config_request(request, method, path, params, options)
49
+ case method.to_sym
50
+ when :delete, :get
51
+ request.url(path, params)
52
+ when :post, :put
53
+ request.path = path
54
+ request.body = params unless params.empty?
55
+ end
56
+
57
+ request
58
+ end
59
+
60
+ def get(path, params={}, options={}, &callback)
61
+ request(:get, path, params, options, &callback)
62
+ end
63
+
64
+ def delete(path, params={}, options={}, &callback)
65
+ request(:delete, path, params, options, &callback)
66
+ end
67
+
68
+ def post(path, params={}, options={}, &callback)
69
+ request(:post, path, params, options, &callback)
70
+ end
71
+
72
+ def put(path, params={}, options={}, &callback)
73
+ request(:put, path, params, options, &callback)
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,8 @@
1
+ module RouteNGNClient
2
+ class RateNGNClientException < StandardError; end
3
+ class UnexpectedHTTPException < RateNGNClientException; end
4
+ class PermissionDeniedException < RateNGNClientException; end
5
+ class NonExistentRecord < RateNGNClientException; end
6
+ class RateNGNClientConnectionException < RateNGNClientException; end
7
+ class RouteNGNResponseException < RateNGNClientException; end
8
+ end
@@ -0,0 +1,23 @@
1
+ module RouteNGNClient
2
+ module Logging
3
+ def logger
4
+ @logger ||= init_logger(STDOUT, :debug)
5
+ end
6
+
7
+ def init_logger(io, level)
8
+ logger = unless io
9
+ Logger.new(STDOUT)
10
+ else
11
+ Logger.new(io)
12
+ end
13
+
14
+ if level == :debug
15
+ logger.level = Logger::DEBUG
16
+ else
17
+ logger.level = Logger::INFO
18
+ end
19
+
20
+ logger
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module RouteNGNClient
2
+ class RouteNGNResponseMiddleware < Faraday::Response::Middleware
3
+ include RouteNGNClient::Logging
4
+
5
+ ERROR_STATUSES = (400...600)
6
+
7
+ def on_complete(env)
8
+ case env[:status]
9
+ when 401
10
+ raise PermissionDeniedException
11
+ when 404
12
+ raise NonExistentRecord
13
+ when 0
14
+ raise UnexpectedHTTPException, "recieved an unexpected HTTP response code #{env[:status]}"
15
+ when ERROR_STATUSES
16
+ raise UnexpectedHTTPException, "recieved an unexpected HTTP response code #{env[:status]}"
17
+ end
18
+
19
+ env
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,177 @@
1
+ module RouteNGNClient
2
+ class Model
3
+ include Logging
4
+
5
+ attr_accessor :attributes, :request_account_id
6
+
7
+ def initialize(attributes = {})
8
+ @attributes = OpenStruct.new(attributes)
9
+ @request_account_id = nil
10
+ self.create_parent_accessors
11
+ self.create_child_accessors
12
+ self.create_only_child_accessors
13
+ end
14
+
15
+ def to_hash
16
+ @attributes.marshal_dump
17
+ end
18
+
19
+ def to_json(pretty = false)
20
+ MultiJson.dump(self.to_hash, :pretty => pretty)
21
+ end
22
+
23
+ def to_log(options = {})
24
+ self.to_json(true)
25
+ end
26
+
27
+ module ClassMethods
28
+
29
+ attr_accessor :parents
30
+ attr_accessor :children
31
+ attr_accessor :only_children
32
+
33
+ def from_hash(h)
34
+ new h
35
+ end
36
+
37
+ def from_json(s)
38
+ from_hash MultiJson.load(s, :symbolize_keys => true)
39
+ end
40
+
41
+ def belongs_to(*args)
42
+ @parents = args.collect do |arg|
43
+ "RouteNGNClient::#{arg.to_s.classify}".constantize
44
+ end
45
+ end
46
+
47
+ def has_many(*args)
48
+ @children = args.collect do |arg|
49
+ "RouteNGNClient::#{arg.to_s.classify}".constantize
50
+ end
51
+ end
52
+
53
+ def has_one(*args)
54
+ @only_children = args.collect do |arg|
55
+ "RouteNGNClient::#{arg.to_s.classify}".constantize
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ extend ClassMethods
62
+
63
+ protected
64
+
65
+ def create_only_child_accessors
66
+ return if self.class.only_children.blank?
67
+
68
+ p = self.class.to_s.demodulize.underscore.singularize.to_sym
69
+ instance = self
70
+
71
+ self.class.only_children.each do |klass|
72
+ c = klass.to_s.demodulize.underscore.singularize.to_sym
73
+ instance_var = "@#{klass.to_s.demodulize.underscore.singularize}".to_sym
74
+
75
+ (class << self; self; end).class_eval do
76
+ define_method(c) do |options = {}|
77
+ if model = instance_variable_get(instance_var)
78
+ return model
79
+ else
80
+ if attributes.send(c).is_a?(klass)
81
+ model = attributes.send(c)
82
+ elsif attributes.send(c).kind_of?(Hash)
83
+ model = klass.send :from_hash, attributes.send(c)
84
+ else
85
+ child_id = attributes.send("#{c.to_s}_id".to_sym)
86
+ model = klass.send(:find, child_id, :account_id => instance.request_account_id) unless options[:no_fetch] || !child_id
87
+ end
88
+
89
+ return unless model
90
+
91
+ model.request_account_id = instance.request_account_id
92
+
93
+ instance_variable_set(instance_var, model)
94
+
95
+ model
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ def create_child_accessors
103
+ return if self.class.children.blank?
104
+
105
+ p = self.class.to_s.demodulize.underscore.singularize.to_sym
106
+ instance = self
107
+
108
+ self.class.children.each do |klass|
109
+ c = klass.to_s.demodulize.underscore.pluralize.to_sym
110
+ instance_var = "@#{klass.to_s.demodulize.underscore.pluralize}".to_sym
111
+
112
+ (class << self; self; end).class_eval do
113
+ define_method(c) do |options = {}|
114
+ if models = instance_variable_get(instance_var)
115
+ return models
116
+ else
117
+ collection = attributes.send(c) || []
118
+
119
+ models = collection.collect do |h|
120
+ if h.kind_of?(Hash) && h[c.to_s.singularize.to_sym]
121
+ model = klass.send :from_hash, h[c.to_s.singularize.to_sym]
122
+ elsif h.kind_of?(Hash)
123
+ model = klass.send :from_hash, h
124
+ else
125
+ model = h
126
+ end
127
+
128
+ model.request_account_id = instance.request_account_id
129
+
130
+ model
131
+ end
132
+
133
+ instance_variable_set(instance_var, models)
134
+
135
+ models
136
+ end
137
+ end
138
+ end
139
+ end
140
+
141
+ end
142
+
143
+ def create_parent_accessors
144
+ return if self.class.parents.blank?
145
+
146
+ self.class.parents.each do |klass|
147
+ p = klass.to_s.demodulize.underscore.singularize.to_sym
148
+ instance_var = "@#{klass.to_s.demodulize.underscore.singularize}".to_sym
149
+ instance = self
150
+
151
+ (class << self; self; end).class_eval do
152
+ define_method(p) do |options = {}|
153
+ if model = instance_variable_get(instance_var)
154
+ return model
155
+ else
156
+ if v = attributes.send(p)
157
+ model = v.kind_of?(Hash) ? klass.send(:from_hash, v) : v
158
+ model.request_account_id = instance.request_account_id
159
+ instance_variable_set(instance_var, model)
160
+ model
161
+ elsif !(id = attributes.send("#{p.to_s}_id".to_sym)).blank?
162
+ model = attributes.send(p) if attributes.send(p).is_a?(klass)
163
+
164
+ model = klass.send(:find, id, :account_id => instance.request_account_id) unless model || options[:no_fetch]
165
+
166
+ instance_variable_set(instance_var, model)
167
+
168
+ model
169
+ end
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end
175
+
176
+ end
177
+ end
@@ -0,0 +1,23 @@
1
+ module RouteNGNClient
2
+ class Account < RemoteModel
3
+
4
+ set_resource_attributes({
5
+ :path_base => 'system_admin/accounts',
6
+ :collection_name => 'accounts',
7
+ :per_page => 50,
8
+ :request_options => {
9
+ }
10
+ })
11
+
12
+ has_many :users, :rate_sheets, :code_sheets, :carriers
13
+
14
+ attr_accessor :inbound_end_point_groups
15
+
16
+ def initialize(attributes = {})
17
+ super
18
+
19
+ @inbound_end_point_groups ||= []
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ module RouteNGNClient
2
+ class AllowList < Model
3
+
4
+ belongs_to :inbound_end_point_group
5
+
6
+ def initialize(attributes = {})
7
+ super attributes
8
+
9
+ @attributes.uri_matchers = (@attributes.uri_matchers || []).collect { |h| RouteNGNClient::URIMatcher.from_hash h }
10
+ self.order_uri_matchers!
11
+ end
12
+
13
+ def order_uri_matchers!
14
+ @attributes.uri_matchers.sort!
15
+ end
16
+
17
+ def best_uri_match(header, uri)
18
+ return nil unless header && uri && @attributes.uri_matchers.find { |uri_matcher| uri_matcher.matches_header? header }
19
+
20
+ prefix = uri.user.prefix(self.inbound_end_point_group.dialcode_prefix_regexp) unless uri.user.blank?
21
+ user_params = uri.user.params.collect { |k,v| "#{k}=#{v}" } unless uri.user.params.blank?
22
+ uri_params = uri.params.collect { |k,v| "#{k}=#{v}" } unless uri.params.blank?
23
+
24
+ h = {}.with_indifferent_access
25
+ h[:header] = header
26
+ h[:prefix] = prefix unless prefix.blank?
27
+ h[:user_params] = user_params unless user_params.blank?
28
+ h[:uri_params] = uri_params unless uri_params.blank?
29
+
30
+ @attributes.uri_matchers.each do |uri_matcher|
31
+ return uri_matcher if uri_matcher.matches? h
32
+ end
33
+
34
+ nil
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ module RouteNGNClient
2
+ class Carrier < RemoteModel
3
+
4
+ set_resource_attributes({
5
+ :path_base => 'metallic/carriers',
6
+ :collection_name => 'carriers',
7
+ :per_page => 25
8
+ })
9
+
10
+ belongs_to :account
11
+ has_many :outbound_end_point_groups, :inbound_end_point_groups
12
+
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ module RouteNGNClient
2
+ class Code < RemoteModel
3
+
4
+ set_resource_attributes({
5
+ :path_base => 'metallic/codes',
6
+ :collection_name => 'codes',
7
+ :per_page => 200,
8
+ :request_options => {
9
+ }
10
+ })
11
+
12
+ class << self
13
+
14
+ def find_by_exploded_dialcode(dialcode, options = {}, &callback)
15
+ subsets = Utils.string_subsets dialcode
16
+ find_by_dialcodes subsets, options, &callback
17
+ end
18
+
19
+ def find_by_dialcodes(dialcodes, options = {}, &callback)
20
+ options = {:page => 1, :per_page => (dialcodes.length + 1)}.merge(self.resource_attributes[:request_options]).merge(options)
21
+
22
+ models = all({:code => {:dialcode => {'$in' => dialcodes}}}, options)
23
+
24
+ callback.call(models) if callback
25
+
26
+ models
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ module RouteNGNClient
2
+ class CodeSheet < RemoteModel
3
+
4
+ set_resource_attributes({
5
+ :collection_name => 'code_sheets',
6
+ :per_page => 25
7
+ })
8
+ belongs_to :account
9
+
10
+ end
11
+ end
@@ -0,0 +1,124 @@
1
+ module RouteNGNClient
2
+ class Contact < Model
3
+
4
+ def route
5
+ @attributes.route
6
+ end
7
+
8
+ def end_point
9
+ @attributes.end_point
10
+ end
11
+
12
+ def inbound_rate
13
+ self.route ? self.route.inbound_rate : nil
14
+ end
15
+
16
+ def outbound_rate
17
+ self.route ? self.route.outbound_rate : nil
18
+ end
19
+
20
+ def outbound_end_point_group
21
+ self.route.outbound_end_point_group if self.route
22
+ end
23
+
24
+ def aor
25
+ @attributes.aor
26
+ end
27
+
28
+ def build_aor(sip_request, inbound_end_point_group, options = {})
29
+ options = options.with_indifferent_access
30
+
31
+ uri = self.build_uri(sip_request)
32
+
33
+ template = Template.new(inbound_end_point_group.attributes.contact_template)
34
+ template.sub!(self, uri, :inbound_end_point_host => options[:inbound_end_point_host], :inbound_end_point_port => options[:inbound_end_point_port])
35
+
36
+ @attributes.aor = template.contact_template
37
+ end
38
+
39
+ def build_uri(sip_request, options = {})
40
+ options = options.with_indifferent_access
41
+
42
+ request_uri = sip_request.request_uri
43
+ sip_uri = request_uri.clone
44
+ oepg = self.outbound_end_point_group
45
+ oepg_parts = oepg ? oepg.build_uri_parts : {}
46
+ oep_parts = self.end_point ? self.end_point.build_uri_parts : {}
47
+
48
+ dialcode = oepg && oepg.attributes.dialcode_expression ? sip_uri.user.dialcode(oepg.dialcode_regexp) : sip_uri.user.user
49
+ dialcode = oepg.attributes.dialcode_template.gsub("${dialcode_match}", dialcode) if dialcode && oepg && oepg.attributes.dialcode_template
50
+
51
+ blacklisted_user_params = oepg.attributes.uri_user_params_blacklist || [] if oepg
52
+ blacklisted_params = oepg.attributes.uri_params_blacklist || [] if oepg
53
+ merge_user_params, merge_params = {}, {}
54
+
55
+ merge_user_params['rn'] = sip_request.rn if sip_request.rn && !sip_request.request_uri.params.keys.include?('rn') && !sip_request.request_uri.params.keys.include?('rn')
56
+ merge_user_params['npdi'] = sip_request.npdi if sip_request.npdi && !sip_request.request_uri.params.keys.include?('npdi') && !sip_request.request_uri.params.keys.include?('npdi')
57
+ merge_user_params.merge! oepg_parts[:uri_user_params]
58
+ merge_params['rn'] = sip_request.rn if sip_request.rn && !merge_user_params.has_key?('rn')
59
+ merge_params['npdi'] = sip_request.npdi if sip_request.npdi && !merge_user_params.has_key?('npdi')
60
+ merge_params.merge! oepg_parts[:uri_params]
61
+
62
+ sip_uri.regenerate(:user => {
63
+ :user => dialcode,
64
+ :merge_params => merge_user_params,
65
+ :blacklisted_params => blacklisted_user_params
66
+ },
67
+ :host => oep_parts[:host],
68
+ :port => oep_parts[:port],
69
+ :merge_params => merge_params,
70
+ :blacklisted_params => blacklisted_params)
71
+ end
72
+
73
+ class Template
74
+
75
+ attr_accessor :contact_template
76
+
77
+ def initialize(contact_template)
78
+ @contact_template = contact_template.clone
79
+ end
80
+
81
+ def sub!(contact, sip_uri, options = {})
82
+ options = options.with_indifferent_access
83
+
84
+ iep_host = options[:inbound_end_point_host]
85
+ iep_port = options[:inbound_end_point_port]
86
+
87
+ #@contact_template.gsub!("${oepg.prefix}", '') # legacy as of 10/24/12
88
+ @contact_template.gsub!("${ruri.dialcode}", sip_uri.user.user_string)
89
+ @contact_template.gsub!("${ruri.user_params}", !sip_uri.user.params.blank? ? ";#{sip_uri.user.params_string}" : '')
90
+ @contact_template.gsub!("${ruri.params}", !sip_uri.params.blank? ? ";#{sip_uri.params_string}" : '')
91
+ @contact_template.gsub!("${iep.host}", !iep_host.blank? ? iep_host : '')
92
+ @contact_template.gsub!("${iep.port}", !iep_port.blank? ? ":#{iep_port}" : '')
93
+ @contact_template.gsub!("${oep.host}", sip_uri.host)
94
+ @contact_template.gsub!("${oep.port}", ":#{sip_uri.port}")
95
+ @contact_template.gsub!("${oepg.prefix}", '') # legacy as of 10/24/12
96
+ @contact_template.gsub!("${oepg.uri_user_params}", '') #legacy as of 3/29/13 (params already merged in)
97
+ @contact_template.gsub!("${oepg.uri_params}", '') #legacy as of 3/29/13 (params already merged in)
98
+
99
+ self.sub_outbound_rate! contact.outbound_rate
100
+ self.sub_inbound_rate! contact.inbound_rate
101
+ end
102
+
103
+ def sub_outbound_rate!(rate)
104
+ @contact_template.gsub!("${or.price}", rate && rate.attributes.price ? rate.attributes.price.to_s : '?')
105
+ @contact_template.gsub!("${or.type}", rate && rate.attributes.type ? rate.attributes.type : '?')
106
+ @contact_template.gsub!("${or.dialcode}", rate && rate.attributes.dialcode ? rate.attributes.dialcode : '?')
107
+ @contact_template.gsub!("${or.lata}", rate && rate.attributes.lata ? rate.attributes.lata : '?')
108
+ @contact_template.gsub!("${or.ocn}", rate && rate.attributes.ocn ? rate.attributes.ocn : '?')
109
+ @contact_template.gsub!("${or.billing_increment}", rate && rate.attributes.billing_increment ? rate.attributes.billing_increment.to_s : '?')
110
+ end
111
+
112
+ def sub_inbound_rate!(rate)
113
+ @contact_template.gsub!("${ir.price}", rate && rate.attributes.price ? rate.attributes.price.to_s : '?')
114
+ @contact_template.gsub!("${ir.type}", rate && rate.attributes.type ? rate.attributes.type : '?')
115
+ @contact_template.gsub!("${ir.dialcode}", rate && rate.attributes.dialcode ? rate.attributes.dialcode : '?')
116
+ @contact_template.gsub!("${ir.billing_increment}", rate && rate.attributes.billing_increment ? rate.attributes.billing_increment.to_s : '?')
117
+ @contact_template.gsub!("${ir.lata}", rate && rate.attributes.lata ? rate.attributes.lata : '?')
118
+ @contact_template.gsub!("${ir.ocn}", rate && rate.attributes.ocn ? rate.attributes.ocn : '?')
119
+ end
120
+
121
+ end
122
+
123
+ end
124
+ end
@@ -0,0 +1,18 @@
1
+ module RouteNGNClient
2
+ class EndPoint < Model
3
+
4
+ belongs_to :outbound_end_point_group, :inbound_end_point_group
5
+
6
+ def build_uri_parts(options = {})
7
+ {
8
+ :host => (self.attributes.host ? self.attributes.host.strip : ''),
9
+ :port => (self.attributes.port ? self.attributes.port.to_s : '')
10
+ }
11
+ end
12
+
13
+ def random_priority(max = 1000)
14
+ rand(max)
15
+ end
16
+
17
+ end
18
+ end