elastix 0.0.3 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZmY3ZWE4YzIzZjcwZTk3OGUzOTczYzM3NmQ2NGIwYjYwYzkzOTNjMg==
4
+ MTExMDJiNmNjNjM3NmZhMTE1MmEwYzdkODAwODExODFlY2ZiZTI5Yg==
5
5
  data.tar.gz: !binary |-
6
- OWZmMjIyOTZiODkyNTMyNWI4NjI1NjRmN2JjMzAzNzBlYWMwNDBkNQ==
6
+ YmE3ZmVkZWFmMWUxZTAxYTgzMDEwYWJjOWRiMTIyOGE2NGIxOGNiZg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZDgzMTgyOTQ4ODA4NjdkNzJmODZlNjQ5MzMzZDExMzViYjdjODc4OTUwYzFh
10
- N2UyNTNlYmNjMDQ5YmY3NzZmYjQ2NjY3OTViZDg0ZGNhNDZiM2NmOTVlZTcy
11
- ODBmMjU2Nzg1MTkzMjI3OGVhZWM0NDllM2QzYjlkN2U4NDdjOGU=
9
+ OWE3OGRjYjgxYmRlYTVkNzRmNTVhYzBlMGZiYzAzNjViNGEyNzk2ODUxZjcz
10
+ YzBhODQ1YjUyYzViMzM0MmYwYWM0OTE3NDdlZjU4MjNmMjQzZDA1NmRlOWNk
11
+ MDEyOWI4MTYyNTk5NDJiMzQ5YWIxNGQyM2Q2ZjM1ZmUxM2RmYjI=
12
12
  data.tar.gz: !binary |-
13
- YzU2ZTUxMGU2MWQ1ZTc4NzgyNjVjNTcxNDZlMjE4NjM4ZDNjNzM1MDA2MWI2
14
- ZDVlZjMzZjJiYzhjMDRhOGEyNzVhNjhhNGE5YmQ5YjQ4MmMyYTI5N2RhNzQ0
15
- ZmEwZDZhNDFjNDlhMzhlMzdkMTkyYTg4ZTEzZGE4ZjEyZWY2YTk=
13
+ ZWU3OTJhMWI2OTkzYzU1ZWJmYWM4YWE3ZDI2MjBjOTNlOTliNWMzNDUyMTFj
14
+ MTYxOTZkMWVmZTRmMjBkM2Q5OWE3NmE2ZWQ1YWQ4ODA3OTAwNzczNzE2NjEx
15
+ YzNkZTE5NjJiMWQ5ZDEyZDJmMWVmZWIxZjIyNWJjNTIzMGZhNDU=
data/README.md CHANGED
@@ -40,7 +40,7 @@ Here is a list of methods that have been implemented:
40
40
  * all
41
41
  * ==
42
42
  * to\_hash
43
- * update\_attributes
43
+ * update\_attributes!
44
44
 
45
45
  ## Contributing
46
46
 
data/elastix.gemspec CHANGED
@@ -19,4 +19,6 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency 'mechanize', '~> 2.7'
22
+ spec.add_dependency 'activerecord', '~> 3.2'
23
+ spec.add_dependency 'mysql2'
22
24
  end
data/lib/elastix.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "elastix/version"
2
2
  require "elastix/base"
3
3
  require "elastix/extension"
4
+ require "elastix/sip"
5
+ require "elastix/user"
4
6
 
5
7
  module Elastix
6
8
  # Your code goes here...
data/lib/elastix/base.rb CHANGED
@@ -1,25 +1,53 @@
1
1
  require 'mechanize'
2
2
  module Elastix
3
- attr_accessor :elastix
4
3
  class Base
5
- def self.establish_connection( ip, username, password)
6
- $elastix = Mechanize.new
7
- $elastix.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
8
- $base_address = "https://#{ip}"
9
- $elastix.get($base_address)
10
- login_form = $elastix.page.forms[0]
11
- login_form.input_user = username
12
- login_form.input_pass = password
13
- $elastix.submit(login_form,login_form.button_with("submit_login"))
4
+ def self.establish_web_connection host, username, password
5
+ @@elastix = Mechanize.new
6
+ @@elastix.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
7
+ @@base_address = "https://#{host}"
8
+ login username, password
14
9
  end
10
+
15
11
 
