Class: Interfax

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

Constant Summary collapse

WSDL_URL =
"https://ws.interfax.net/dfs.asmx?WSDL"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Interfax

Returns a new instance of Interfax.



6
7
8
9
10
11
12
# File 'lib/interfax.rb', line 6

def initialize(options = {})
  @username = options[:username] || ENV['INTERFAX_USERNAME']
  @password = options[:password] || ENV['INTERFAX_PASSWORD']
  @raw_response = {}
  @bytes_uploaded = 0
  Gyoku.convert_symbols_to(:camelcase)
end

Instance Method Details

#cancel_file_upload(options = {}) ⇒ Object



41
42
43
44
45
# File 'lib/interfax.rb', line 41

def cancel_file_upload(options = {})
  session_id = options.delete(:session_id) || @last_session_id
  return if session_id.nil?
  request :cancel_file_upload, :session_id => session_id
end

#clientObject



14
15
16
# File 'lib/interfax.rb', line 14

def client
  @client ||= ::Savon::Client.new(WSDL_URL)
end

#sendfax_ex_2(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/interfax.rb', line 47

def sendfax_ex_2(options = {})
  default_options = {
    :retries_to_perform => 1,
    :page_size => 'A4',
    :page_orientation => 'Portrait',
    :is_high_resolution => false,
    :is_fine_rendering => true,
  }    
  default_options.merge!(
    :file_types => File.extname(@file_path)[1..4].upcase,
    :file_sizes => "#{@bytes_uploaded}/sessionID=#{@last_session_id}"
  ) unless @bytes_uploaded.zero?
  options = default_options.merge(options)
  options[:is_high_resolution] = options[:is_high_resolution] ? 1 : 0 unless options[:is_high_resolution].is_a? Integer
  options[:is_fine_rendering] = options[:is_fine_rendering] ? 1 : 0 unless options[:is_fine_rendering].is_a? Integer
  request :sendfax_ex_2, options
end

#start_file_uploadObject



18
19
20
21
22
# File 'lib/interfax.rb', line 18

def start_file_upload
  @last_session_id = request :start_file_upload do |response|
    response[:session_id]
  end
end

#upload_file_chunk(file_path, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/interfax.rb', line 24

def upload_file_chunk(file_path, options = {})
  chunk_size = options.delete(:chunk_size) || 200000
  session_id = options.delete(:session_id) || @last_session_id || start_file_upload
  @file_path = file_path

  each_chunk(chunk_size) do |chunk, is_last|
    request :upload_file_chunk, {
      :chunk => Base64.encode64(chunk),
      :SessionID => session_id,
      :IsLast => is_last ? 1 : 0
    } do |response, result|
      @bytes_uploaded = result
    end
  end
  return @bytes_uploaded == File.size(@file_path)
end