teliaxr 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +20 -13
  2. data/lib/teliaxr.rb +154 -14
  3. metadata +5 -5
@@ -4,35 +4,42 @@
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- Unofficial ruby api to Teliax Dashboard, it will one day allow you to manage your teliax acct from
7
+ Unofficial ruby api to the Teliax Dashboard
8
8
 
9
9
 
10
10
  == FEATURES/PROBLEMS:
11
- It doesnt do much at the time
11
+ *current features*
12
+
13
+ # logs in to your teliax account using supplied credentials
14
+ # checks your account balance,
15
+ # list your current numbers
16
+ # list your destinations,
17
+ # change proxy settings for existing numbers
18
+ # change destination settings for existing numbers
19
+ # search availability for new numbers (not implemented)
12
20
 
13
- Can login to your teliax account and list your numbers
14
21
 
15
22
  Since it is unofficial and it relies on scrapping Teliax web sites it may broke if they change their website. YMMV
16
23
 
17
24
 
18
25
  == SYNOPSIS:
19
26
 
20
- teliax = Teliaxr::API.new(username, password)
21
- teliax.login
22
- teliax.numbers
23
- teliax.numbers_list
24
- 1234567890
25
- 1234567891
26
- 1234567892
27
+ +teliax = Teliaxr::API.new(username, password)+
28
+ teliax.account_balance
29
+
30
+ teliax.destinations
31
+ teliax.proxies
32
+ number teliax.numbers.first
33
+
34
+ teliax.set_proxy_for_number(number, "atlanta")
27
35
 
28
36
  == REQUIREMENTS:
29
37
 
30
- mechanize 0.9.3
31
- nokogiri
38
+ mechanize-0.9.3
32
39
 
33
40
  == INSTALL:
34
41
 
35
- sudo gem install teliaxr
42
+ sudo gem install teliaxr --source=http://gemcutter.org
36
43
 
37
44
  == LICENSE:
38
45
 
@@ -2,48 +2,187 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module Teliaxr
5
- VERSION = '0.0.1'
5
+ VERSION = '0.0.2'
6
6
  require 'rubygems'
7
7
  I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2=1
8
8
  require 'mechanize'
9
9
  require 'nokogiri'
10
10
 
11
11
  class API
12
- attr_reader :page
12
+ attr_reader :page, :account_number
13
+
14
+ # ====
15
+ # takes a username and password to log in to the teliax dashboard
13
16
  def initialize(username, password, url=nil)
14
17
  @username = username
15
18
  @password = password
16
- @url = url
17
- @url ||= "https://www.teliax.com"
19
+ @teliax_url = url
20
+ @teliax_url ||= "https://www.teliax.com"
18
21
  @agent = WWW::Mechanize.new
