Sha256: d3ff23318b690d6416bc92327fed50faca590350717c97704e3cccc521398e09

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module CalendariumRomanum
  module Remote
    module Drivers
      # Communicates with the remote API using Ruby standard library
      class NetHttpDriver
        def get(date, calendar_uri)
          uri_str =
            calendar_uri +
            (calendar_uri.end_with?('/') ? '' : '/') +
            "#{date.year}/#{date.month}/#{date.day}"
          uri = URI(uri_str)

          get_request uri
        end

        def year(year, calendar_uri)
          uri_str =
            calendar_uri +
            (calendar_uri.end_with?('/') ? '' : '/') +
            year.to_s
          uri = URI(uri_str)

          get_request uri
        end

        private

        def get_request(uri)
          begin
            response = Net::HTTP.get_response uri
          rescue SocketError, Errno::ECONNREFUSED => err
            raise ServerNotFoundError.new err.message
          rescue Timeout::Error, Errno::EINVAL,
                 Errno::ECONNRESET, EOFError,
                 Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
                 Net::ProtocolError => err
            raise TransportError.new err.message
          end

          if response.code == '200'
            return response.body
          elsif response.code == '400'
            json = JSON.parse(response.body)
            raise BadRequestError.new(json['error'])
          else
            raise Error.new("Unexpected status #{response.code.inspect}")
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
calendarium-romanum-remote-0.1.0 lib/calendarium-romanum/remote/drivers/net_http_driver.rb