ruby-bandwidth-iris 1.0.0

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 (77) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +13 -0
  3. data/.gitignore +12 -0
  4. data/.rspec +3 -0
  5. data/.travis.yml +7 -0
  6. data/Gemfile +10 -0
  7. data/LICENSE.txt +20 -0
  8. data/README.md +571 -0
  9. data/Rakefile +5 -0
  10. data/examples/account.rb +15 -0
  11. data/examples/available_npa_nxx.rb +17 -0
  12. data/examples/available_number.rb +17 -0
  13. data/examples/city.rb +16 -0
  14. data/examples/config.yml.example +4 -0
  15. data/examples/covered-rate-center.rb +19 -0
  16. data/examples/loa.pdf +0 -0
  17. data/examples/order.rb +51 -0
  18. data/examples/port-in.rb +95 -0
  19. data/examples/sip_peer.rb +60 -0
  20. data/examples/site.rb +29 -0
  21. data/examples/tn.rb +20 -0
  22. data/lib/bandwidth-iris/account.rb +10 -0
  23. data/lib/bandwidth-iris/api_item.rb +36 -0
  24. data/lib/bandwidth-iris/available_npa_nxx.rb +15 -0
  25. data/lib/bandwidth-iris/available_number.rb +15 -0
  26. data/lib/bandwidth-iris/city.rb +15 -0
  27. data/lib/bandwidth-iris/client.rb +232 -0
  28. data/lib/bandwidth-iris/client_wrapper.rb +28 -0
  29. data/lib/bandwidth-iris/covered_rate_center.rb +17 -0
  30. data/lib/bandwidth-iris/disc_number.rb +19 -0
  31. data/lib/bandwidth-iris/disconnect.rb +36 -0
  32. data/lib/bandwidth-iris/dlda.rb +39 -0
  33. data/lib/bandwidth-iris/errors.rb +31 -0
  34. data/lib/bandwidth-iris/import_to_account.rb +24 -0
  35. data/lib/bandwidth-iris/in_service_number.rb +24 -0
  36. data/lib/bandwidth-iris/lidb.rb +24 -0
  37. data/lib/bandwidth-iris/lnp_checker.rb +18 -0
  38. data/lib/bandwidth-iris/lsr_order.rb +58 -0
  39. data/lib/bandwidth-iris/order.rb +76 -0
  40. data/lib/bandwidth-iris/port_in.rb +80 -0
  41. data/lib/bandwidth-iris/port_out.rb +24 -0
  42. data/lib/bandwidth-iris/rate_center.rb +19 -0
  43. data/lib/bandwidth-iris/sip_peer.rb +47 -0
  44. data/lib/bandwidth-iris/site.rb +83 -0
  45. data/lib/bandwidth-iris/subscription.rb +42 -0
  46. data/lib/bandwidth-iris/tn.rb +41 -0
  47. data/lib/bandwidth-iris/tn_reservation.rb +26 -0
  48. data/lib/bandwidth-iris/user.rb +20 -0
  49. data/lib/bandwidth-iris/version.rb +4 -0
  50. data/lib/ruby-bandwidth-iris.rb +29 -0
  51. data/ruby-bandwidth-iris.gemspec +26 -0
  52. data/spec/bandwidth-iris/account_spec.rb +19 -0
  53. data/spec/bandwidth-iris/available_npa_nxx_spec.rb +22 -0
  54. data/spec/bandwidth-iris/available_number_spec.rb +19 -0
  55. data/spec/bandwidth-iris/city_spec.rb +22 -0
  56. data/spec/bandwidth-iris/client_spec.rb +125 -0
  57. data/spec/bandwidth-iris/covered_rate_center_spec.rb +22 -0
  58. data/spec/bandwidth-iris/disconnect_spec.rb +52 -0
  59. data/spec/bandwidth-iris/dlda_spec.rb +47 -0
  60. data/spec/bandwidth-iris/import_to_account_spec.rb +36 -0
  61. data/spec/bandwidth-iris/in_service_number_spec.rb +33 -0
  62. data/spec/bandwidth-iris/lidb_spec.rb +44 -0
  63. data/spec/bandwidth-iris/lnp_checker_spec.rb +24 -0
  64. data/spec/bandwidth-iris/lsr_order_spec.rb +96 -0
  65. data/spec/bandwidth-iris/order_spec.rb +124 -0
  66. data/spec/bandwidth-iris/port_in_spec.rb +135 -0
  67. data/spec/bandwidth-iris/port_out_spec.rb +36 -0
  68. data/spec/bandwidth-iris/rate_center_spec.rb +29 -0
  69. data/spec/bandwidth-iris/sip_peer_spec.rb +86 -0
  70. data/spec/bandwidth-iris/site_spec.rb +95 -0
  71. data/spec/bandwidth-iris/subscription_spec.rb +58 -0
  72. data/spec/bandwidth-iris/tn_reservation_spec.rb +39 -0
  73. data/spec/bandwidth-iris/tn_spec.rb +68 -0
  74. data/spec/bandwidth-iris/user_spec.rb +21 -0
  75. data/spec/helper.rb +75 -0
  76. data/spec/xml.yml +46 -0
  77. metadata +256 -0
