Created
August 26, 2014 22:16
-
-
Save gpad/5287e187731bedfa4244 to your computer and use it in GitHub Desktop.
Hey Web SMS example
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
require 'net/http' | |
require 'net/https' | |
require 'uri' | |
require 'base64' | |
class HeyWeb | |
BASE_URL = "https://secure.apisms.it/http/" | |
ID_API = 5 | |
REPORT_TYPE = 'P' | |
def initialize username, password, id_api=nil, report_type=nil, base_url=nil | |
@username = username | |
@password = password | |
@base_url = base_url || BASE_URL | |
@id_api = id_api || ID_API | |
@report_type = report_type || REPORT_TYPE | |
end | |
def get_credit | |
execute 'get_credit' | |
end | |
def send_sms from, to, text | |
execute 'send_sms', { | |
'sender' => Base64.encode64(from), | |
'body' => Base64.encode64(text.encode('ISO-8859-1', undef: :replace)), | |
'destination' => to, #numero senza il + | |
'id_api' => @id_api, | |
'report_type' => @report_type, | |
} | |
end | |
def execute path, args = {} | |
uri = URI.join(@base_url, path) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = uri.is_a? URI::HTTPS | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
request = Net::HTTP::Post.new uri.path | |
params = { | |
'authlogin' => @username, | |
'authpasswd' => @password | |
} | |
request.set_form_data params.merge(args) | |
response = http.request(request) | |
puts response.body | |
end | |
end |
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
hw = HeyWeb.new 'username', 'passwod' | |
hw.get_credit # return the credit | |
hw.send_sms 'FROM', '393471234567', 'testo di prova' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment