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:



14
15
16
# File 'lib/line/bot/request.rb', line 14

def initialize
  yield(self) if block_given?
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



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

def credentials
  @credentials
end

#endpointObject

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#endpoint_pathObject

Returns the value of attribute endpoint_path.



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

def endpoint_path
  @endpoint_path
end

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

#to_channel_idObject

Returns the value of attribute to_channel_id.



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

def to_channel_id
  @to_channel_id
end

#to_midObject

Returns the value of attribute to_mid.



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

def to_mid
  @to_mid
end

Instance Method Details

#contentLine::Bot::Message::Base#content



40
41
42
# File 'lib/line/bot/request.rb', line 40

def content
  message.content
end

#getNet::HTTPResponse

Get content of specified URL.

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)


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

def get
  validate_for_getting_message
  https.get(endpoint_path, header)
end

#headerHash

Returns:

  • (Hash)


57
58
59
60
61
62
63
64
65
# File 'lib/line/bot/request.rb', line 57

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

  header.merge(hash)
end

#httpsNet::HTTP

Returns:

  • (Net::HTTP)


19
20
21
22
23
24
25
26
27
# File 'lib/line/bot/request.rb', line 19

def https
  uri = URI(endpoint)
  https = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == "https"
    https.use_ssl = true
  end

  https
end

#payloadHash

Returns:

  • (Hash)


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

def payload
  payload = {
    to: to,
    toChannel: to_channel_id,
    eventType: message.event_type.to_s,
    content: content
  }

  payload.to_json
end

#postNet::HTTPResponse

Post content of specified URL.

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)


82
83
84
85
# File 'lib/line/bot/request.rb', line 82

def post
  validate_for_posting_message
  https.post(endpoint_path, payload, header)
end

#toArray

Returns:

  • (Array)

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
# File 'lib/line/bot/request.rb', line 30

def to
  raise ArgumentError, 'Wrong argument type `to_mid`' unless to_mid.instance_of?(String) || to_mid.instance_of?(Array)
  to = to_mid.instance_of?(String) ? [to_mid] : to_mid

  raise ArgumentError, 'Wrong argument type `to_mid`' unless to.size > 0 && to.reject {|item| item.instance_of?(String) }

  to
end

#validate_for_getting_messageObject

Raises:

  • (ArgumentError)


87
88
89
# File 'lib/line/bot/request.rb', line 87

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

#validate_for_posting_messageObject

Raises:

  • (ArgumentError)


91
92
93
94
# File 'lib/line/bot/request.rb', line 91

def validate_for_posting_message
  raise ArgumentError, 'Invalid argument `message`' unless message.valid?
  raise ArgumentError, 'Wrong argument type `endpoint_path`' unless endpoint_path.instance_of?(String)
end