Class: Bandwidth

Inherits:
Object
  • Object
show all
Defined in:
lib/bandwidth/bandwidth.rb

Constant Summary collapse

API_METHODS =

Available REST API methods

[ :get_telephone_number,
:get_number_order,
:get_number_orders,
:area_code_number_search,
:npa_nxx_number_search,
:rate_center_number_search,
:rate_center_number_order,
:tollfree_number_search,
:basic_number_order,
:change_number,
:sip_trunk_order,
:consumed_numbers,
:reserve_numbers,
:pbod,
:rate_center_block_order,
:get_rate_center_block_order,
:get_rate_center_block_orders,
:get_cdr_archive ]

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Object

Instantiate a Bandwdith object

Examples:

Instantiate a Bandwdith object

require 'rubygems'
require 'bandwidth'
bandwdith = Bandwidth.new(:developer_key => 'test')

Parameters:

  • params (required, Hash)

Options Hash (params):

  • :developer_key (required, String)

    assigned to you by Bandwidth

  • :processing_type (optional, Boolean)

    whether to ‘process’ the request or only ‘validate’, default is ‘process’

  • :log_level (optional, Symbol)

    which level to set the logging at, off by default

  • :use_lab_uris (optional, Boolean)

    if true, will use the Bandwidth Lab URIs rather than production

Raises:

  • ArgumentError when the :developer_key is not present



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bandwidth/bandwidth.rb', line 36

def initialize(params)
  raise ArgumentError, ":developer_key required" if params[:developer_key].nil?      
  
  if params[:log_level]
    HTTPI.log_level = params[:log_level]
  else
    HTTPI.log = false
  end
  
  @use_labs_uris   = params[:use_lab_uris] || false
  @developer_key   = params[:developer_key]
  @numbers_request = create_request(:numbers, params)
  @cdrs_request    = create_request(:cdrs, params)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, params = {}) ⇒ Hashie::Mash Object

Provides the dispatcher to the available REST methods on the Bandwidth API

Examples:

Retrieve numbers available in an area code

bandwidth.area_code_number_search :area_code => '720', :max_quantity => 10

Parameters:

  • the (required, Symbol)

    method name to invoke on the REST API

  • the (optional, Hash)

    parameters to pass to the method, should be symbols and may be all lowercase with underscores or camelCase

Returns:

  • (Hashie::Mash Object)

    containing the results of the REST call

Raises:

  • NoMethodError if the method requested is not defined in the API_METHODS constant



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bandwidth/bandwidth.rb', line 60

def method_missing(method_name, params={})
  raise NoMethodError, "The method #{method_name.to_s} does not exist." if API_METHODS.include?(method_name) == false
  
  if method_name == :get_cdr_archive
    @cdrs_request.body = build_xml(method_name, params)
                                                                 
    response = HTTPI.post @cdrs_request
    Hashie::Mash.new({ :code    => response.code,
                       :body    => response.raw_body,
                       :headers => response.headers })
  else
    @numbers_request.body = build_xml(method_name, params)                                  
    response = HTTPI.post @numbers_request
    Hashie::Mash.new({ :code    => response.code,
                       :body    => Crack::XML.parse(response.raw_body),
                       :headers => response.headers })
  end
end

Instance Method Details

#build_xml(method_name, params) ⇒ String

Builds the XML document for the method call

Parameters:

  • the (required, Symbol)

    method name to invoke on the REST API

  • the (optional, Hash)

    parameters to pass to the method, should be symbols and may be all lowercase with underscores or camelCase

Returns:

  • (String)

    the resulting XML document



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bandwidth/bandwidth.rb', line 85

def build_xml(method_name, params)
  builder_params = params.merge({ :developer_key => @developer_key })
  
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.send(method_name.to_s.camelize.uncapitalize.to_sym, xml_namespaces) do
      builder_params.each do |parent_k, parent_v|
        if parent_v.instance_of?(Array)
          xml.send(parent_k.to_s.camelize.uncapitalize.to_sym) do
            parent_v.each do |item|
              item.each do |item_k, item_v|
                symbol = (item_k.to_s + '_').to_sym
                xml.send(symbol, item_v)
              end
            end
          end
        else
          xml.send(parent_k.to_s.camelize.uncapitalize.to_sym, parent_v)
        end
      end
    end
  end
  builder.to_xml
end