Class: Line::Bot::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/line/bot/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Line::Bot::Request

Initializes a new Request

Yields:

  • (_self)

Yield Parameters:



28
29
30
# File 'lib/line/bot/request.rb', line 28

def initialize
  yield(self) if block_given?
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



23
24
25
# File 'lib/line/bot/request.rb', line 23

def credentials
  @credentials
end

#endpointObject

Returns the value of attribute endpoint.



23
24
25
# File 'lib/line/bot/request.rb', line 23

def endpoint
  @endpoint
end

#endpoint_pathObject

Returns the value of attribute endpoint_path.



23
24
25
# File 'lib/line/bot/request.rb', line 23

def endpoint_path
  @endpoint_path
end

#httpclientObject

Returns the value of attribute httpclient.



23
24
25
# File 'lib/line/bot/request.rb', line 23

def httpclient
  @httpclient
end

#messagesObject

Returns the value of attribute messages.



23
24
25
# File 'lib/line/bot/request.rb', line 23

def messages
  @messages
end

#reply_tokenObject

Returns the value of attribute reply_token.



23
24
25
# File 'lib/line/bot/request.rb', line 23

def reply_token
  @reply_token
end

#toObject

Returns the value of attribute to.



23
24
25
# File 'lib/line/bot/request.rb', line 23

def to
  @to
end

Instance Method Details

#assert_for_getting_messageObject

Raises:

  • (ArgumentError)


72
73
74
# File 'lib/line/bot/request.rb', line 72

def assert_for_getting_message
  raise ArgumentError, 'Wrong argument type `endpoint_path`' unless endpoint_path.is_a?(String)
end

#assert_for_posting_messageObject

Raises:

  • (ArgumentError)


76
77
78
# File 'lib/line/bot/request.rb', line 76

def assert_for_posting_message
  raise ArgumentError, 'Wrong argument type `endpoint_path`' unless endpoint_path.is_a?(String)
end

#getNet::HTTPResponse

Get content of specified URL.

Returns:

  • (Net::HTTPResponse)


57
58
59
60
# File 'lib/line/bot/request.rb', line 57

def get
  assert_for_getting_message
  httpclient.get(endpoint + endpoint_path, header)
end

#headerHash

Returns:

  • (Hash)


44
45
46
47
48
49
50
51
52
# File 'lib/line/bot/request.rb', line 44

def header
  header = {
    'Content-Type' => 'application/json; charset=UTF-8',
    'User-Agent' => "LINE-BotSDK-Ruby/#{Line::Bot::API::VERSION}",
  }
  hash = credentials.inject({}) { |h, (k, v)| h[k] = v.to_s; h }

  header.merge(hash)
end

#payloadHash

Returns:

  • (Hash)


33
34
35
36
37
38
39
40
41
# File 'lib/line/bot/request.rb', line 33

def payload
  payload = {
    to: to,
    replyToken: reply_token,
    messages: messages
  }

  payload.delete_if{|k, v| v.nil?}.to_json
end

#postNet::HTTPResponse

Post content of specified URL.

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)


67
68
69
70
# File 'lib/line/bot/request.rb', line 67

def post
  assert_for_posting_message
  httpclient.post(endpoint + endpoint_path, payload, header)
end