Class: FantasticRobot::Connection
- Inherits:
-
Object
- Object
- FantasticRobot::Connection
- Defined in:
- lib/fantastic_robot/connection.rb
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
Instance Method Summary collapse
-
#api_call(method, payload) ⇒ Object
Function to call API passing a payload.
- #get_by_path(path) ⇒ Object
-
#initialize ⇒ Connection
constructor
A new instance of Connection.
Constructor Details
#initialize ⇒ Connection
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
#conn ⇒ Object (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
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
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 |