Module: HtReq

Defined in:
lib/ht_req.rb,
lib/ht_req/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.errorObject



29
30
31
32
33
# File 'lib/ht_req.rb', line 29

def self.error()
  result = @error
  @error=[]
  result
end

.get_json_data(param) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/ht_req.rb', line 36

def self.get_json_data(param)
  res = self.request(param)
  if res.code != '200'
    return false
  else
    JSON.parse(res.body) rescue false
  end
end

.helpObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ht_req.rb', line 14

def self.help()
  puts "\u3053\u3093\u306A\u3075\u3046\u306B\u4F7F\u3044\u307E\u3059\nHtReq.send_request({\n:method => 'GET',\n:url => 'https://kyozai.net/'\n:params =>{\n  'name'=>'tagomori',\n  'pass'=>'tagomori123'\n},\n:header=>{'Content-Type'=>'application/x-www-form-urlencoded'}\n})\n"
end

.request(param = {}) ⇒ Object

メインの関数



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ht_req.rb', line 54

def self.request(param={})
  #methodがなければGETにする
  request_method = param[:method].nil? ? 'GET': param[:method]
  #urlがなければとりあえず自分のサイトに送っておく
  if param[:url].nil? then param[:url]='https://kyozai.net/gemtest/ht_req/1' end
  #urlをパースする
  url = URI.parse(param[:url])
  #pathがなければrootにする
  if url.path=='' then url.path='/' end
  #Getの場合は必要であればパラメータを設定する
  url = self.set_get_params_to_url(url,param[:params],request_method)
  #httpのインスタンスを作成
  http = Net::HTTP.new(url.host,url.port)
  #ssl通信を許可
  http.use_ssl=true
  http.verify_mode=OpenSSL::SSL::VERIFY_NONE
  #接続した結果のレスポンスをreturn
  res = http.start do 
    #Postの場合とGetの場合を両方インスタンス化
    req = eval('Net::HTTP::'+request_method.capitalize+'.new(url)')
    #リクエストヘッダを設定する
    req=self.set_request_header(req,param)
    #Postの場合、パラメータがあれば送る
    req = self.set_post_param(req,param[:params],request_method)
    http.request(req)
  end
  if res.code != '200'
    @error.push({:code=>res.code,
                  :body=>res.body,
                  :datetime=>Time.now.to_s,
                  :header=>res.header.msg
                })
  end
  res
end

.set_get_params_to_url(url, param, request_method = 'GET') ⇒ Object

getのパラメータを設定するurlはパースしたものでも文字列で与えてもいいが、返り値はパースしたものになる



102
103
104
105
106
107
108
109
110
# File 'lib/ht_req.rb', line 102

def self.set_get_params_to_url(url,param,request_method='GET')
  #文字列がセットされた場合はパースする
  if url.class == String then url = URI.parse(url) end
  #パラメータをセットする
  if request_method.capitalize=='Get' && !param.nil?
    url.query = URI.encode_www_form(param).gsub("+","%20")
  end
  url
end

.set_post_param(req, param, request_method = "POST") ⇒ Object

Postの場合はフォームデータを入れる



113
114
115
116
117
118
# File 'lib/ht_req.rb', line 113

def self.set_post_param(req,param,request_method="POST")
    if request_method.capitalize=='Post' && !param.nil?
      req.set_form_data(param)
    end
    req
end

.set_request_header(req, param) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/ht_req.rb', line 90

def self.set_request_header(req,param)
  if !param[:header].nil?
    param[:header].each{|k,v|
      req[k]=v
    }
  end
  req
end

.test_request(param = {}) ⇒ Object

テスト送信してみて問題ないか確かめる



48
49
50
# File 'lib/ht_req.rb', line 48

def self.test_request(param={})
  self.request(param).code == '200' ? true:false
end