Class: Sensingplaza::WebRequest
- Inherits:
-
Object
- Object
- Sensingplaza::WebRequest
- Defined in:
- lib/sensingplaza/webrequest.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#url ⇒ Object
Returns the value of attribute url.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
-
#user_password ⇒ Object
Returns the value of attribute user_password.
Instance Method Summary collapse
-
#get(getdata, endpoint, htmlret = false, headers = {}) ⇒ Object
getdata - Hash ex) { 1 => 10.2, 2 => 12, 3 => “hoge” …
-
#initialize(requesthost, logger = nil) ⇒ WebRequest
constructor
requesthost - String ex) “hoge.com” logger - Logger.
-
#post(postdata, endpoint, htmlret = false) ⇒ Object
postdata - Hash ex) { 1 => 10.2, 2 => 12, 3 => “hoge” …
Constructor Details
#initialize(requesthost, logger = nil) ⇒ WebRequest
requesthost - String ex) “hoge.com” logger - Logger
13 14 15 16 17 18 19 20 |
# File 'lib/sensingplaza/webrequest.rb', line 13 def initialize(requesthost, logger = nil) @url = requesthost @url = requesthost[0, requesthost.length - 1] if requesthost[-1] == "/" @logger = logger @user_id = nil @user_password = nil end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
9 10 11 |
# File 'lib/sensingplaza/webrequest.rb', line 9 def logger @logger end |
#url ⇒ Object
Returns the value of attribute url.
9 10 11 |
# File 'lib/sensingplaza/webrequest.rb', line 9 def url @url end |
#user_id ⇒ Object
Returns the value of attribute user_id.
9 10 11 |
# File 'lib/sensingplaza/webrequest.rb', line 9 def user_id @user_id end |
#user_password ⇒ Object
Returns the value of attribute user_password.
9 10 11 |
# File 'lib/sensingplaza/webrequest.rb', line 9 def user_password @user_password end |
Instance Method Details
#get(getdata, endpoint, htmlret = false, headers = {}) ⇒ Object
getdata - Hash ex) { 1 => 10.2, 2 => 12, 3 => “hoge” … } endpoint - String ex) “/setvalues” htmlret - Boolean -> false: return json string, true: html response headers - Hash -> header values
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sensingplaza/webrequest.rb', line 75 def get(getdata, endpoint, htmlret = false, headers = {}) endpoint = "/" + endpoint if endpoint[0] != "/" reqaddr = @url + endpoint dparams = URI.encode_www_form(getdata) uri = URI.parse(reqaddr + "?" + dparams) response = nil begin if reqaddr =~ /^https/ https = Net::HTTP.new(uri.host, 443) https.use_ssl = true https.verify_mode = OpenSSL::SSL::VERIFY_NONE https.start{|h| request = Net::HTTP::Get.new(uri.request_uri) request.basic_auth(@user_id, @user_password) unless @user_id.nil? headers.each do |k, v| request[k] = v end response = h.request(request) } else Net::HTTP.start(uri.host, uri.port){|http| request = Net::HTTP::Get.new(uri.request_uri) request.basic_auth(@user_id, @user_password) unless @user_id.nil? headers.each do |k, v| request[k] = v end response = http.request(request) } end jsondata = response.body.strip errflg = false errflg = true if htmlret == false && jsondata =~ /^<html>/ if errflg raise WebRequestError.new(jsondata.force_encoding("utf-8")) end rescue unless @logger.nil? @logger.error("WebRequestError: can not send data.") @logger.error("#{$!}") else puts "WebRequestError: can not send data." puts "#{$!}" end jsondata = nil end return jsondata, response.code.to_i, response if htmlret return jsondata end |
#post(postdata, endpoint, htmlret = false) ⇒ Object
postdata - Hash ex) { 1 => 10.2, 2 => 12, 3 => “hoge” … } endpoint - String ex) “/setvalues” htmlret - Boolean -> false: return json string, true: html response
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/sensingplaza/webrequest.rb', line 27 def post(postdata, endpoint, htmlret = false) endpoint = "/ " + endpoint if endpoint[0] != "/" reqaddr = @url + endpoint uri = URI.parse(reqaddr) response = nil begin if reqaddr =~ /^https/ https = Net::HTTP.new(uri.host, 443) https.use_ssl = true https.verify_mode = OpenSSL::SSL::VERIFY_NONE https.start{|h| request = Net::HTTP::Post.new(uri.path) request.basic_auth(@user_id, @user_password) unless @user_id.nil? request.body = postdata.to_json response = h.request(request) } else Net::HTTP.start(uri.host, uri.port){|http| request = Net::HTTP::Post.new(uri.path) request.basic_auth(@user_id, @user_password) unless @user_id.nil? request.body = postdata.to_json response = http.request(request) } end jsondata = response.body.strip errflg = false errflg = true if htmlret == false && jsondata =~ /^<html>/ if errflg raise WebRequestError.new(jsondata.force_encoding("utf-8")) end rescue unless @logger.nil? @logger.error("WebRequestError: can not send data.") @logger.error("#{$!}") else puts "WebRequestError: can not send data." puts "#{$!}" end jsondata = nil end return jsondata, response.code.to_i if htmlret return jsondata end |