Sha256: a55931d516548107ae28084f295ba4a637dad29c160003fdf7e871a610e8588e

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module Useless
  module Doc
    module Rack

      # +Doc::Rack::Retriever+ sets an appropriate retriever for the current
      # RACK_ENV. In production it uses the +Standard+ retriever which makes
      # real HTTP calls. Elsewhere, it uses +Stub+ which servers corresponding
      # files from the spec/documents directory.
      #
      class Retriever
        def initialize(app)
          @app = app
        end

        def call(env)
          if ENV['RACK_ENV'] == 'production'
            env['useless.doc.retriever'] = Standard
          else
            env['useless.doc.retriever'] = Stub
          end

          @app.call(env)
        end

        module Standard
          def self.retrieve(url)
            response = Typhoeus.options url, headers: { 'Accept' => 'application/json' }
            response.response_body
          end
        end

        class Stub
          def self.retrieve(url)
            uri = URI(url)
            path = File.dirname(__FILE__) + "/../../../../spec/documents#{uri.path}.json"
            File.read(path)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
useless-doc-0.1.3 lib/useless/doc/rack/retriever.rb
useless-doc-0.1.2 lib/useless/doc/rack/retriever.rb