Sha256: 5c355d5fed309c5088cde2d1101c22bcc2bc96ed8372048ed4d0888199a0201b

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module CalendariumRomanum
  module Remote
    # Orchestrates retrieval and deserialization of calendar data
    # from a server accessed throught HTTP GET requests
    # and returning JSON responses.
    class Driver
      def initialize(uri_scheme, denormalizer)
        @uri_scheme = uri_scheme
        @denormalizer = denormalizer
      end

      def day(date)
        process :day, date
      end

      def year(year)
        process :year, year
      end

      protected

      def process(action, argument)
        uri = @uri_scheme.public_send action, argument
        response = request uri
        handle_errors response
        data = MultiJson.load response.body

        @denormalizer.public_send action, data
      end

      def request(uri)
        request = HTTPI::Request.new uri
        request.headers['Accept'] = 'application/json'
        request.headers['User-Agent'] =
          'calendarium-romanum-remote/' + Remote::VERSION

        HTTPI.get request
      end

      def handle_errors(response)
        if response.code != 200
          raise UnexpectedResponseError.new("Unexpected HTTP status #{response.code.inspect}")
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
calendarium-romanum-remote-0.3.0 lib/calendarium-romanum/remote/driver.rb
calendarium-romanum-remote-0.2.0 lib/calendarium-romanum/remote/driver.rb