Sha256: a4374f49c45ed9969c2227ec349c023431ed207dec72d01e7b738a069beb5ae3

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

# Public: collection of keikoku notifications
#
# This class encapsulates Keikoku::Notification objects
# as a collection.
#
# It includes the Enumerable module, so `map`, `detect`,
# and friends can be used.
#
# Examples
#
#   notifications = Keikokuc::NotificationList.new(user:    'user@example.com',
#                                                  api_key: 'abcd')
#   if notifications.fetch
#     notifications.each do |notification|
#       puts notification.inspect
#     end
#   else
#     # handle error
#   end
class Keikokuc::NotificationList
  include Enumerable

  attr_accessor :user, :password

  # Public: Initializes a NotificationList
  #
  # opts - options hash containing attribute values for the object
  #        being constructed accepting the following three keys:
  #  user - the heroku account's email (required)
  #  password - the heroku account's password (required)
  #  client - the client, used for DI in tests
  def initialize(opts)
    @user          = opts.fetch(:user)
    @password      = opts.fetch(:password)
    @client        = opts[:client]
    @notifications = []
  end

  # Public: fetches notifications for the provided user
  #
  # Sets notifications to a set of `Notification` objects
  # accessible via methods in Enumerable
  #
  # Returns a boolean set to true if fetching succeeded
  def fetch
    result, error = client.get_notifications
    if error.nil?
      @notifications = result.map do |attributes|
        Keikokuc::Notification.new(attributes)
      end
    end

    error.nil?
  end

  # Public: the number of notifications
  #
  # Returns an Integer set to the number of notifications
  def size
    @notifications.size
  end

  # Public: yields each Notification
  #
  # Yields every notification in this collection
  def each
    @notifications.each
  end

private
  def client # :nodoc:
    @client ||= Client.new(user: user, password: password)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
keikokuc-0.1 lib/keikokuc/notification_list.rb