Sha256: f474bb3f7639bf1b1b44d9b04a949dbc603b62a6fc3239a0b514affb808bb115

Contents?: true

Size: 1.65 KB

Versions: 5

Compression:

Stored size: 1.65 KB

Contents

module USaidWat
  module Service
    class MockComment
      attr_reader :subreddit, :body, :id, :link_id, :created_utc, :link_title, :ups, :downs

      def initialize(dict)
        data = dict['data']
        @subreddit = data['subreddit']
        @body = data['body']
        @id = data['id']
        @link_id = data['link_id']
        @created_utc = data['created_utc']
        @link_title = data['link_title']
        @ups = data['ups']
        @downs = data['downs']
      end
    end

    class MockSubmission
      attr_reader :subreddit, :title, :created_utc, :permalink, :url

      def initialize(dict)
        data = dict['data']
        @subreddit = data['subreddit']
        @title = data['title']
        @created_utc = data['created_utc']
        @permalink = data['permalink']
        @url = data['url']
      end
    end

    class MockUser
      def initialize(username)
        @username = username
      end

      def about
        load_data("user_#{@username}.json")['data']
      end

      def comments(n)
        json = load_data("#{@username}.json")
        json['data']['children'].map { |d| MockComment.new(d) }
      end

      def posts
        json = load_data("submissions_#{@username}.json")
        json['data']['children'].map { |d| MockSubmission.new(d) }
      end

      private

      def load_data(data_file)
        path = File.join(File.dirname(__FILE__), "..", "..", "features", "fixtures", data_file)
        raise USaidWat::Client::NoSuchUserError, @username unless File.exists?(path)
        JSON.parse(IO.read(path))
      end
    end

    class MockService
      def user(username)
        MockUser.new(username)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
usaidwat-1.4.5 lib/usaidwat/service.rb
usaidwat-1.4.4 lib/usaidwat/service.rb
usaidwat-1.4.3 lib/usaidwat/service.rb
usaidwat-1.4.2 lib/usaidwat/service.rb
usaidwat-1.4.1 lib/usaidwat/service.rb