Created
April 6, 2011 11:26
-
-
Save rubiii/905499 to your computer and use it in GitHub Desktop.
Savon InterFAX example: Get an inbound fax image in chunks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# more examples at: | |
# https://gist.github.com/903337 | |
require "rubygems" | |
require "base64" | |
# $ gem install savon (works with v0.9.1 and higher) | |
require "savon" | |
# convert hash key symbols to camelcase | |
Gyoku.convert_symbols_to(:camelcase) | |
# create a client using the InterFAX WSDL | |
client = Savon::Client.new("http://ws.interfax.net/inbound.asmx?WSDL") | |
# get a single inbound fax with :get_list | |
response = client.request(:int, :get_list) do | |
soap.body = { | |
:username => "your username", | |
:password => "your password", | |
:l_type => "AllMessages", | |
:max_items => 1 | |
} | |
end | |
# translate the response to a Hash and get the message | |
message = response.to_hash[:get_list_response][:obj_message_item][:message_item] | |
message_id = message[:message_id].to_i # => 21438000 | |
message_size = message[:message_size].to_i # => 1982 | |
image = "" | |
chunk_size = 250 | |
# split the message_size into chunks of 250k or smaller | |
chunks = [chunk_size] * (message_size / chunk_size) << message_size % chunk_size | |
# loop to retrieve each chunk of the image | |
chunks.inject(0) do |from, chunk| | |
response = client.request(:int, :get_image_chunk) do | |
soap.body = { | |
:username => "your username", | |
:password => "your password", | |
:message_ID => message_id, | |
:mark_as_read => true, | |
:from => from, | |
:chunk_size => chunk | |
} | |
end | |
image += Base64.decode64(response[:get_image_chunk_response][:image]) | |
from + chunk | |
end | |
# save the image to disk | |
File.open("/Users/username/Desktop/fax-#{message_id}.pdf", "w") { |f| f.write(image) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment