This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Phaxio Ruby Helper

By: a guest on Jan 4th, 2012  |  syntax: Rails  |  size: 3.64 KB  |  views: 286  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #################################################
  2. ## PUT THIS IN multipart.rb
  3. #################################################
  4.  
  5. # Takes a hash of string and file parameters and returns a string of text
  6. # formatted to be sent as a multipart form post.
  7. #
  8. # Author:: Cody Brimhall <mailto:cbrimhall@ucdavis.edu>
  9. # Created:: 22 Feb 2008
  10.  
  11.  
  12. module Multipart
  13.   VERSION = "1.0.0" unless const_defined?(:VERSION)
  14.  
  15.   # Formats a given hash as a multipart form post
  16.   # If a hash value responds to :string or :read messages, then it is
  17.   # interpreted as a file and processed accordingly; otherwise, it is assumed
  18.   # to be a string
  19.   class Post
  20.     # We have to pretend like we're a web browser...
  21.     USERAGENT = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6" unless const_defined?(:USERAGENT)
  22.     BOUNDARY = "0123456789ABLEWASIEREISAWELBA9876543210" unless const_defined?(:BOUNDARY)
  23.     CONTENT_TYPE = "multipart/form-data; boundary=#{ BOUNDARY }" unless const_defined?(:CONTENT_TYPE)
  24.     HEADER = { "Content-Type" => CONTENT_TYPE, "User-Agent" => USERAGENT } unless const_defined?(:HEADER)
  25.  
  26.     def self.prepare_query(params)
  27.       fp = []
  28.  
  29.       params.each do |k, v|
  30.         # Are we trying to make a file parameter?
  31.         if v.respond_to?(:path) and v.respond_to?(:read) then
  32.           fp.push(FileParam.new(k, v.path, v.read))
  33.         # We must be trying to make a regular parameter
  34.         else
  35.           fp.push(StringParam.new(k, v))
  36.         end
  37.       end
  38.  
  39.       # Assemble the request body using the special multipart format
  40.       query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("")  + "--" + BOUNDARY + "--"
  41.       return query, HEADER
  42.     end
  43.   end
  44.  
  45.   private
  46.  
  47.   # Formats a basic string key/value pair for inclusion with a multipart post
  48.   class StringParam
  49.     attr_accessor :k, :v
  50.  
  51.     def initialize(k, v)
  52.       @k = k
  53.       @v = v
  54.     end
  55.  
  56.     def to_multipart
  57.       return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
  58.     end
  59.   end
  60.  
  61.   # Formats the contents of a file or string for inclusion with a multipart
  62.   # form post
  63.   class FileParam
  64.     attr_accessor :k, :filename, :content
  65.  
  66.     def initialize(k, filename, content)
  67.       @k = k
  68.       @filename = filename
  69.       @content = content
  70.     end
  71.  
  72.     def to_multipart
  73.       # If we can tell the possible mime-type from the filename, use the
  74.       # first in the list; otherwise, use "application/octet-stream"
  75.       mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]
  76.       return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{ filename }\"\r\n" +
  77.              "Content-Type: #{ mime_type.simplified }\r\n\r\n#{ content }\r\n"
  78.     end
  79.   end
  80. end
  81.  
  82.  
  83.  
  84. #################################################
  85. ## PUT THIS IN phaxio.rb
  86. #################################################
  87.  
  88. require "rubygems"
  89. require 'uri'
  90. require 'cgi'
  91. require 'net/http'
  92. require 'net/https'
  93. require 'multipart.rb'
  94. require 'yaml'
  95. require 'mime/types'
  96.  
  97. PHAXIO_HOST = "api.phaxio.com"
  98. PHAXIO_API_URI = "/v1"
  99. PHAXIO_API_KEY = "KEY_HERE"
  100. PHAXIO_API_SECRET = "SECRET_HERE"
  101. LSClientVersion = "phaxio-client-v0.1"
  102.  
  103.  
  104. def send_demo_fax(number)
  105.     faxfile = File.new('FILENAME_HERE')
  106.     data, headers = Multipart::Post.prepare_query({"to" => number, "api_key" => PHAXIO_API_KEY, "api_secret" => PHAXIO_API_SECRET, "filename" => faxfile})
  107.     http = Net::HTTP.new(PHAXIO_HOST)
  108.     res = http.start {|con| con.post(PHAXIO_API_URI+"/send", data, headers) }
  109. end
  110.  
  111. puts send_demo_fax("PHONE_NUMBER_HERE")
clone this paste RAW Paste Data