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

#fileObject

Returns the value of attribute file.



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

def file
  @file
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

#payloadString

Returns:

  • (String)


33
34
35
# File 'lib/line/bot/request.rb', line 33

def payload
  @payload
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_deleting_messageObject

Raises:

  • (ArgumentError)


99
100
101
# File 'lib/line/bot/request.rb', line 99

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

#assert_for_getting_messageObject

Raises:

  • (ArgumentError)


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

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)


95
96
97
# File 'lib/line/bot/request.rb', line 95

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

#deleteObject



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

def delete
  assert_for_deleting_message
  httpclient.delete(endpoint + endpoint_path, header)
end

#getNet::HTTPResponse

Get content of specified URL.

Returns:

  • (Net::HTTPResponse)


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

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

#headerHash

Returns:

  • (Hash)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/line/bot/request.rb', line 46

def header
  content_type =
    if file.is_a? File
      case file.path
      when /\.png\z/i then 'image/png'
      when /\.jpe?g\z/i then 'image/jpeg'
      else
        raise ArgumentError.new("invalid file extension: #{file.path}")
      end
    else
      'application/json; charset=UTF-8'
    end

  header = {
    'Content-Type' => content_type,
    '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

#postNet::HTTPResponse

Post content of specified URL.

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)


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

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