Class: Mushikago::Http::Request
- Inherits:
-
Object
- Object
- Mushikago::Http::Request
- Defined in:
- lib/mushikago/http/request.rb
Overview
MushikagoサービスのHTTPリクエストを表すクラス
Direct Known Subclasses
DeleteRequest, GetRequest, MultipartRequest, PostRequest, PutRequest
Instance Attribute Summary collapse
-
#headers ⇒ Hash
readonly
Headers.
-
#host ⇒ String
Host.
-
#params ⇒ String
readonly
Params.
-
#path ⇒ String
Path.
-
#port ⇒ Integer
Port.
Class Method Summary collapse
-
.add_param(name, options = {}) {|value| ... } ⇒ Object
(also: request_parameter)
リクエストパラメータへのアクセサを定義する.
Instance Method Summary collapse
-
#[](key) ⇒ String
パラメータの値.
- #[]=(key, value) ⇒ Object
-
#initialize(options = {}) ⇒ Request
constructor
A new instance of Request.
-
#to_http_request ⇒ Net::HTTPRequest
HTTPリクエストオブジェクトに変換する.
-
#url_encoded_params ⇒ String
URLエンコードされ、&で接続されたパラメータの文字列.
Constructor Details
#initialize(options = {}) ⇒ Request
Returns a new instance of Request.
19 20 21 22 23 24 25 26 27 |
# File 'lib/mushikago/http/request.rb', line 19 def initialize ={} @headers = {} endpoint = [:endpoint] || Mushikago.config.endpoint host, port = endpoint.split(':') @host = host @port = port @path = '/' @params = {} end |
Instance Attribute Details
#headers ⇒ Hash (readonly)
Returns headers.
9 10 11 |
# File 'lib/mushikago/http/request.rb', line 9 def headers @headers end |
#host ⇒ String
Returns host.
11 12 13 |
# File 'lib/mushikago/http/request.rb', line 11 def host @host end |
#params ⇒ String (readonly)
Returns params.
17 18 19 |
# File 'lib/mushikago/http/request.rb', line 17 def params @params end |
#path ⇒ String
Returns path.
15 16 17 |
# File 'lib/mushikago/http/request.rb', line 15 def path @path end |
#port ⇒ Integer
Returns port.
13 14 15 |
# File 'lib/mushikago/http/request.rb', line 13 def port @port end |
Class Method Details
.add_param(name, options = {}) {|value| ... } ⇒ Object Also known as: request_parameter
リクエストパラメータへのアクセサを定義する
102 103 104 105 106 107 108 109 110 |
# File 'lib/mushikago/http/request.rb', line 102 def add_param name, ={}, &transform attr_accessor name define_method("#{name}=") do |value| value = value || [:default] value = transform ? transform.call(value) : value self[name.to_s] = value instance_variable_set("@#{name}".to_sym, value) end end |
Instance Method Details
#[](key) ⇒ String
Returns パラメータの値.
37 38 39 40 |
# File 'lib/mushikago/http/request.rb', line 37 def [] key param = params.detect{|p| p[0] == key} param ? param[1] : nil end |
#[]=(key, value) ⇒ Object
31 32 33 |
# File 'lib/mushikago/http/request.rb', line 31 def []= key, value params[key] = value end |
#to_http_request ⇒ Net::HTTPRequest
HTTPリクエストオブジェクトに変換する
50 51 52 53 54 55 56 57 |
# File 'lib/mushikago/http/request.rb', line 50 def to_http_request http_request = new_http_request headers.each do |key, value| http_request[key] = value end http_request.body = url_encoded_params if http_request.request_body_permitted? return http_request end |
#url_encoded_params ⇒ String
Returns URLエンコードされ、&で接続されたパラメータの文字列.
44 45 46 |
# File 'lib/mushikago/http/request.rb', line 44 def url_encoded_params params.sort.select{|p| p[1].kind_of?(String)}.collect{|pp| pp.map{|p| encoding p}.join('=')}.join('&') end |