Class: Rubyviber::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyviber/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(token, adapter: Faraday.default_adapter, logging: true, raise_errors: true, log_bodies: false) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubyviber/client.rb', line 7

def initialize token, adapter: Faraday.default_adapter, logging: true, raise_errors: true, log_bodies: false
  @faraday = Faraday.new url: "https://chatapi.viber.com/pa/", headers: { "X-Viber-Auth-Token": token } do |faraday|
    faraday.request :multipart

    # Logging
    faraday.response(:logger, ::Logger.new(STDOUT), bodies: log_bodies) if logging

    # Json encoder
    faraday.use FaradayMiddleware::EncodeJson

    # 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



28
29
30
31
32
33
34
35
36
# File 'lib/rubyviber/client.rb', line 28

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

  call method.to_s, args[0]
end

Instance Method Details

#call(method, args = {}) ⇒ Object

Call method from viber rest api



39
40
41
42
43
44
45
# File 'lib/rubyviber/client.rb', line 39

def call method, args = {}
  unless args.is_a? Hash
    raise ArgumentError.new "argument must be a Hash"
  end

  @faraday.post method.to_s, args
end