Skip to content

Instantly share code, notes, and snippets.

@davidjrice
Created November 11, 2009 15:10
Show Gist options
  • Save davidjrice/232015 to your computer and use it in GitHub Desktop.
Save davidjrice/232015 to your computer and use it in GitHub Desktop.
require 'net/http'
HOST = "http://www.mymobileapi.com"
PORT = 80
def log!(message)
STDERR.puts "#{Time.now.to_s} #{message}"
end
def call_remote(method, params)
http = Net::HTTP.new(HOST, PORT)
uri = "#{HOST}#{method.to_s}"
if params
uri += "?Type=#{params[:type]}&username=#{params[:username]}&password=#{params[:password]}"
uri += "&num%20to=#{params[:to]}&data1=#{params[:message]}"
end
headers = { 'Content-Type' => 'application/xml', 'Accept' => 'application/xml' }
log! params.inspect
log! uri
response = http.start do |http|
http.get(uri, headers)
end
if response.kind_of? Net::HTTPSuccess
return response.body
else
raise Exception.new("#{response.code}: #{response.message}")
end
rescue Exception => e
log! "Error contacting #{HOST}: #{e}"
log! e.backtrace.join("\n")
raise e
end
#######
# run me like this
# ruby mymobileapi-test.rb 0044555555555 "This is my message blah."
#######
params = {:type => "something", :username => "steve", :password => "blah"}
exit(0) unless ARGV && ARGV[0] && ARGV[1]
log! "to " + ARGV[0]
log! "msg " + ARGV[1]
params.merge(:to => ARGV[0], :message => ARGV[1])
response = call_remote("/api5/http5.aspx?", params)
log! response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment