Skip to content

Commit 2526012

Browse files
author
Corny K
committed
first commit
0 parents  commit 2526012

File tree

7 files changed

+180
-0
lines changed

7 files changed

+180
-0
lines changed

MIT-LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2009 Digineo GmbH, Germany
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Configuration
2+
================
3+
4+
Sipgate.user = "your-account"
5+
Sipgate.password = "your-password"
6+
7+
8+
Usage
9+
============
10+
11+
12+
sip = Sipgate.new
13+
14+
# send a pdf
15+
result = sip.fax(4912345678, File.read("document.pdf"))
16+
17+
# check sending status
18+
status = sip.status(result.session_id)
19+
20+
21+
22+
23+
Copyright (c) 2009 Digineo GmbH (http://www.digineo.de/), released under the MIT license

init.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'sipgate'
2+
require 'response'
3+
require 'error'

lib/error.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Sipgate::Exception < Exception
2+
3+
attr_reader :code
4+
5+
def initialize(message,code)
6+
@code = code
7+
super(message)
8+
end
9+
10+
end

lib/response.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Sipgate::Response
2+
3+
CODE_SUCCESS = 200
4+
5+
def initialize(response)
6+
@response = response
7+
end
8+
9+
def success?
10+
status_code == CODE_SUCCESS
11+
end
12+
13+
def session_id
14+
@response[:session_id]
15+
end
16+
17+
def session_status
18+
@response[:session_status]
19+
end
20+
21+
def status_code
22+
@response[:status_code]
23+
end
24+
25+
def status_string
26+
@response[:status_string]
27+
end
28+
29+
# gibt den Status eines Fax-Versandes zurück. Mögliche Antworten:
30+
# :pending, :sent, :failed
31+
def sending_status
32+
case session_status
33+
when "sending", "queued"
34+
:pending
35+
when "sent"
36+
:sent
37+
when "error during submit", "failed"
38+
:failed
39+
else
40+
raise "Konnte keinen Status aus Antwort ermitteln: #{inspect}"
41+
end
42+
end
43+
44+
end

lib/sipgate.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'xmlrpc/client'
2+
3+
class Sipgate
4+
5+
cattr_accessor :user, :password
6+
7+
def initialize
8+
@client = XMLRPC::Client.new2("https://#{self.class.user}:#{self.class.password}@samurai.sipgate.net/RPC2")
9+
http = @client.instance_variable_get(:@http)
10+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
11+
http.ca_path = '/etc/ssl/certs'
12+
end
13+
14+
# versendet ein Fax
15+
def fax(number, pdf_message)
16+
number = number.gsub(/^(\+)/, "").gsub(/\s+/, "")
17+
18+
call "samurai.SessionInitiate",
19+
'RemoteUri' => "sip:#{number}@sipgate.net",
20+
'TOS' => 'fax',
21+
'Content' => Base64.encode64(pdf_message)
22+
end
23+
24+
# fragt den Status eines Faxes ab
25+
def status(session_id)
26+
call "samurai.SessionStatusGet",
27+
'SessionID' => session_id
28+
end
29+
30+
def call(*args)
31+
response = Response.new(rubyized_hash(@client.call(*args)))
32+
33+
unless response.success?
34+
raise Sipgate::Exception.new(response.status_string, response.status_code)
35+
end
36+
37+
response
38+
end
39+
40+
# make server responses look more Ruby like (underscored Symbol as Hash keys)
41+
# source: http://github.com/martinrehfeld/telefon/blob/7133964255eefb4679d8759f467ec2f749459aaa/app/models/sipgate.rb
42+
def rubyized_hash(h)
43+
returning new_hash = {} do
44+
h.each do |k,v|
45+
new_val = if v.is_a?(Hash)
46+
rubyized_hash(v)
47+
elsif v.is_a?(Array)
48+
v.map{|e| e.is_a?(Hash) ? rubyized_hash(e) : e}
49+
else
50+
v
51+
end
52+
new_hash[k.to_s.underscore.to_sym] = new_val
53+
end
54+
end
55+
end
56+
57+
end

sipgate.gemspec

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
Gem::Specification.new do |s|
3+
s.name = %q{sipgate}
4+
s.version = "0.0.1"
5+
6+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
7+
s.authors = ["Corny", "Averell"]
8+
s.date = %q{2009-08-21}
9+
s.description = %q{send fax through sipgate and check their sending status.}
10+
s.email = %q{[email protected]}
11+
s.files = %w(
12+
MIT-LICENSE
13+
README.rdoc
14+
init.rb
15+
lib/sipgate.rb
16+
lib/response.rb
17+
lib/error.rb
18+
)
19+
s.has_rdoc = true
20+
s.homepage = %q{http://github.com/corny/sipgate}
21+
s.require_paths = ["lib"]
22+
s.rubygems_version = %q{1.3.0}
23+
end

0 commit comments

Comments
 (0)