Sha256: 463dc087a37dcdf8fd6cdbf6fc86993074010c366452cf10d6e313fd5057d197

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

require "dexby/connection"
require "dexby/parse"

class Dexby::Reader
  def initialize(username, password, connection_class=Dexby::Connection, parser_class=Dexby::Parse)
    @connection_class = connection_class
    @parser_class = parser_class
    @username = username
    @password = password
    @session_id = nil
  end

  def connection
    @connection_class
  end

  def parser
    @parser_class
  end

  def read(minutes=1440, count=1)
    ensure_session_id
    result = session_connection_read(minutes, count)
    if result[1] != 200
      raise ::StandardError, result[1]
    end
    parser.parse_all(result[0])
  end

  def session_connection_read(minutes, count)
    result = read_connection(minutes, count)
    if result[1] == 401 || result[1] == 500 # expired session_id
      result = get_session_reread(minutes, count)
    end
    return result
  end

  def get_session_reread(minutes, count)
    get_session_id
    read_connection(minutes, count)
  end

  def read_connection(minutes, count)
    connection.read(@session_id, minutes, count)
  end

  def ensure_session_id
    get_session_id if @session_id.nil?
  end

  def get_session_id
    result = connection.login(@username, @password)
    if result[1] == 200
      @session_id = result[0]
    else
      raise ::StandardError
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dexby-0.1.1 lib/dexby/reader.rb