Sha256: df53828770217c5e1ea2cf7649c725cf3b777ffc9d5fbc050c6fb25caa5a8fcc

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

require 'requests'

module USaidWat
  module Service
    class RedditService
      def user(username)
        data = %w{about comments submitted}.reduce({}) { |memo, obj| memo.update(obj.to_sym =>  get_page(username, obj)) }
        USaidWat::Thing::User.new(username, data[:about], data[:comments], data[:submitted])
      end

      private

      def get_page(username, page)
        url = "https://www.reddit.com/user/#{username}/#{page}.json"
        url += '?limit=100' if ['comments', 'submitted'].include?(page)
        get(url)
      end

      def get(uri)
        hdrs = {'User-Agent' => "usaidwat v#{USaidWat::VERSION}"}
        Requests.request('GET', uri, :headers => hdrs).json
      rescue Timeout::Error
        :server_error
      rescue Requests::Error => e
        case e.response.code.to_i
        when 404 then :no_such_user
        when 500 then :server_error
        else          :nil
        end
      end
    end

    class MockService
      def user(username)
        USaidWat::Thing::User.new(username,
                                  load_data("user_#{username}.json"),
                                  load_data("#{username}.json"),
                                  load_data("submissions_#{username}.json"))
      end

      private

      def load_data(data_file)
        path = File.join(File.dirname(__FILE__), "..", "..", "features", "fixtures", data_file)
        return :no_such_user unless File.exists?(path)
        JSON.parse(IO.read(path))
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
usaidwat-1.6.1 lib/usaidwat/service.rb
usaidwat-1.6.0 lib/usaidwat/service.rb
usaidwat-1.5.1 lib/usaidwat/service.rb
usaidwat-1.5.0 lib/usaidwat/service.rb