diff --git a/examples/account.rb b/examples/account.rb index b3762f1..f4c49bc 100644 --- a/examples/account.rb +++ b/examples/account.rb @@ -11,5 +11,10 @@ :password => config['password'], :account_id => config['account_id'] } -puts BandwidthIris::Account.get() + +begin + puts BandwidthIris::Account.get() +rescue BandwidthIris::Errors::GenericError => e + puts e.message +end diff --git a/examples/available_npa_nxx.rb b/examples/available_npa_nxx.rb index b64bbc3..7b22c6a 100644 --- a/examples/available_npa_nxx.rb +++ b/examples/available_npa_nxx.rb @@ -13,5 +13,9 @@ } -puts BandwidthIris::AvailableNpaNxx.list({:state => 'NC', :quantity => 3}) +begin + puts BandwidthIris::AvailableNpaNxx.list({:state => 'NC', :quantity => 3}) +rescue BandwidthIris::Errors::GenericError => e + puts e.message +end diff --git a/examples/available_number.rb b/examples/available_number.rb index f72f126..5d5e6dc 100644 --- a/examples/available_number.rb +++ b/examples/available_number.rb @@ -13,5 +13,9 @@ } -puts BandwidthIris::AvailableNumber.list({:state => "NC", :quantity => 3}) +begin + puts BandwidthIris::AvailableNumber.list({:state => "NC", :quantity => 3}) +rescue BandwidthIris::Errors::GenericError => e + puts e.message +end diff --git a/examples/available_number_details.rb b/examples/available_number_details.rb index 1bd76ee..a0e7630 100644 --- a/examples/available_number_details.rb +++ b/examples/available_number_details.rb @@ -12,4 +12,8 @@ :account_id => config['account_id'] } -puts BandwidthIris::AvailableNumber.list({:state => "NC", :quantity => 3, :enable_t_n_detail => true}) +begin + puts BandwidthIris::AvailableNumber.list({:state => "NC", :quantity => 3, :enable_t_n_detail => true}) +rescue BandwidthIris::Errors::GenericError => e + puts e.message +end diff --git a/examples/city.rb b/examples/city.rb index db2330b..d5cf2a5 100644 --- a/examples/city.rb +++ b/examples/city.rb @@ -12,5 +12,9 @@ :account_id => config['account_id'] } -puts BandwidthIris::City.list({:available => true, :state => 'NC'}) +begin + puts BandwidthIris::City.list({:available => true, :state => 'NC'}) +rescue BandwidthIris::Errors::GenericError => e + puts e.message +end diff --git a/examples/config.yml.example b/examples/config.yml.example index d177d24..6b143dd 100644 --- a/examples/config.yml.example +++ b/examples/config.yml.example @@ -1,4 +1,4 @@ -api_endpoint: "https:/dashboard.bandwidth.com" +api_endpoint: "https://dashboard.bandwidth.com" account_id: "" user_name: "" password: "" diff --git a/examples/covered-rate-center.rb b/examples/covered-rate-center.rb index 59e72ac..f161ab8 100644 --- a/examples/covered-rate-center.rb +++ b/examples/covered-rate-center.rb @@ -12,8 +12,11 @@ :account_id => config['account_id'] } - -BandwidthIris::CoveredRateCenter.list({:zip => '27609', :page=>1, :size=>100}).each do |c| - puts c.to_data +begin + BandwidthIris::CoveredRateCenter.list({:zip => '27609', :page=>1, :size=>100}).each do |c| + puts c.to_data + end +rescue BandwidthIris::Errors::GenericError => e + puts e.message end diff --git a/examples/order.rb b/examples/order.rb index 0290eba..969e6df 100644 --- a/examples/order.rb +++ b/examples/order.rb @@ -14,35 +14,39 @@ number = '9195551212' #exisitng number for order +begin + site = BandwidthIris::Site.create({ + :name => "Ruby Test Site", + :description => "A Site From Ruby SDK Examples", + :address => { + :house_number => "123", + :street_name => "Anywhere St", + :city => "Raleigh", + :state_code =>"NC", + :zip => "27609", + :address_type => "Service" + } + }) + + order = BandwidthIris::Order.create({ + :name =>"A Test Order", + :site_id => site.id, + :existing_telephone_number_order_type => { + :telephone_number_list => + { + :telephone_number => [number] + } + + } + }) + + puts order.to_data + + order = BandwidthIris::Order.get(order.id) +rescue BandwidthIris::Errors::GenericError => e + puts e.message +end -site = BandwidthIris::Site.create({ - :name => "Ruby Test Site", - :description => "A Site From Ruby SDK Examples", - :address => { - :house_number => "123", - :street_name => "Anywhere St", - :city => "Raleigh", - :state_code =>"NC", - :zip => "27609", - :address_type => "Service" - } -}) - -order = BandwidthIris::Order.create({ - :name =>"A Test Order", - :site_id => site.id, - :existing_telephone_number_order_type => { - :telephone_number_list => - { - :telephone_number => [number] - } - - } -}) - -puts order.to_data - -order = BandwidthIris::Order.get(order.id) puts order.to_data diff --git a/examples/port-in.rb b/examples/port-in.rb index e27d3fe..7a6e73e 100644 --- a/examples/port-in.rb +++ b/examples/port-in.rb @@ -16,58 +16,62 @@ host = '1.1.2.3' +begin + site = BandwidthIris::Site.create({ + :name => "Ruby Test Site", + :description => "A Site From Ruby SDK Examples", + :address => { + :house_number => "123", + :street_name => "Anywhere St", + :city => "Raleigh", + :state_code =>"NC", + :zip => "27609", + :address_type => "Service" + } + }) -site = BandwidthIris::Site.create({ - :name => "Ruby Test Site", - :description => "A Site From Ruby SDK Examples", - :address => { - :house_number => "123", - :street_name => "Anywhere St", - :city => "Raleigh", - :state_code =>"NC", - :zip => "27609", - :address_type => "Service" - } -}) - -data = { - :peer_name => "A New SIP Peer", - :is_default_peer => true, - :short_messaging_protocol => "SMPP", - :site_id => site[:id], - :voice_hosts => - { - :host => { - :host_name => host - } - }, - :sms_hosts => - { - :host => { - :host_name => host - } - }, - :termination_hosts => + data = { + :peer_name => "A New SIP Peer", + :is_default_peer => true, + :short_messaging_protocol => "SMPP", + :site_id => site[:id], + :voice_hosts => { - :termination_host => { - :host_name => host, - :port => 5060, + :host => { + :host_name => host + } + }, + :sms_hosts => + { + :host => { + :host_name => host + } + }, + :termination_hosts => + { + :termination_host => { + :host_name => host, + :port => 5060, + } } - } -} + } -sip_peer = BandwidthIris::SipPeer.create(site[:id], data) + sip_peer = BandwidthIris::SipPeer.create(site[:id], data) -res = BandwidthIris::LnpChecker.check(number_to_check) + res = BandwidthIris::LnpChecker.check(number_to_check) -if res[:portable_numbers] && res[:portable_numbers][:tn] == number_to_check - puts 'Your number is portable. Creating PortIn Order' - port_in = BandwidthIris::PortIn.create(create_port_in_order(number_to_check, site, sip_peer)) - puts "Created order #{port_in[:id]}" - port_in.create_file(File.open('./loa.pdf', 'r'), 'application/pdf') + if res[:portable_numbers] && res[:portable_numbers][:tn] == number_to_check + puts 'Your number is portable. Creating PortIn Order' + port_in = BandwidthIris::PortIn.create(create_port_in_order(number_to_check, site, sip_peer)) + puts "Created order #{port_in[:id]}" + port_in.create_file(File.open('./loa.pdf', 'r'), 'application/pdf') + end +rescue BandwidthIris::Errors::GenericError => e + puts e.message end + def create_port_in_order(number, site, sip_peer) { :site_id => site[:id], diff --git a/examples/sip_peer.rb b/examples/sip_peer.rb index 4738102..d372418 100644 --- a/examples/sip_peer.rb +++ b/examples/sip_peer.rb @@ -14,47 +14,51 @@ host = '10.20.30.41' +begin + site = BandwidthIris::Site.create({ + :name => "Ruby Test Site", + :description => "A Site From Ruby SDK Examples", + :address => { + :house_number => "123", + :street_name => "Anywhere St", + :city => "Raleigh", + :state_code =>"NC", + :zip => "27609", + :address_type => "Service" + } + }) -site = BandwidthIris::Site.create({ - :name => "Ruby Test Site", - :description => "A Site From Ruby SDK Examples", - :address => { - :house_number => "123", - :street_name => "Anywhere St", - :city => "Raleigh", - :state_code =>"NC", - :zip => "27609", - :address_type => "Service" - } -}) - -data = { - :peer_name => "A New SIP Peer", - :is_default_peer => true, - :short_messaging_protocol => "SMPP", - :site_id => site[:id], - :voice_hosts => - { - :host => { - :host_name => host - } - }, - :sms_hosts => - { - :host => { - :host_name => host - } - }, - :termination_hosts => + data = { + :peer_name => "A New SIP Peer", + :is_default_peer => true, + :short_messaging_protocol => "SMPP", + :site_id => site[:id], + :voice_hosts => + { + :host => { + :host_name => host + } + }, + :sms_hosts => { - :termination_host => { - :host_name => host, - :port => 5060, + :host => { + :host_name => host + } + }, + :termination_hosts => + { + :termination_host => { + :host_name => host, + :port => 5060, + } } - } -} -sip_peer = BandwidthIris::SipPeer.create(site[:id], data) + } + sip_peer = BandwidthIris::SipPeer.create(site[:id], data) +rescue BandwidthIris::Errors::GenericError => e + puts e.message +end + sip_peer.delete(site[:id]) diff --git a/examples/site.rb b/examples/site.rb index 8131702..0b11667 100644 --- a/examples/site.rb +++ b/examples/site.rb @@ -12,18 +12,21 @@ :account_id => config['account_id'] } - -site = BandwidthIris::Site.create({ - :name => "Ruby Test Sitei1", - :description => "A Site From Ruby SDK Examples", - :address => { - :house_number => "123", - :street_name => "Anywhere St", - :city => "Raleigh", - :state_code =>"NC", - :zip => "27609", - :address_type => "Service" - } -}) +begin + site = BandwidthIris::Site.create({ + :name => "Ruby Test Sitei1", + :description => "A Site From Ruby SDK Examples", + :address => { + :house_number => "123", + :street_name => "Anywhere St", + :city => "Raleigh", + :state_code =>"NC", + :zip => "27609", + :address_type => "Service" + } + }) +rescue BandwidthIris::Errors::GenericError => e + puts e.message +end site.delete() diff --git a/examples/tn.rb b/examples/tn.rb index 73583ae..6b93809 100644 --- a/examples/tn.rb +++ b/examples/tn.rb @@ -13,8 +13,11 @@ :account_id => config['account_id'] } -list = BandwidthIris::Tn.list({:npa => '818', :page => 1, :size => 100}).each do |n| - puts n.to_data +begin + list = BandwidthIris::Tn.list({:npa => '818', :page => 1, :size => 100}).each do |n| + puts n.to_data + end + puts BandwidthIris::Tn.get(list[0][:full_number]).to_data +rescue BandwidthIris::Errors::GenericError => e + puts e.message end - -puts BandwidthIris::Tn.get(list[0][:full_number]).to_data diff --git a/lib/bandwidth-iris/errors.rb b/lib/bandwidth-iris/errors.rb index e5e4fc3..e2cbbc7 100644 --- a/lib/bandwidth-iris/errors.rb +++ b/lib/bandwidth-iris/errors.rb @@ -14,7 +14,7 @@ class GenericError < StandardError # return [Hash] Body attr_reader :body - # @return [String] Error code + # @return [String] Iris Error code attr_reader :code # @api private @@ -23,8 +23,8 @@ def initialize http_status, reason, headers, body @reason = reason @headers = headers @body = body - @code = '' # Iris Error Code can be accessed with body[:error][:code] - super message = "Http code #{@http_status}" + @code = body.nil? ? '' : body[:error][:code] + super message = "HTTP Error\nStatus Code: #{@http_status}\nReason: #{@reason}\nHTTP Headers: #{@headers}\nResponse Body: #{@body}\nIris Error Code: #{@code}" end end diff --git a/spec/bandwidth-iris/client_spec.rb b/spec/bandwidth-iris/client_spec.rb index 2486e6f..39b1cef 100644 --- a/spec/bandwidth-iris/client_spec.rb +++ b/spec/bandwidth-iris/client_spec.rb @@ -88,8 +88,13 @@ end it 'should raise error if http status >= 400' do - client.stubs.get('/v1.0/path1') { |env| [400, {}, ''] } - expect{client.make_request(:get, '/path1')}.to raise_error(Errors::GenericError, "Http code 400") + client.stubs.get('/v1.0/path1') { |env| [400, {'content-type'=>'application/xml'}, '4010The state abbreviation N is not valid.'] } + expect{client.make_request(:get, '/path1')}.to raise_error(an_instance_of(Errors::GenericError).and having_attributes({ + http_status: 400, + headers: {"content-type"=>"application/xml"}, + code: 4010, + body: {:error=>{:code=>4010, :description=>"The state abbreviation N is not valid."}} + })) end end end