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| ... } ⇒ Request

Returns a new instance of Request.

Yields:

  • (_self)

Yield Parameters:



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

def initialize
  yield(self) if block_given?
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



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

def credentials
  @credentials
end

#endpoint_pathObject

Returns the value of attribute endpoint_path.



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

def endpoint_path
  @endpoint_path
end

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

#to_midObject

Returns the value of attribute to_mid.



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

def to_mid
  @to_mid
end

Instance Method Details

#contentObject



25
26
27
# File 'lib/line/bot/request.rb', line 25

def content
  message.content
end

#getObject

Raises:

  • (ArgumentError)


50
51
52
53
54
# File 'lib/line/bot/request.rb', line 50

def get
  raise ArgumentError, "Invalid arguments" unless validate_for_getting_message?

  https.get(endpoint_path, header)
end

#headerObject



40
41
42
43
44
45
46
47
48
# File 'lib/line/bot/request.rb', line 40

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

#httpsObject



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

def https
  https = Net::HTTP.new('trialbot-api.line.me', 443)
  https.use_ssl = true

  https
end

#payloadObject



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

def payload
  payload = {
    to: to,
    toChannel: 1383378250, # Fixed  value
    eventType: message.event_type.to_s,
    content: content
  }

  payload.to_json
end

#postObject

Raises:

  • (ArgumentError)


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

def post
  raise ArgumentError, "Invalid arguments" unless validate_for_posting_message?

  https.post(endpoint_path, payload, header)
end

#toObject



21
22
23
# File 'lib/line/bot/request.rb', line 21

def to
  to_mid.instance_of?(String) ? [to_mid] : to_mid
end

#validate_for_getting_message?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/line/bot/request.rb', line 62

def validate_for_getting_message?
  !endpoint_path.nil?
end

#validate_for_posting_message?Boolean

Returns:

  • (Boolean)


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

def validate_for_posting_message?
  to.size > 0 && message.valid? && endpoint_path
end