Class: Rubogram::Client
- Inherits:
-
Object
- Object
- Rubogram::Client
- Defined in:
- lib/rubogram/client.rb
Instance Method Summary collapse
-
#initialize(token, adapter: Faraday.default_adapter, logging: true, raise_errors: true) ⇒ Client
constructor
A new instance of Client.
-
#method_missing(method, *args, &block) ⇒ Object
Using method_missing for catching all the methods.
Constructor Details
#initialize(token, adapter: Faraday.default_adapter, logging: true, raise_errors: true) ⇒ Client
Returns a new instance of Client.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rubogram/client.rb', line 6 def initialize token, adapter: Faraday.default_adapter, logging: true, raise_errors: true @faraday = Faraday.new "https://api.telegram.org/bot#{token}/" do |faraday| faraday.request :multipart faraday.request :url_encoded # Logging faraday.response :logger if logging # Enabling error raising faraday.use Faraday::Response::RaiseError if raise_errors # Enabling json parser faraday.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/ faraday.adapter adapter end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Using method_missing for catching all the methods
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rubogram/client.rb', line 25 def method_missing method, *args, &block if args.size > 1 raise ArgumentError.new "wrong number of arguments (#{args.size} for 0..1)" end args.push({}) if args.size == 0 unless args[0].is_a? Hash raise ArgumentError.new "argument must be a Hash" end method = method.to_s.split('_').inject([]){ |b,e| b.push(b.empty? ? e : e.capitalize) }.join @faraday.post method, args[0] end |