Sha256: 71f80271a67a7ad6a255815a02df10f17091184a6db7fd244897aa3f2f35e864

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

require 'snooby'
require 'usaidwat/service'
require 'usaidwat/ext/snooby'
require 'usaidwat/ext/time'

module USaidWat
  module Client
    class ReachabilityError < RuntimeError; end

    class NoSuchUserError < StandardError; end

    class BaseRedditor
      attr_reader :username

      def initialize(username)
        @username = username
      end

      def comments
        user.comments(100)
      rescue NoMethodError
        raise NoSuchUserError, username
      rescue TypeError, Net::HTTP::Persistent::Error
        raise ReachabilityError, "Reddit unreachable"
      end

      def link_karma
        about('link_karma')
      end

      def comment_karma
        about('comment_karma')
      end

      def created_at
        Time.at(about('created_utc'))
      end

      def age
        (Time.now - created_at).ago
      end

      def to_s
        "#{username}"
      end

      def posts
        user.posts
      rescue NoMethodError
        raise NoSuchUserError, username
      rescue TypeError, Net::HTTP::Persistent::Error
        raise ReachabilityError, "Reddit unreachable"
      end

      private

      def user
        @service.user(username)
      end

      def about(key)
        user.about[key]
      rescue NoMethodError
        raise NoSuchUserError, username
      rescue TypeError, Net::HTTP::Persistent::Error
        raise ReachabilityError, "Reddit unreachable"
      end
    end

    class Redditor < BaseRedditor
      def initialize(username)
        @service = Snooby::Client.new("usaidwat v#{USaidWat::VERSION}")
        super
      end
    end

    class TestRedditor < BaseRedditor
      def initialize(username)
        @service = USaidWat::Service::MockService.new
        super
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
usaidwat-1.4.5 lib/usaidwat/client.rb
usaidwat-1.4.4 lib/usaidwat/client.rb