16
- def self.close_connection
17
- $base_address = nil
18
- $elastix = nil
12
+ def self.establish_db_connection(host, username, password)
13
+ ActiveRecord::Base.establish_connection(
14
+ :adapter => "mysql2",
15
+ :host => host,
16
+ :database => "asterisk",
17
+ :username => username,
18
+ :password => password,
19
+ )
20
+ end
21
+
22
+ def self.establish_connection(web_options={}, db_options={})
23
+ establish_web_connection(web_options[:host], web_options[:username], web_options[:password])
24
+ establish_db_connection(db_options[:host], db_options[:username], db_options[:password])
25
+ end
26
+
27
+ def self.close_db_connection
28
+ ActiveRecord::Base.connection.close
29
+ end
30
+
31
+ def self.use_existing_db_connection connection
32
+ establish_connection connection
33
+ end
34
+
35
+ def self.close_db_connection
36
+ @@base_address = nil
37
+ @@elastix = nil
19
38
  end
20
39
 
21
40
  def self.reload
22
- $elastix.get("#{$base_address}/config.php?handler=reload")
41
+ @@elastix.get("#{@@base_address}/config.php?handler=reload")
23
42
  end
43
+
44
+ private
45
+ def self.login username, password
46
+ @@elastix.get(@@base_address)
47
+ login_form = @@elastix.page.forms[0]
48
+ login_form.input_user = username
49
+ login_form.input_pass = password
50
+ @@elastix.submit(login_form,login_form.button_with("submit_login"))
51
+ end
24
52
  end
25
53
  end
@@ -1,106 +1,93 @@
1
1
  module Elastix
2
- class Extension
2
+ class Extension < Base
3
3
  attr_accessor :extension, :name, :sipname, :outboundcid, :devinfo_secret
4
4
 
5
- def initialize(params)
5
+ def initialize params
6
6
  params.each_pair{|key,value| instance_variable_set "@#{key}", value}
7
7
  end
8
8
 
9
9
  def destroy
10
- $elastix.get("#{$base_address}/config.php?type=setup&amp;display=extensions&amp;extdisplay=#{self.extension}&amp;action=del")
10
+ @@elastix.get("#{@@base_address}/config.php?type=setup&amp;display=extensions&amp;extdisplay=#{self.extension}&amp;action=del")
11
11
  Base.reload
12
12
  end
13
13
 
14
14
  def save
15
- raise "Extension already exists" if Extension.exist? self.extension
16
- Extension.new_extension_object(self.to_hash)
15
+ if Extension.exist? extension
16
+ update_extension_object
17
+ else
18
+ new_extension_object
19
+ end
17
20
  Base.reload
18
21
  end
19
22
 
20
- def self.create(params)
21
- e = Extension.new(params)
22
- e.save
23
- e
24
- end
25
-
26
- def self.all
27
- #TODO abstract this method
28
- page = $elastix.get("#{$base_address}/index.php?menu=pbxconfig")
29
- extensions = []
30
- page.links.each do |link|
31
- extensions << Extension.find(get_extension_from_link_text(link.text)) if href_is_acceptable? link.href
32
- end
33
- extensions
34
- end
35
-
36
- def update_attributes!(params)
37
- params[:extension] = self.extension
38
- Extension.update_extension_object(params)
23
+ def update_attributes params
24
+ params.each_pair{|key,value| instance_variable_set "@#{key}", value}
25
+ self.update_extension_object
39
26
  Base.reload
40
27
  end
41
28
 
42
- def self.find(extension)
43
- get_extension_object(extension) if exist?(extension)
44
- end
45
-
46
- def ==(extension)
29
+ def == extension
47
30
  self.to_hash == extension.to_hash
48
31
  end
49
32
 
50
33
  def to_hash
51
34
  Hash[self.instance_variables.map{|var| [var.to_s.delete("@"), self.instance_variable_get(var)]}]
52
- #my_hash.symbolize_keys
53
35
  end
54
36
 
37
+
38
+ def self.find extension
39
+ get_extension_object(extension) if exist?(extension)
40
+ end
41
+
42
+ def self.create params
43
+ e = Extension.new(params)
44
+ e.save
45
+ e
46
+ end
47
+
48
+ def self.all
49
+ Sip.uniq.pluck(:id)
50
+ end
51
+
55
52
  private
56
- def self.get_extension_object(extension)
57
- Extension.new(get_extension_attributes(get_extension_display_page(extension).forms.first))
58
- end
59
-
60
- def self.update_extension_object(params)
61
- page = get_extension_display_page(params[:extension])
62
- update_and_submit_form(page, params)
53
+ def update_extension_object
54
+ page = Extension.get_extension_display_page(self.extension)
55
+ update_and_submit_form(page, self.to_hash)
63
56
  end
64
57
 
65
- def self.new_extension_object(params)
66
- page = get_extension_display_page(params[:extension])
58
+ def new_extension_object
67
59
  #This is necessary for new objects. It would be nice to figure out why.
60
+ page = @@elastix.get("#{@@base_address}/index.php?menu=pbxconfig")
68
61
  page.encoding = "utf-8"
