lib/whats/client.rb in whatsapp-0.1.5 vs lib/whats/client.rb in whatsapp-1.0.0
- old
+ new
@@ -2,32 +2,46 @@
require "whats/actions/login"
module Whats
class Client
- def initialize(login = Whats::Actions::Login.new)
+ def initialize(token = nil, token_type = :bearer)
@base_path = Whats.configuration.base_path
- @login = login
+ @token = token || login.token
+ @token_type = token_type
end
- def request(path, payload)
+ def request(path, payload = nil)
full_path = "#{base_path}#{path}"
response = Typhoeus.post(
full_path,
headers: {
- "Authorization" => "Bearer #{login.token}",
+ "Authorization" => "#{token_name} #{token}",
"Content-Type" => "application/json"
},
- body: payload.to_json
+ body: payload && payload.to_json
)
raise Whats::Errors::RequestError.new("API request error.", response) if response.failure?
JSON.parse(response.response_body)
end
private
- attr_reader :base_path, :login
+ attr_reader :base_path, :token, :token_type
+
+ def token_name
+ case token_type
+ when :basic
+ "Basic"
+ when :bearer
+ "Bearer"
+ end
+ end
+
+ def login
+ Whats::Actions::Login.new
+ end
end
end