class Fabychy::Bot

Constants

API_ENDPOINT

Attributes

me[R]

Public Class Methods

new(token) click to toggle source
# File lib/fabychy/bot.rb, line 7
def initialize(token)
  @access_token = token
  @offset = 0
  @timeout = 60
  @fail_silently = false
  @connection = HTTPClient.new
end

Public Instance Methods

get_user(user_id) click to toggle source
# File lib/fabychy/bot.rb, line 23
def get_user(user_id)
  response = api_get("#{user_id}?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=#{@access_token}")
  Fabychy::DataTypes::User.new(response.result)
end
send_message(message) click to toggle source
# File lib/fabychy/bot.rb, line 19
def send_message(message)
  api_post("me/messages?access_token=#{@access_token}", Fabychy::Sanitizer.sanitize(:msg, message))
end
send_raw(message) click to toggle source
# File lib/fabychy/bot.rb, line 15
def send_raw(message)
  api_post("me/messages?access_token=#{@access_token}", message)
end

Private Instance Methods

api_get(action) click to toggle source
# File lib/fabychy/bot.rb, line 31
def api_get(action)
  api_uri = "#{action}"
  begin
    response = @connection.get(
      "#{API_ENDPOINT}/#{api_uri}",
      nil
    )

    ApiResponse.new(response,@fail_silently)
  rescue HTTPClient::ReceiveTimeoutError => e
    if !@fail_silently
      fail Fabychy::Errors::TimeoutError, e.to_s
    end
  end
end
api_post(action, params) click to toggle source
# File lib/fabychy/bot.rb, line 47
def api_post(action, params)
  api_uri = "#{action}"

  begin
    response = @connection.post(
      "#{API_ENDPOINT}/#{api_uri}",
      MultiJson.dump(params),
      'Content-Type' => 'application/json'
    )

    ApiResponse.new(response,@fail_silently)
  rescue HTTPClient::ReceiveTimeoutError => e
    if !@fail_silently
      fail Fabychy::Errors::TimeoutError, e.to_s
    end
  end
end