Class: FantasticRobot::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/fantastic_robot/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



8
9
10
11
12
13
14
15
16
# File 'lib/fantastic_robot/connection.rb', line 8

def initialize
  # Create the connection object to make calls to the API
  @conn = Faraday.new(:url => "https://api.telegram.org/bot#{FantasticRobot.configuration.api_key}/") do |faraday|
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  :net_http  # make requests with Net::HTTP
  end
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



6
7
8
# File 'lib/fantastic_robot/connection.rb', line 6

def conn
  @conn
end

Instance Method Details

#api_call(method, payload) ⇒ Object

Function to call API passing a payload

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fantastic_robot/connection.rb', line 19

def api_call method, payload
  raise ArgumentError, "API method not specified." if method.blank?

  payload ||= {}

  res = @conn.post method.to_s, payload

  raise Faraday::Error, "Wrong response: #{res.inspect}" if (res.status != 200)

  return res
end

#get_by_path(path) ⇒ Object

Raises:

  • (Faraday::Error)


31
32
33
34
# File 'lib/fantastic_robot/connection.rb', line 31

def get_by_path path
  return open("https://api.telegram.org/bot#{FantasticRobot.configuration.api_key}/#{path}")
  raise Faraday::Error, "Wrong response: #{res.inspect}" if (res.status != 200)
end