Last active
December 18, 2015 22:18
-
-
Save jadon1979/5853055 to your computer and use it in GitHub Desktop.
Http Requests
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
module SomeLib | |
module Utilities | |
module HttpRequest | |
#send get request to sensor | |
def send_get_request(url = nil) | |
send_data url | |
end | |
#send post request to sensor | |
def send_post_request(url = nil, body = nil) | |
send_data url, :post, body | |
end | |
private | |
# send get/post data to device | |
def send_data(url, method_to_send = :get, body = nil) | |
uri = URI.parse(url) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
http.ssl_version = 'SSLv3' #TLSv1 depending on the update version of the device | |
request = Net::HTTP::Get.new(uri.request_uri) | |
request.basic_auth("user", "pass") | |
request.body = body if method_to_send == :post | |
set_headers request | |
http.request(request) | |
end | |
# Build Headers hash | |
def set_headers(request) | |
headers = { | |
'User-Agent' => 'Some Agent', | |
'Accept' => 'text/xml', | |
'Content-type' => 'text/xml', | |
'Connection' => 'keepalive', | |
'Accept-Charset' => 'utf-8', | |
'Pragma' => 'no-cache', | |
'Cache-Control' => 'no-cache' | |
} | |
headers.each { |k, v| request.add_field("#{k}", "#{v}") } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment