routengn-client 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,70 @@
1
+ module RouteNGNClient
2
+ class InboundEndPointGroup < RemoteModel
3
+
4
+ set_resource_attributes({
5
+ :path_base => 'metallic/inbound_end_point_groups',
6
+ :collection_name => 'inbound_end_point_groups',
7
+ :per_page => 200,
8
+ :request_options => {
9
+ }
10
+ })
11
+
12
+ belongs_to :carrier
13
+ has_many :end_points, :inbound_rates, :rate_sheets
14
+ has_one :allow_list
15
+
16
+ attr_accessor :account
17
+
18
+ def initialize(attributes = {})
19
+ super
20
+
21
+ @account ||= nil
22
+ end
23
+
24
+ def self.find_by_end_point_host(host, options = {}, &callback)
25
+ options = { :per_page => 200 }.merge(options)
26
+ all({:inbound_end_point_group => {:'end_points.host' => host}}, options, &callback)
27
+ end
28
+
29
+ def primary_route_table(options = {}, &callback)
30
+ if self.attributes.primary_route_table_id.blank?
31
+ callback.call(nil) if callback
32
+ else
33
+ RouteTable.find self.attributes.primary_route_table_id, options, &callback
34
+ end
35
+ end
36
+
37
+ def secondary_route_table(options = {}, &callback)
38
+ if self.attributes.secondary_route_table_id.blank?
39
+ callback.call(nil) if callback
40
+ else
41
+ RouteTable.find self.attributes.secondary_route_table_id, options, &callback
42
+ end
43
+ end
44
+
45
+ def dialcode_regexp
46
+ return @dr if @dr
47
+
48
+ @dr = Regexp.new(self.attributes.dialcode_expression) unless self.attributes.dialcode_expression.blank?
49
+ end
50
+
51
+ def dialcode_prefix_regexp
52
+ return @dpr if @dpr
53
+
54
+ @dpr = Regexp.new(self.attributes.dialcode_prefix_expression) unless self.attributes.dialcode_prefix_expression.blank?
55
+ end
56
+
57
+ def ani_regexp
58
+ return @ar if @ar
59
+
60
+ @ar = Regexp.new(self.attributes.ani_expression) unless self.attributes.ani_expression.blank?
61
+ end
62
+
63
+ def ani_prefix_regexp
64
+ return @apr if @apr
65
+
66
+ @apr = Regexp.new(self.attributes.ani_prefix_expression) unless self.attributes.ani_prefix_expression.blank?
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,13 @@
1
+ module RouteNGNClient
2
+ class InboundRate < RemoteModel
3
+
4
+ set_resource_attributes({
5
+ :collection_name => 'rates',
6
+ :index_method => 'inbound',
7
+ :per_page => 200
8
+ })
9
+
10
+ belongs_to :inbound_end_point_group
11
+
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ module RouteNGNClient
2
+ class OutboundEndPointGroup < RemoteModel
3
+
4
+ set_resource_attributes({
5
+ :path_base => 'metallic/outbound_end_point_groups',
6
+ :collection_name => 'outbound_end_point_groups',
7
+ :per_page => 50,
8
+ :request_options => {
9
+ }
10
+ })
11
+
12
+ belongs_to :carrier
13
+ has_many :end_points, :outbound_rates
14
+
15
+ def prioritized_end_points
16
+ eps_with_priority = self.end_points.select { |ep| !ep.attributes.priority.nil? }
17
+ eps_with_priority.shuffle!
18
+ eps_without_priority = self.end_points.select { |ep| ep.attributes.priority.nil? }
19
+ eps_without_priority.shuffle!
20
+
21
+ eps_with_priority.sort! { |x,y| x.attributes.priority <=> y.attributes.priority }
22
+ eps_without_priority.sort! { |x,y| x.random_priority <=> y.random_priority }
23
+
24
+ eps = (eps_with_priority + eps_without_priority)
25
+ self.attributes.max_contact_end_points ? eps[0, self.attributes.max_contact_end_points] : eps
26
+ end
27
+
28
+ def dialcode_regexp
29
+ Regexp.new(self.attributes.dialcode_expression) unless self.attributes.dialcode_expression.blank?
30
+ end
31
+
32
+ def build_uri_parts(options = {})
33
+ {
34
+ :uri_user_params => self.s_to_h(self.attributes.uri_user_params ? self.attributes.uri_user_params.strip : ''),
35
+ :uri_params => self.s_to_h(self.attributes.uri_params ? self.attributes.uri_params.strip : '')
36
+ }
37
+ end
38
+
39
+ protected
40
+
41
+ def s_to_h(s)
42
+ h = {}
43
+ m = s.match /^;*(.*);*$/
44
+
45
+ h = Hash[m[1].split(';').map! {|p| p.split('=')}] if m[1]
46
+
47
+ h
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,13 @@
1
+ module RouteNGNClient
2
+ class OutboundRate < RemoteModel
3
+
4
+ set_resource_attributes({
5
+ :collection_name => 'rates',
6
+ :index_method => 'outbound',
7
+ :per_page => 200
8
+ })
9
+
10
+ belongs_to :outbound_end_point_group
11
+
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module RouteNGNClient
2
+ class RateSheet < RemoteModel
3
+
4
+ set_resource_attributes({
5
+ :collection_name => 'rate_sheets',
6
+ :per_page => 25
7
+ })
8
+ belongs_to :account
9
+ belongs_to :inbound_end_point_group
10
+ belongs_to :outbound_end_point_group
11
+
12
+ end
13
+ end
@@ -0,0 +1,231 @@
1
+ module RouteNGNClient
2
+ class Record < RemoteModel
3
+
4
+ set_resource_attributes({
5
+ :collection_name => 'records',
6
+ :per_page => 25
7
+ })
8
+
9
+ belongs_to :account
10
+ belongs_to :inbound_end_point_group
11
+
12
+ def initialize(attributes = {})
13
+ super
14
+ end
15
+
16
+ def create!(attributes = {}, options = {})
17
+ options = options.merge({ :account_id => (self.account_id || (self.account.id if self.account)) })
18
+
19
+ attrs = attributes.merge({
20
+ :call_detail_record => (self.call_detail_record || self.call_detail_record_hash),
21
+ :inbound_end_point_group_id => (self.inbound_end_point_group_id || (self.inbound_end_point_group.id if self.inbound_end_point_group)),
22
+ :requested_at_float => (self.requested_at_float || (self.request.requested_at_float if self.request)),
23
+ :response_code => (self.response_code || (self.final_response.response_code if self.final_response)),
24
+ :logs => self.logs
25
+ })
26
+
27
+ self.class.create!(attrs, options)
28
+ end
29
+
30
+ def call_detail_record
31
+ self.attributes.call_detail_record
32
+ end
33
+
34
+ def call_detail_record_hash
35
+ self.class.values_to_hash(self.to_comma_headers, self.to_comma)
36
+ end
37
+
38
+ def routes_csvs(delimiter = ';')
39
+ (self.routes || []).map { |r| CSV.generate_line(r.to_comma, :force_quotes => true, :col_sep => delimiter, :quote_char => '\'').strip! }
40
+ end
41
+
42
+ def routes_string(delimiter = '|')
43
+ self.routes_csvs.join(delimiter)
44
+ end
45
+
46
+ def append_log(s, options = {})
47
+ append_timestamp = options.has_key?(:append_timestamp) ? options[:append_timestamp] : true
48
+
49
+ self.attributes.logs ||= []
50
+
51
+ if append_timestamp
52
+ age = self.attributes.request ? self.attributes.request.age.round(5) : nil
53
+ self.attributes.logs << "#{age}: #{s}"
54
+ else
55
+ self.attributes.logs << "#{s}"
56
+ end
57
+ end
58
+
59
+ def set_request(r)
60
+ return unless r
61
+
62
+ self.attributes.request = r
63
+ self.requested_at_float = r.requested_at_float
64
+ self.attributes.sip_request = r.sip_request
65
+ 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}"
66
+ self.append_log "#{r.sip_request.data if r.sip_request}"
67
+ end
68
+
69
+ def set_inbound_end_point_group(iepg)
70
+ return unless iepg
71
+
72
+ self.append_log "Using inbound_end_point_group:"
73
+ self.append_log iepg.to_log, :append_timestamp => false
74
+ self.attributes.inbound_end_point_group = iepg
75
+ self.attributes.account = iepg.account if iepg
76
+ end
77
+
78
+ def set_inbound_rate(rate)
79
+ return unless rate
80
+
81
+ self.append_log "Using inbound_rate:"
82
+ self.append_log rate.to_log, :append_timestamp => false
83
+ self.attributes.inbound_rate = rate
84
+ end
85
+
86
+ def set_origination_code(code)
87
+ return unless code
88
+
89
+ self.append_log "Using origination_code:"
90
+ self.append_log code.to_log, :append_timestamp => false
91
+ self.attributes.origination_code = code
92
+ end
93
+
94
+ def set_destination_code(code)
95
+ return unless code
96
+
97
+ self.append_log "Using destination_code:"
98
+ self.append_log code.to_log, :append_timestamp => false
99
+ self.attributes.destination_code = code
100
+ end
101
+
102
+ def set_rate_type(type)
103
+ return unless type
104
+
105
+ self.append_log "Using rate_type: #{type}"
106
+ self.attributes.rate_type = type
107
+ end
108
+
109
+ def add_route(route)
110
+ return unless route
111
+
112
+ self.append_log "Added route:"
113
+ self.append_log route.to_log, :append_timestamp => false
114
+ self.attributes.routes ||= []
115
+ self.attributes.routes << route
116
+ end
117
+
118
+ def add_route_table(route_table)
119
+ return unless route_table
120
+
121
+ self.append_log "Using route_table:"
122
+ self.append_log route_table.to_log, :append_timestamp => false
123
+ self.attributes.route_tables ||= []
124
+ self.attributes.route_tables << route_table
125
+ end
126
+
127
+ def final_route_table
128
+ self.attributes.route_tables.blank? ? nil : self.attributes.route_tables.last
129
+ end
130
+
131
+ def add_response(r)
132
+ return unless r
133
+
134
+ self.attributes.responses ||= []
135
+ self.attributes.responses << r
136
+ self.append_log "Sending response code: #{(r.sip_response.blank? || r.sip_response.status_code.blank?) ? 'Unknown' : r.sip_response.status_code}"
137
+ self.append_log "#{r.sip_response.data if r.sip_response}"
138
+ end
139
+
140
+ def final_response
141
+ self.attributes.responses.blank? ? nil : self.attributes.responses.last
142
+ end
143
+
144
+ def request
145
+ self.attributes.request
146
+ end
147
+
148
+ def sip_request
149
+ self.attributes.sip_request
150
+ end
151
+
152
+ def origination_code
153
+ self.attributes.origination_code
154
+ end
155
+
156
+ def destination_code
157
+ self.attributes.destination_code
158
+ end
159
+
160
+ def inbound_rate
161
+ self.attributes.inbound_rate
162
+ end
163
+
164
+ def sip_redirector_version
165
+ self.attributes.sip_redirector_version
166
+ end
167
+
168
+ def cache_prep!
169
+ attrs = Hashie::Mash.new
170
+ attrs.call_detail_record = self.call_detail_record_hash
171
+ attrs.account_id = self.account_id || (self.account.id if self.account)
172
+ attrs.inbound_end_point_group_id = self.inbound_end_point_group.id if self.inbound_end_point_group
173
+ attrs.requested_at_float = self.requested_at_float || (self.request.requested_at_float if self.request)
174
+ attrs.response_code = self.final_response.response_code if self.final_response
175
+ attrs.logs = self.logs
176
+ self.attributes = attrs
177
+ end
178
+
179
+ def as_cdr(delimiter = ',')
180
+ CSV.generate_line self.to_comma, :force_quotes => true, :col_sep => delimiter, :row_sep => nil
181
+ end
182
+
183
+ def self.values_to_hash(headers, values)
184
+ values.each_with_index {| v,i| headers[i] = [headers[i].to_sym, v] }
185
+ Hash[headers]
186
+ end
187
+
188
+ comma do
189
+ request :requested_at_iso => 'requested_at_iso'
190
+ request :requested_at_float => 'requested_at_float'
191
+ request :server_hostname => 'server'
192
+ request :peer_ip => 'peer_ip'
193
+ request :peer_port => 'peer_port'
194
+ request :sip_request_method => 'request_method'
195
+ request :uuid => 'request_uuid'
196
+ request :worker_id => 'worker_id'
197
+ sip_request :prefix => 'request_prefix'
198
+ sip_request :dialcode => 'request_dialcode'
199
+ sip_request :call_id => 'request_call_id'
200
+ sip_request :npdi => 'request_npdi'
201
+ sip_request :rn => 'request_rn'
202
+ sip_request :ani => 'request_ani'
203
+ origination_code :dialcode => 'origination_dialcode'
204
+ origination_code :local => 'origination_local'
205
+ origination_code :city => 'origination_city'
206
+ origination_code :state => 'origination_state'
207
+ origination_code :country => 'origination_country'
208
+ origination_code :lata => 'origination_lata'
209
+ origination_code :ocn => 'origination_ocn'
210
+ destination_code :dialcode => 'destination_dialcode'
211
+ destination_code :local => 'destination_local'
212
+ destination_code :city => 'destination_city'
213
+ destination_code :state => 'destination_state'
214
+ destination_code :country => 'destination_country'
215
+ destination_code :lata => 'destination_lata'
216
+ destination_code :ocn => 'destination_ocn'
217
+ final_route_table :name => 'route_table_name'
218
+ inbound_end_point_group :id => 'inbound_end_point_group_id'
219
+ inbound_end_point_group :name => 'inbound_end_point_group_name'
220
+ inbound_rate :price => 'inbound_rate_price'
221
+ inbound_rate :type => 'inbound_rate_type'
222
+ routes_string 'routes' #"routes #{Route.to_comma_headers.join(';')}|route2|route3..."
223
+ final_response :elapsed_time => 'elapsed_time'
224
+ final_response :response_code => 'response_code'
225
+ final_response :internal_response_code => 'internal_response_code'
226
+ account :id => 'account_id'
227
+ sip_redirector_version 'sip_redirector_version'
228
+ end
229
+
230
+ end
231
+ end
@@ -0,0 +1,42 @@
1
+ module RouteNGNClient
2
+ class Route < Model
3
+
4
+ belongs_to :route_table, :outbound_end_point_group
5
+ has_one :outbound_rate
6
+
7
+ def contacts
8
+ @contacts ||= self.build_contacts
9
+ end
10
+
11
+ def build_contacts(end_points = nil)
12
+ if oepg = self.outbound_end_point_group
13
+ end_points ||= oepg.prioritized_end_points
14
+ @contacts = end_points.collect do |end_point|
15
+ Contact.new(:end_point => end_point, :route => self)
16
+ end
17
+ end
18
+ end
19
+
20
+ def build_aors(sip_request, inbound_end_point_group, options = {})
21
+ self.contacts.collect { |c| c.build_aor(sip_request, inbound_end_point_group, options) }
22
+ end
23
+
24
+ def inbound_rate
25
+ @attributes.inbound_rate
26
+ end
27
+
28
+ def blocked?
29
+ @attributes.has_key?('blocked') && @attributes.blocked == 'true' ? true : false
30
+ end
31
+
32
+ comma do
33
+ outbound_rate :price => 'outbound_rate_price'
34
+ outbound_rate :type => 'outbound_rate_type'
35
+ outbound_rate :dialcode => 'outbound_rate_dialcode'
36
+ outbound_rate :lata => 'outbound_rate_lata'
37
+ outbound_rate :ocn => 'outbound_rate_ocn'
38
+ outbound_end_point_group :name => 'outbound_end_point_group_name'
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,7 @@
1
+ module RouteNGNClient
2
+ class RouteBlock < RemoteModel
3
+
4
+ belongs_to :route_table
5
+
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module RouteNGNClient
2
+ class RouteContainer < Model
3
+
4
+ belongs_to :route_table
5
+ has_many :routes
6
+ has_one :inbound_rate
7
+
8
+ def enhanced_routes
9
+ self.routes.each do |route|
10
+ route.attributes.inbound_rate = self.inbound_rate
11
+ route.attributes.outbound_end_point_group = OutboundEndPointGroup.find(route.attributes.outbound_end_point_group_id, {:account_id => route.request_account_id})
12
+ end
13
+
14
+ self.routes
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ module RouteNGNClient
2
+ class RouteOrderingOverride < RemoteModel
3
+
4
+ belongs_to :route_table
5
+
6
+ end
7
+ end
@@ -0,0 +1,30 @@
1
+ module RouteNGNClient
2
+ class RouteTable < RemoteModel
3
+
4
+ set_resource_attributes({
5
+ :path_base => 'metallic/route_tables',
6
+ :collection_name => 'route_tables',
7
+ :per_page => 25,
8
+ :request_options => {
9
+ }
10
+ })
11
+
12
+ belongs_to :account
13
+ has_many :routes, :outbound_end_point_groups
14
+
15
+ def route_containers(params = {}, options = {}, &callback)
16
+ options[:klass] ||= RouteContainer
17
+ options[:account_id] ||= self.request_account_id
18
+
19
+ models = []
20
+
21
+ response = Request.new(self.class.resource_attributes.merge(:collection_name => 'route_containers'), :get, [self.class.path_base, self.attributes.id, 'route_containers'], self.class.path_ext, params, options).execute(RouteNGNClient.connection)
22
+ models = response.to_models
23
+
24
+ callback.call(models) if callback
25
+
26
+ models
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ module RouteNGNClient
2
+ class SystemAdminAccount < Account
3
+
4
+ end
5
+ end
@@ -0,0 +1,72 @@
1
+ module RouteNGNClient
2
+ class URIMatcher < Model
3
+ include Comparable
4
+
5
+ HEADER_ORDER = %(contact from request)
6
+
7
+ belongs_to :allow_list
8
+
9
+ def <=>(other)
10
+ if self.header == other.header
11
+ case (self.weight <=> other.weight)
12
+ when 0
13
+ self.id.to_s <=> other.id.to_s
14
+ when 1 #self.weight is higher
15
+ -1
16
+ when -1 #self.weight is lower
17
+ 1
18
+ else
19
+ 0
20
+ end
21
+ else
22
+ return 0 if !HEADER_ORDER.include?(self.header) && !HEADER_ORDER.include?(other.header)
23
+ return -1 if !HEADER_ORDER.include?(self.header)
24
+ return 1 if !HEADER_ORDER.include?(other.header)
25
+ return (HEADER_ORDER.index(self.header) <=> HEADER_ORDER.index(other.header))
26
+ end
27
+ end
28
+
29
+ def weight
30
+ @weight ||= [@attributes.prefix, @attributes.user_params, @attributes.uri_params].select { |x| !x.blank? }.length
31
+ end
32
+
33
+ def matches?(args = {})
34
+ return false if !self.matches_header?(args[:header])
35
+ return false if !@attributes.prefix.blank? && !self.matches_prefix?(args[:prefix])
36
+ return false if !@attributes.user_params.blank? && !self.matches_user_params?(args[:user_params])
37
+ return false if !@attributes.uri_params.blank? && !self.matches_uri_params?(args[:uri_params])
38
+ return true
39
+ end
40
+
41
+ def matches_header?(arg)
42
+ !@attributes.header.blank? && !arg.blank? && @attributes.header == arg
43
+ end
44
+
45
+ def matches_prefix?(arg)
46
+ @attributes.prefix == arg
47
+ end
48
+
49
+ def matches_user_params?(arg)
50
+ if arg.is_a?(String)
51
+ @attributes.user_params == arg
52
+ elsif arg.is_a?(Array)
53
+ arg.include?(@attributes.user_params)
54
+ else
55
+ false
56
+ end
57
+ end
58
+
59
+ def matches_uri_params?(arg)
60
+ if arg.is_a?(String)
61
+ @attributes.uri_params == arg
62
+ elsif arg.is_a?(Array)
63
+ arg.include?(@attributes.uri_params)
64
+ else
65
+ false
66
+ end
67
+ end
68
+
69
+ end
70
+
71
+ class UriMatcher < URIMatcher;end
72
+ end
@@ -0,0 +1,20 @@
1
+ module RouteNGNClient
2
+ class User < RemoteModel
3
+
4
+ set_resource_attributes({
5
+ :collection_name => 'users',
6
+ :per_page => 25
7
+ })
8
+
9
+ belongs_to :account
10
+
11
+ def admin?
12
+ self.admin.nil? ? false : self.admin
13
+ end
14
+
15
+ def call_processor?
16
+ self.call_processor.nil? ? false : self.call_processor
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,69 @@
1
+ module RouteNGNClient
2
+ class RemoteModel < Model
3
+
4
+ DEFAULT_RESOURCE_ATTRIBUTES = {
5
+ :path_base => nil,
6
+ :path_ext => '.json',
7
+ :index_method => nil,
8
+ :collection_name => '',
9
+ :per_page => 25,
10
+ :request_options => {}
11
+ }
12
+
13
+ @resource_attributes = DEFAULT_RESOURCE_ATTRIBUTES
14
+
15
+ module ClassMethods
16
+ attr_reader :resource_attributes
17
+
18
+ def path_ext;@resource_attributes[:path_ext];end
19
+ def index_method;@resource_attributes[:index_method];end
20
+ def collection_name;@resource_attributes[:collection_name];end
21
+
22
+ def set_resource_attributes(ra)
23
+ @resource_attributes = DEFAULT_RESOURCE_ATTRIBUTES.merge(ra)
24
+ end
25
+
26
+ def path_base(params = {})
27
+ pb = @resource_attributes[:path_base] || File.join('/', @resource_attributes[:collection_name])
28
+ params.each { |k,v| pb.gsub!(":#{k}", v) } unless params.blank?
29
+ pb
30
+ end
31
+
32
+ def all(conditions = {}, options = {}, &callback)
33
+ options.merge!(:klass => self)
34
+ params = conditions
35
+ params[:per_page] ||= @resource_attributes[:per_page]
36
+ params[:page] ||= 1
37
+ params[:include_paging_info] = 'true' unless params.has_key?(:include_paging_info)
38
+
39
+ response = Request.new(@resource_attributes, :get, [path_base, index_method], path_ext, params, options).execute(RouteNGNClient.connection)
40
+ models = response.to_models
41
+
42
+ callback.call(models) if callback
43
+
44
+ models
45
+ end
46
+
47
+ def where(conditions = {}, options = {}, &callback)
48
+ self.all(conditions, options, &callback)
49
+ end
50
+
51
+ def find(id, options = {}, &callback)
52
+ options.merge!(:klass => self)
53
+
54
+ response = Request.new(@resource_attributes, :get, [path_base, id.to_s], path_ext, {}, options).execute(RouteNGNClient.connection)
55
+ model = response.to_model
56
+
57
+ callback.call(models) if callback
58
+
59
+ model
60
+ end
61
+
62
+ def create!(attributes = {}, options = {}, &callback)
63
+ raise NotImplementedError
64
+ end
65
+ end
66
+
67
+ extend ClassMethods
68
+ end
69
+ end