Class: CTM::NumberList

Inherits:
List
  • Object
show all
Defined in:
lib/ctm/number_list.rb

Instance Attribute Summary

Attributes inherited from List

#filters, #list_type, #objects, #page, #per_page, #token, #total_entries, #total_pages

Instance Method Summary collapse

Methods inherited from List

#create, #each, #each_page, #find, #get

Constructor Details

#initialize(options = {}, token = nil) ⇒ NumberList

Returns a new instance of NumberList.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/ctm/number_list.rb', line 3

def initialize(options={}, token=nil)
  super('Number', options, token)
  @source_id = options[:source_id]
  if @source_id && @account_id
    @list_type_path = "accounts/#{@account_id}/sources/#{@source_id}/#{@list_token_type}"
  elsif @account_id
    @list_type_path = "accounts/#{@account_id}/#{@list_token_type}"
  else
    @list_type_path = @list_token_type
  end
end

Instance Method Details

#add(number) ⇒ Object

add trackng number to tracking source



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ctm/number_list.rb', line 49

def add(number)
  path_str = "/api/v1/#{@list_type_path}/#{number.id}/add.json"
  #puts "Add to #{@account_id}:#{@source_id} -> #{number.id} -> #{path_str}"
  #         accounts/25          /sources/5012     /numbers
  # /api/v1/accounts/:account_id/sources/:source_id/numbers/:id/add
  res = self.class.post(path_str, :body => {}.merge(:auth_token => @token))
  if res && res['status'] == 'success'
    CTM::Source.new(res['source'], @token)
  else
    puts res.inspect
    raise CTM::Error::Add.new(res["reason"])
  end
end

#buy(digits, options = {}) ⇒ Object

buy number with the digits



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ctm/number_list.rb', line 33

def buy(digits, options={})
  path_str = "/api/v1/#{@list_type_path}.json"
  res = self.class.post(path_str, :body => {:phone_number => digits}.merge(options).
                                                                     merge(:auth_token => @token))
  if res && res['status'] == 'success'
    CTM::Number.new(res['number'], @token)
  else
    options = {}
    options[:test] = 1 if Rails.env != 'production'
    number = .numbers.buy(number_to_buy, options)
    puts res.inspect
    raise CTM::Error::Buy.new(res["reason"])
  end
end

#search(country_code, options = {}) ⇒ Object

find a number within the given country/region and options like area_code or containing with contains



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ctm/number_list.rb', line 16

def search(country_code, options={})
  options = {:country_code => country_code}.merge(options)
  path_str = "/api/v1/#{@list_type_path}/search.json"
  res = self.class.get(path_str, :query => options.merge(:auth_token => @token))
  data = res.parsed_response
  if data["status"] == "success"
    list_data = {'available_numbers' => data['results'].map {|res| res.merge('account_id' => @account_id) },
                 'page' => 1,
                 'per_page' => data['results'].size,
                 'total_entries' => data['results'].size}
    CTM::List.new('AvailableNumber', {:account_id => @account_id}, @token, list_data)
  else
    raise CTM::Error::List.new(data["error"])
  end
end