Class: Mushikago::Http::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/mushikago/http/request.rb

Overview

MushikagoサービスのHTTPリクエストを表すクラス

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 options={}
  @headers = {}
  endpoint = options[:endpoint] || Mushikago.config.endpoint
  host, port = endpoint.split(':')
  @host = host
  @port = port
  @path = '/'
  @params = {}
end

Instance Attribute Details

#headersHash (readonly)

Returns headers.

Returns:

  • (Hash)

    headers



9
10
11
# File 'lib/mushikago/http/request.rb', line 9

def headers
  @headers
end

#hostString

Returns host.

Returns:

  • (String)

    host



11
12
13
# File 'lib/mushikago/http/request.rb', line 11

def host
  @host
end

#paramsString (readonly)

Returns params.

Returns:

  • (String)

    params



17
18
19
# File 'lib/mushikago/http/request.rb', line 17

def params
  @params
end

#pathString

Returns path.

Returns:

  • (String)

    path



15
16
17
# File 'lib/mushikago/http/request.rb', line 15

def path
  @path
end

#portInteger

Returns port.

Returns:

  • (Integer)

    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

リクエストパラメータへのアクセサを定義する

Examples:

add_param :price do |v| v.to_i end

Parameters:

  • name (String)

    パラメータ名

Yields:

  • (value)

    アクセサを通して渡された値を変換するブロック

Yield Returns:

  • (Object)

    変換された値



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

def add_param name, options={}, &transform
  attr_accessor name
  define_method("#{name}=") do |value|
    value = value || options[: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 パラメータの値.

Parameters:

  • key (String)

    パラメータのキー

Returns:

  • (String)

    パラメータの値



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

Parameters:

  • key (String)

    パラメータのキー

  • value (String)

    パラメータの値



31
32
33
# File 'lib/mushikago/http/request.rb', line 31

def []= key, value
  params[key] = value
end

#to_http_requestNet::HTTPRequest

HTTPリクエストオブジェクトに変換する

Returns:

  • (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_paramsString

Returns URLエンコードされ、&で接続されたパラメータの文字列.

Returns:

  • (String)

    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