data/examples/city.rb ADDED
@@ -0,0 +1,16 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'yaml'
5
+ require 'ruby-bandwidth-iris'
6
+ config = YAML.load_file('config.yml')
7
+
8
+ BandwidthIris::Client.global_options = {
9
+ :api_endpoint => config['api_endpoint'],
10
+ :user_name => config['user_name'],
11
+ :password => config['password'],
12
+ :account_id => config['account_id']
13
+ }
14
+
15
+ puts BandwidthIris::City.list({:available => true, :state => 'NC'})
16
+
@@ -0,0 +1,4 @@
1
+ api_endpoint: "https://api.inetwork.com"
2
+ account_id: ""
3
+ user_name: ""
4
+ password: ""
@@ -0,0 +1,19 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'yaml'
5
+ require 'ruby-bandwidth-iris'
6
+ config = YAML.load_file('config.yml')
7
+
8
+ BandwidthIris::Client.global_options = {
9
+ :api_endpoint => config['api_endpoint'],
10
+ :user_name => config['user_name'],
11
+ :password => config['password'],
12
+ :account_id => config['account_id']
13
+ }
14
+
15
+
16
+ BandwidthIris::CoveredRateCenter.list({:zip => '27609', :page=>1, :size=>100}).each do |c|
17
+ puts c.to_data
18
+ end
19
+
data/examples/loa.pdf ADDED
Binary file
data/examples/order.rb ADDED
@@ -0,0 +1,51 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'yaml'
5
+ require 'ruby-bandwidth-iris'
6
+ config = YAML.load_file('config.yml')
7
+
8
+ BandwidthIris::Client.global_options = {
9
+ :api_endpoint => config['api_endpoint'],
10
+ :user_name => config['user_name'],
11
+ :password => config['password'],
12
+ :account_id => config['account_id']
13
+ }
14
+
15
+ number = '9195551212' #exisitng number for order
16
+
17
+
18
+ site = BandwidthIris::Site.create({
19
+ :name => "Ruby Test Site",
20
+ :description => "A Site From Ruby SDK Examples",
21
+ :address => {
22
+ :house_number => "123",
23
+ :street_name => "Anywhere St",
24
+ :city => "Raleigh",
25
+ :state_code =>"NC",
26
+ :zip => "27609",
27
+ :address_type => "Service"
28
+ }
29
+ })
30
+
31
+ order = BandwidthIris::Order.create({
32
+ :name =>"A Test Order",
33
+ :site_id => site.id,
34
+ :existing_telephone_number_order_type => {
35
+ :telephone_number_list =>
36
+ {
37
+ :telephone_number => [number]
38
+ }
39
+
40
+ }
41
+ })
42
+
43
+ puts order.to_data
44
+
45
+ order = BandwidthIris::Order.get(order.id)
46
+
47
+ puts order.to_data
48
+
49
+ order.delete()
50
+ site.delete()
51
+
@@ -0,0 +1,95 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'yaml'
5
+ require 'ruby-bandwidth-iris'
6
+ config = YAML.load_file('config.yml')
7
+
8
+ BandwidthIris::Client.global_options = {
9
+ :api_endpoint => config['api_endpoint'],
10
+ :user_name => config['user_name'],
11
+ :password => config['password'],
12
+ :account_id => config['account_id']
13
+ }
14
+
15
+ number_to_check = '+12525130283' #TODO fill with valid number
16
+
17
+ host = '1.1.2.3'
18
+
19
+
20
+ site = BandwidthIris::Site.create({
21
+ :name => "Ruby Test Site",
22
+ :description => "A Site From Ruby SDK Examples",
23
+ :address => {
24
+ :house_number => "123",
25
+ :street_name => "Anywhere St",
26
+ :city => "Raleigh",
27
+ :state_code =>"NC",
28
+ :zip => "27609",
29
+ :address_type => "Service"
30
+ }
31
+ })
32
+
33
+ data = {
34
+ :peer_name => "A New SIP Peer",
35
+ :is_default_peer => true,
36
+ :short_messaging_protocol => "SMPP",
37
+ :site_id => site[:id],
38
+ :voice_hosts =>
39
+ {
40
+ :host => {
41
+ :host_name => host
42
+ }
43
+ },
44
+ :sms_hosts =>
45
+ {
46
+ :host => {
47
+ :host_name => host
48
+ }
49
+ },
50
+ :termination_hosts =>
51
+ {
52
+ :termination_host => {
53
+ :host_name => host,
54
+ :port => 5060,
55
+ }
56
+ }
57
+
58
+ }
59
+
60
+ sip_peer = BandwidthIris::SipPeer.create(site[:id], data)
61
+
62
+ res = BandwidthIris::LnpChecker.check(number_to_check)
63
+
64
+ if res[:portable_numbers] && res[:portable_numbers][:tn] == number_to_check
65
+ puts 'Your number is portable. Creating PortIn Order'
66
+ port_in = BandwidthIris::PortIn.create(create_port_in_order(number_to_check, site, sip_peer))
67
+ puts "Created order #{port_in[:id]}"
68
+ port_in.create_file(File.open('./loa.pdf', 'r'), 'application/pdf')
69
+ end
70
+
71
+ def create_port_in_order(number, site, sip_peer)
72
+ {
73
+ :site_id => site[:id],
74
+ :peer_id => sip_peer[:id],
75
+ :billing_telephone_number => number,
76
+ :subscriber => {
77
+ :subscriber_type => "BUSINESS",
78
+ :business_name => "Company",
79
+ :service_address => {
80
+ :house_number => "123",
81
+ :street_name => "EZ Street",
82
+ :city => "Raleigh",
83
+ :state_code => "NC",
84
+ :zip => "27615",
85
+ :county => "Wake"
86
+ }
87
+ },
88
+ :loa_authorizing_person => "Joe Blow",
89
+ :list_of_phone_numbers => {
90
+ :phoneNumber => number
91
+ },
92
+ :billingType => "PORTIN"
93
+ }
94
+ end
95
+
@@ -0,0 +1,60 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'yaml'
5
+ require 'ruby-bandwidth-iris'
6
+ config = YAML.load_file('config.yml')
7
+
8
+ BandwidthIris::Client.global_options = {
9
+ :api_endpoint => config['api_endpoint'],
10
+ :user_name => config['user_name'],
11
+ :password => config['password'],
12
+ :account_id => config['account_id']
13
+ }
14
+
15
+ host = '10.20.30.41'
16
+
17
+
18
+ site = BandwidthIris::Site.create({
19
+ :name => "Ruby Test Site",
20
+ :description => "A Site From Ruby SDK Examples",
21
+ :address => {
22
+ :house_number => "123",
23
+ :street_name => "Anywhere St",
24
+ :city => "Raleigh",
25
+ :state_code =>"NC",
26
+ :zip => "27609",
27
+ :address_type => "Service"
28
+ }
29
+ })
30
+
31
+ data = {
32
+ :peer_name => "A New SIP Peer",
33
+ :is_default_peer => true,
34
+ :short_messaging_protocol => "SMPP",
35
+ :site_id => site[:id],
36
+ :voice_hosts =>
37
+ {
38
+ :host => {
39
+ :host_name => host
40
+ }
41
+ },
42
+ :sms_hosts =>
43
+ {
44
+ :host => {
45
+ :host_name => host
46
+ }
47
+ },
48
+ :termination_hosts =>
49
+ {
50
+ :termination_host => {
51
+ :host_name => host,
52
+ :port => 5060,
53
+ }
54
+ }
55
+
56
+ }
57
+ sip_peer = BandwidthIris::SipPeer.create(site[:id], data)
58
+
59
+
60
+ sip_peer.delete(site[:id])
data/examples/site.rb ADDED
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'yaml'
5
+ require 'ruby-bandwidth-iris'
6
+ config = YAML.load_file('config.yml')
7
+
8
+ BandwidthIris::Client.global_options = {
9
+ :api_endpoint => config['api_endpoint'],
10
+ :user_name => config['user_name'],
11
+ :password => config['password'],
12
+ :account_id => config['account_id']
13
+ }
14
+
15
+
16
+ site = BandwidthIris::Site.create({
17
+ :name => "Ruby Test Sitei1",
18
+ :description => "A Site From Ruby SDK Examples",
19
+ :address => {
20
+ :house_number => "123",
21
+ :street_name => "Anywhere St",
22
+ :city => "Raleigh",
23
+ :state_code =>"NC",
24
+ :zip => "27609",
25
+ :address_type => "Service"
26
+ }
27
+ })
28
+
29
+ site.delete()
data/examples/tn.rb ADDED
@@ -0,0 +1,20 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'yaml'
5
+ require 'ruby-bandwidth-iris'
6
+ config = YAML.load_file('config.yml')
7
+
8
+
9
+ BandwidthIris::Client.global_options = {
10
+ :api_endpoint => config['api_endpoint'],
11
+ :user_name => config['user_name'],
12
+ :password => config['password'],
13
+ :account_id => config['account_id']
14
+ }
15
+
16
+ list = BandwidthIris::Tn.list({:npa => '818', :page => 1, :size => 100}).each do |n|
17
+ puts n.to_data
18
+ end
19
+
20
+ puts BandwidthIris::Tn.get(list[0][:full_number]).to_data
@@ -0,0 +1,10 @@
1
+ module BandwidthIris
2
+ class Account
3
+ extend ClientWrapper
4
+
5
+ def self.get(client)
6
+ client.make_request(:get, client.concat_account_path(nil))[0][:account]
7
+ end
8
+ wrap_client_arg :get
9
+ end
10
+ end
@@ -0,0 +1,36 @@
1
+ module BandwidthIris
2
+ # Module which adds common operations for all Catapult api related classes
3
+ module ApiItem
4
+ # Initializer
5
+ #
6
+ # @param data [Hash] Hash with data of api item. Initializer will create accessors for each key of this hash
7
+ # @param client [Client] Optional client instance. If omitted Client instance with default parameters will be used
8
+ def initialize(data={}, client = nil)
9
+ @client = client || Client.new()
10
+ @data = (data || {}).clone()
11
+ @data.each do |k,v|
12
+ self.define_singleton_method(k) do
13
+ @data[k]
14
+ end
15
+ self.define_singleton_method("#{k}=".to_sym()) do |val|
16
+ @data[k] = val
17
+ end
18
+ end
19
+ end
20
+
21
+ # Return data of api item as hash
22
+ def to_data()
23
+ @data.clone()
24
+ end
25
+
26
+ # Return item of @data by name
27
+ def [] (name)
28
+ @data[name]
29
+ end
30
+
31
+ # Set value of @data's item by name
32
+ def []= (name, value)
33
+ @data[name] = value
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,15 @@
1
+ module BandwidthIris
2
+ AVAILABLE_NPA_NXX_PATH = 'availableNpaNxx'
3
+
4
+ class AvailableNpaNxx
5
+ extend ClientWrapper
6
+
7
+ def self.list(client, query)
8
+ list = client.make_request(:get, client.concat_account_path(AVAILABLE_NPA_NXX_PATH), query)[0][:available_npa_nxx_list][:available_npa_nxx]
9
+ return [] if !list
10
+ if list.is_a?(Array) then list else [list] end
11
+ end
12
+ wrap_client_arg :list
13
+
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module BandwidthIris
2
+ AVAILABLE_NUMBER_PATH = 'availableNumbers'
3
+
4
+ class AvailableNumber
5
+ extend ClientWrapper
6
+
7
+ def self.list(client, query)
8
+ list = client.make_request(:get, client.concat_account_path(AVAILABLE_NUMBER_PATH), query)[0][:telephone_number_list][:telephone_number]
9
+ return [] if !list
10
+ if list.is_a?(Array) then list else [list] end
11
+ end
12
+ wrap_client_arg :list
13
+
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module BandwidthIris
2
+ CITY_PATH = 'cities'
3
+
4
+ class City
5
+ extend ClientWrapper
6
+
7
+ def self.list(client, query)
8
+ list = client.make_request(:get, CITY_PATH, query)[0][:cities][:city]
9
+ return [] if !list
10
+ if list.is_a?(Array) then list else [list] end
11
+ end
12
+ wrap_client_arg :list
13
+
14
+ end
15
+ end
@@ -0,0 +1,232 @@
1
+ require 'faraday'
2
+ require 'certified'
3
+ require 'active_support/xml_mini'
4
+ require 'active_support/core_ext/hash/conversions'
5
+ require 'active_support/core_ext/string/inflections'
6
+
7
+
8
+ module BandwidthIris
9
+
10
+ class Client
11
+ def initialize (account_id = nil, user_name = nil, password = nil, options = nil)
12
+ if user_name == nil && password == nil && options == nil
13
+ if account_id && account_id.is_a?(Hash)
14
+ options = account_id
15
+ account_id = nil
16
+ end
17
+ end
18
+ options = options || @@global_options
19
+ account_id = options[:account_id] unless account_id
20
+ user_name = options[:user_name] || options[:username] unless user_name
21
+ password = options[:password] unless password
22
+ options[:api_endpoint] = @@global_options[:api_endpoint] unless options[:api_endpoint]
23
+ options[:api_version] = @@global_options[:api_version] unless options[:api_version]
24
+ api_endpoint = options[:api_endpoint] || "https://api.inetwork.com"
25
+ api_version = options[:api_version] || "v1.0"
26
+
27
+ @build_path = lambda {|path| "/#{api_version}" + (if path[0] == "/" then path else "/#{path}" end) }
28
+ @set_adapter = lambda {|faraday| faraday.adapter(Faraday.default_adapter)}
29
+ @create_connection = lambda{||
30
+ Faraday.new(api_endpoint) { |faraday|
31
+ faraday.basic_auth(user_name, password)
32
+ #faraday.response :logger
33
+ faraday.headers['Accept'] = 'application/xml'
34
+ @set_adapter.call(faraday)
35
+ }
36
+ }
37
+ @concat_account_path = lambda {|path| "/accounts/#{account_id}" + (if path then (if path[0] == "/" then path else "/#{path}" end) else '' end) }
38
+ @api_endpoint = api_endpoint
39
+ @api_version = api_version
40
+ end
41
+
42
+ attr_reader :api_endpoint, :api_version
43
+
44
+ @@global_options = {}
45
+
46
+ # Return global options
47
+ def Client.global_options
48
+ @@global_options
49
+ end
50
+
51
+ # Set global options
52
+ def Client.global_options=(v)
53
+ @@global_options = v
54
+ end
55
+
56
+ # Extract id from location header
57
+ # @param location [String] location header value
58
+ # @return [String] extracted id
59
+ def Client.get_id_from_location_header(location)
60
+ items = (location || '').split('/')
61
+ raise StandardError.new('Missing id in the location header') if items.size < 2
62
+ items.last
63
+ end
64
+
65
+ # Make HTTP request to IRIS API
66
+ # @param method [Symbol] http method to make
67
+ # @param path [string] path of url (exclude api verion and endpoint) to make call
68
+ # @param data [Hash] data which will be sent with request (for :get and :delete request they will be sent with query in url)
69
+ # @return [Array] array with 2 elements: parsed data of response and response headers
70
+ def make_request(method, path, data = {})
71
+ connection = @create_connection.call()
72
+ response = if method == :get || method == :delete
73
+ d = camelcase(data)
74
+ connection.run_request(method, @build_path.call(path), nil, nil) do |req|
75
+ req.params = d unless d == nil || d.empty?
76
+ end
77
+ else
78
+ connection.run_request(method, @build_path.call(path), build_xml(data), {'Content-Type' => 'application/xml'})
79
+ end
80
+ body = check_response(response)
81
+ [body || {}, symbolize(response.headers || {})]
82
+ end
83
+
84
+ # Check response object and raise error if status code >= 400
85
+ # @param response response object
86
+ def check_response(response)
87
+ parsed_body = parse_xml(response.body || '')
88
+ code = find_first_descendant(parsed_body, :error_code)
89
+ description = find_first_descendant(parsed_body, :description)
90
+ unless code
91
+ error = find_first_descendant(parsed_body, :error)
92
+ if error
93
+ code = error[:code]
94
+ description = error[:description]
95
+ else
96
+ errors = find_first_descendant(parsed_body, :errors)
97
+ if errors == nil || errors.length == 0
98
+ code = find_first_descendant(parsed_body, :result_code)
99
+ description = find_first_descendant(parsed_body, :result_message)
100
+ else
101
+ errors = [errors] if errors.is_a?(Hash)
102
+ raise Errors::AgregateError.new(errors.map {|e| Errors::GenericError.new(e[:code], e[:description], response.status)})
103
+ end
104
+ end
105
+ end
106
+ raise Errors::GenericError.new(code, description, response.status) if code && description && code != '0' && code != 0
107
+ raise Errors::GenericError.new('', "Http code #{response.status}", response.status) if response.status >= 400
108
+ parsed_body
109
+ end
110
+
111
+ # Build url path like /accounts/<account-id>/<path>
112
+ def concat_account_path(path)
113
+ @concat_account_path.call(path)
114
+ end
115
+
116
+ # Return new configured connection object
117
+ # @return [Faraday::Connection] connection
118
+ def create_connection()
119
+ @create_connection.call()
120
+ end
121
+
122
+ def build_xml(data)
123
+ doc = build_doc(data)
124
+ doc.values.first.to_xml({:root => doc.keys.first, :skip_types => true, :indent => 0 })
125
+ end
126
+
127
+ protected
128
+
129
+ # Convert all keys of a hash to camel cased strings
130
+ def camelcase v
131
+ case
132
+ when v.is_a?(Array)
133
+ v.map {|i| camelcase(i)}
134
+ when v.is_a?(Hash)
135
+ result = {}
136
+ v.each do |k, val|
137
+ result[k.to_s().camelcase(:lower)] = camelcase(val)
138
+ end
139
+ result
140
+ else
141
+ v
142
+ end
143
+ end
144
+
145
+ # Convert all keys of hash to underscored symbols
146
+ def symbolize v
147
+ case
148
+ when v.is_a?(Array)
149
+ v.map {|i| symbolize(i)}
150
+ when v.is_a?(Hash)
151
+ result = {}
152
+ v.each do |k, val|
153
+ result[k.underscore().to_sym()] = symbolize(val)
154
+ end
155
+ result
156
+ else
157
+ v
158
+ end
159
+ end
160
+
161
+ def parse_xml(xml)
162
+ doc = ActiveSupport::XmlMini.parse(xml)
163
+ process_parsed_doc(doc.values.first)
164
+ end
165
+
166
+ def build_doc(v)
167
+ case
168
+ when v.is_a?(Array)
169
+ v.map {|i| build_doc(i)}
170
+ when v.is_a?(Hash)
171
+ result = {}
172
+ v.each do |k, val|
173
+ if k[0] != '_'
174
+ result[v["_#{k}XmlElement"] || (k.to_s().camelcase(:upper))] = build_doc(val)
175
+ end
176
+ end
177
+ result
178
+ else
179
+ v
180
+ end
181
+ end
182
+
183
+ def process_parsed_doc(v)
184
+ case
185
+ when v.is_a?(Array)
186
+ v.map {|i| process_parsed_doc(i)}
187
+ when v.is_a?(Hash)
188
+ return process_parsed_doc(v['__content__']) if v.keys.length == 1 && v['__content__']
189
+ result = {}
190
+ v.each do |k, val|
191
+ key = if k.downcase() == 'lata' then :lata else k.underscore().to_sym() end
192
+ result[key] = process_parsed_doc(val)
193
+ end
194
+ result
195
+ when v == "true" || v == "false"
196
+ v == "true"
197
+ when /^\d{4}\-\d{2}-\d{2}T\d{2}\:\d{2}\:\d{2}(\.\d{3})?Z$/.match(v)
198
+ DateTime.iso8601(v)
199
+ when /\A\d{9}\d?\Z/.match(v)
200
+ v
201
+ when /\A\d+\Z/.match(v)
202
+ Integer(v)
203
+ when /\A[-+]?[0-9]*\.?[0-9]+\Z/.match(v)
204
+ Float(v)
205
+ else
206
+ v
207
+ end
208
+ end
209
+
210
+ def find_first_descendant v, name
211
+ result = nil
212
+ case
213
+ when v.is_a?(Array)
214
+ v.each do |val|
215
+ result = find_first_descendant(val, name)
216
+ break if result
217
+ end
218
+ when v.is_a?(Hash)
219
+ v.each do |k, val|
220
+ if k == name
221
+ result = val
222
+ break
223
+ else
224
+ result = find_first_descendant(val, name)
225
+ break if result
226
+ end
227
+ end
228
+ end
229
+ result
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,28 @@
1
+ module BandwidthIris
2
+ # Allows to make first argument (client instance) of defined singletom function optional
3
+ module ClientWrapper
4
+ # Make first argument (client instance) of a method optional
5
+ #
6
+ # @param method [Symbol] singleton method name
7
+ # @example
8
+ # class MyClass
9
+ # extend ClientWrapper
10
+ #
11
+ # def self.do_something(client, arg1, arg2)
12
+ # end
13
+ #
14
+ # wrap_client_arg :do_something
15
+ # # Now you can make calls like MyClass.do_something(client, arg1, arg2) and MyClass.do_something(arg1, arg2)
16
+ # # In last case Client instance with default parameters (from Client.global_options) will be used
17
+ # end
18
+ def wrap_client_arg(method)
19
+ old = method(method)
20
+ define_singleton_method(method) do |*args|
21
+ if(args.size == 0 || !(args[0] || {}).is_a?(Client))
22
+ args.unshift(Client.new())
23
+ end
24
+ old.call(*args)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ module BandwidthIris
2
+ COVERED_RATE_CENTER_PATH = 'coveredRateCenters'
3
+ class CoveredRateCenter
4
+ extend ClientWrapper
5
+ include ApiItem
6
+
7
+ def self.list(client, query = nil)
8
+ list = client.make_request(:get, COVERED_RATE_CENTER_PATH, query)[0][:covered_rate_center]
9
+ return [] if !list
10
+ list = if list.is_a?(Array) then list else [list] end
11
+ list.map do |i|
12
+ CoveredRateCenter.new(i, client)
13
+ end
14
+ end
15
+ wrap_client_arg :list
16
+ end
17
+ end