69
62
  form = page.form("frm_extensions")
70
63
  page = form.submit(form.button_with("Submit"))
71
- update_and_submit_form(page, params)
64
+ update_and_submit_form(page, self.to_hash)
72
65
  end
73
66
 
74
- def self.update_and_submit_form(page, params)
67
+ def update_and_submit_form(page, params)
75
68
  form = page.form("frm_extensions")
76
69
  form.encoding = "utf-8"
77
70
  params.each{|key,value| form[key.to_s] = value unless value.nil?}
78
- $elastix.submit(form, form.button_with("Submit"))
71
+ @@elastix.submit(form, form.button_with("Submit"))
79
72
  end
80
73
 
81
- def self.get_extension_display_page(extension)
82
- $elastix.get "#{$base_address}/config.php?type=setup&display=extensions&extdisplay=#{extension}"
74
+ def self.get_extension_object(extension)
75
+ Extension.new(get_extension_attributes(extension))
83
76
  end
84
77
 
85
- def self.get_extension_attributes(form)
86
- Hash[form.fields.map{|field| [field.name, field.value] if is_a_class_attribute(field.name) and not field.value.empty?}]
78
+ def self.get_extension_display_page(extension)
79
+ @@elastix.get "#{@@base_address}/config.php?type=setup&display=extensions&extdisplay=#{extension}"
87
80
  end
88
81
 
89
- def self.is_a_class_attribute attr
90
- Extension.instance_methods.grep(/\w=$/).include? "#{attr}=".to_sym
82
+ #TODO make it easier to add fields. Right now it's just hardcoded what is returned.
83
+ def self.get_extension_attributes extension
84
+ secret = Sip.where(id: extension, keyword: "secret").first.data
85
+ user_fields = User.find_by_extension(extension)
86
+ {extension: extension, name: user_fields.name, sipname: user_fields.sipname, outboundcid: user_fields.outboundcid, devinfo_secret: secret}
91
87
  end
92
88
 
93
89
  def self.exist? extension
94
- if $elastix.get("#{$base_address}/index.php?menu=pbxconfig")
95
- .body.match(/#{extension}/) then true else false end
96
- end
97
-
98
- def self.href_is_acceptable?(href)
99
- href.include? "config.php?type=setup&display=extensions&extdisplay=" unless href.nil?
100
- end
101
-
102
- def self.get_extension_from_link_text(link_text)
103
- link_text.scan(/<(.*)>/).first.first
90
+ !!Sip.find_by_id(extension)
104
91
  end
105
92
  end
106
93
  end
@@ -0,0 +1,7 @@
1
+ require 'active_record'
2
+
3
+ module Elastix
4
+ class Sip < ActiveRecord::Base
5
+ self.table_name = "sip"
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module Elastix
2
+ class User < ActiveRecord::Base
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module Elastix
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -8,7 +8,7 @@ describe Extension do
8
8
  }
9
9
  }
10
10
 
11
- before { Base.establish_connection $auth_options[:ip], $auth_options[:username], $auth_options[:password] }
11
+ before(:all) { Base.establish_connection $auth_options[:ip], $auth_options[:username], $auth_options[:password] }
12
12
  after(:all){ Base.close_connection }
13
13
  after(:each) do
14
14
  Extension.new(params).destroy if Extension.find(params["extension"])
@@ -97,7 +97,7 @@ describe Extension do
97
97
  describe ".update_attributes" do
98
98
  it "should only update the attributes that are input" do
99
99
  e = Extension.create(params)
100
- e.update_attributes!("sipname" => "testing")
100
+ e.update_attributes("sipname" => "testing")
101
101
  new_ext = Extension.find(e.extension)
102
102
  e.to_hash.should eq new_ext.to_hash
103
103
  end
data/spec/spec_helper.rb CHANGED
@@ -3,5 +3,5 @@ require 'elastix'
3
3
  include Elastix
4
4
 
5
5
  $auth_options = {
6
- ip: "ipaddress", username: "username", password: "password"
6
+ ip: "192.168.1.227", username: "admin", password: "x0ctallc0x"
7
7
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Hahn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-22 00:00:00.000000000 Z
11
+ date: 2013-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mysql2
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  description: ! ' A wrapper of the Elastix Call Center Module that deals primarily
28
56
  with extension management.'
29
57
  email:
@@ -42,6 +70,8 @@ files:
42
70
  - lib/elastix.rb
43
71
  - lib/elastix/base.rb
44
72
  - lib/elastix/extension.rb
73
+ - lib/elastix/sip.rb
74
+ - lib/elastix/user.rb
45
75
  - lib/elastix/version.rb
46
76
  - spec/base_spec.rb
47
77
  - spec/extension_spec.rb