19
- get_main_page
22
+ @numbers_list = {}
23
+ @destinations_list = {}
24
+ @proxies = {
25
+ "denver" => {:proxy_url => "den.teliax.net", :proxy_id => "1"},
26
+ "new york" => {:proxy_url => "nyc.teliax.net", :proxy_id => "2"},
27
+ "los angeles" => {:proxy_url => "lax.teliax.net", :proxy_id => "3"},
28
+ "atlanta" => {:proxy_url => "atl.teliax.net", :proxy_id => "4"},
29
+ "fax" => {:proxy_url => "fax.teliax.net", :proxy_id => "7"}
30
+ }
31
+ @balance = nil
32
+ @account = nil
33
+
34
+ get_page(@teliax_url)
35
+ login
36
+ account_balance
37
+ account_number
38
+ end
39
+
40
+ private
41
+ def click_link_by_name(name)
42
+ #clicks link by name
43
+ @page = @agent.click( @page.links.select {|l| l.text =~/#{name}/}.first )
44
+ end
45
+
46
+ def get_link_by_name(name)
47
+ @page.links.select {|l| l.text =~/#{name}/}.first
20
48
  end
21
49
 
22
- def get_main_page
23
- @page = @agent.get @url
50
+ def get_page(url)
51
+ @page = @agent.get url
24
52
  end
25
53
 
54
+
26
55
  def login
56
+ #i should check that i am on the login page first
27
57
  form = @page.forms.first
28
58
  form['login'] = @username
29
59
  form['password'] = @password
30
- form.submit
31
60
  @page = form.submit
32
-
33
61
  end
62
+
63
+ def url_for_number(nr)
64
+ "#{@teliax_url}/customers/#{@account}/numbers/#{numbers[nr][:number_id]}"
65
+ end
66
+
67
+ public
68
+
69
+ # ====
70
+ # returns account balance as float
71
+ def account_balance(force_update=false)
72
+ #should check if force update has been requested
73
+ return @balance unless @balance.nil?
74
+ @balance = @page.search("div#site_left_nav_headerBlack a").first.content.gsub(/Balance \$/,'').to_f
75
+ end
34
76
 
35
- def numbers
36
- @page = @agent.click( @page.links.select {|l| l.text =~/Numbers/}.first )
77
+
78
+ # ====
79
+ # returns account number
80
+ def account_number(force_update=false)
81
+ return @account unless @account.nil?
82
+ @account = @page.search("div#site_left_nav_headerBlack").text.to_a.last.gsub(/\D/,'')
37
83
  end
38
84
 
39
- def numbers_list
85
+
86
+ # ====
87
+ # returns numbers list as an array, pass parameter force_reload to update list
88
+ def numbers(force_reload=false)
89
+ unless @numbers_list.empty?
90
+ if force_reload == false
91
+ return @numbers_list
92
+ end
93
+ end
94
+
95
+ #puts "RELOADING: force: #{force_reload} list.empty?: #{@numbers_list.empty?}"
96
+
97
+ click_link_by_name "Numbers" #goes to the numbers website
98
+ @numbers_list={}
40
99
  @page.search('div#site_content_container_center table tbody tr').each do |tr|
41
- tr.search('td:nth-child(1)').each do |td|
42
- puts td.content
100
+
101
+ @numbers_list[tr.search('td:nth-child(1)').first.content.gsub(/\D/,'')] = {
102
+ :number_id => tr.search('td:nth-child(3) @id').text.split("_").last.strip,
103
+ :gateway_id => tr.search('td:nth-child(3) option[selected] @value'),
104
+ :gateway_name => tr.search('td:nth-child(3) option[selected]').text,
105
+ :destination_id => tr.search('td:nth-child(4) option[selected] @value'),
106
+ :destination_name => tr.search('td:nth-child(4) option[selected]').text.split(":").last.strip
107
+ }
43
108
  end
109
+ @numbers_list
110
+ end
111
+
112
+
113
+ # ====
114
+ # returns current destination for this number
115
+ def destination_for_number(nr,force_reload=false)
116
+ numbers(force_reload)[nr][:destination_name]
117
+ end
118
+
119
+
120
+ # ====
121
+ # sets rings to destination for this number, weather is a device or another ring number
122
+ def set_destination_for_number(nr,destination_name)
123
+
124
+ @page = @agent.post(url_for_number(nr), {"number[destination]" => destinations[destination_name][:destination_id], "_method" => "put"})
125
+ end
126
+
127
+ # ====
128
+ # returns proxy that is setup to originate calls to this number
129
+ def proxy_for_number(nr,force_reload=false)
130
+ numbers(force_reload)[nr][:gateway_name]
131
+ end
132
+
133
+ # ====
134
+ # set the proxy this number will be get originated from
135
+ def set_proxy_for_number(nr,proxy_name)
136
+ @page = @agent.post(url_for_number(nr), {"number[proxy_id]" => proxies[proxy_name][:proxy_id], "_method" => "put"})
137
+ end
138
+
139
+ # ====
140
+ # returns list of teliax proxies
141
+ def proxies
142
+ @proxies
143
+ end
144
+
145
+ # ====
146
+ # returns list of teliax destinations
147
+ def destinations #
148
+ unless @destinations_list.empty?
149
+ return @destinations_list
150
+ end
151
+
152
+ click_link_by_name "Numbers" #goes to the numbers website
153
+ @destinations_list={}
154
+ @page.search('div#site_content_container_center table tbody tr:first-of-type td:nth-child(4) option').each do |opt|
155
+ destination_id = opt.search('@value').text
156
+
157
+ if destination_id == "_"
158
+ destination_type = "nowhere"
159
+ destination_name = opt.text
160
+ else
161
+ destination_type = destination_id.split("_").first.downcase.strip
162
+
163
+ if destination_type == "number"
164
+ destination_name = opt.text.gsub(/\D/,'')
165
+ else
166
+ destination_name = opt.text.split(":").last.strip
167
+ end
168
+ end
169
+ #puts destination_type
170
+ #puts destination_name
171
+ #puts destination_id
172
+
173
+ @destinations_list[destination_name] = {:destination_id => destination_id, :destination_type => destination_type }
174
+
44
175
  end
176
+ @destinations_list
45
177
  end
46
178
 
179
+ # ====
180
+ # returns list of new numbers based on area code
181
+ def search_for_new_numbers(area_code)
182
+ end
183
+
184
+ # ===
185
+ # returns list of links this is test code
47
186
  def get_menu_links
48
187
  @page.search('div#site_left_nav_content div').each do |nav|
49
188
  nav.search('ul li a').each do |link|
@@ -53,4 +192,5 @@ module Teliaxr
53
192
  end
54
193
 
55
194
  end
195
+
56
196
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: teliaxr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rene Mendoza
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-23 00:00:00 -05:00
12
+ date: 2009-10-25 01:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 2.3.3
34
34
  version:
35
- description: Unofficial ruby api to Teliax Dashboard, it will one day allow you to manage your teliax acct from
35
+ description: Unofficial ruby api to the Teliax Dashboard
36
36
  email:
37
37
  - latiendamac@gmail.com
38
38
  executables: []
@@ -80,10 +80,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  requirements: []
81
81
 
82
82
  rubyforge_project: teliaxr
83
- rubygems_version: 1.3.3
83
+ rubygems_version: 1.3.5
84
84
  signing_key:
85
85
  specification_version: 3
86
- summary: Unofficial ruby api to Teliax Dashboard, it will one day allow you to manage your teliax acct from
86
+ summary: Unofficial ruby api to the Teliax Dashboard
87
87
  test_files:
88
88
  - test/test_helper.rb
89
89
  - test/test_teliaxr.rb