cassette-rails 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/cassette.rb +62 -0
- metadata +44 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: 824a2094c5317f57e06e2f32dbada2066f7f3324
         | 
| 4 | 
            +
              data.tar.gz: 6ea30e2c82aff9fce15249876092f78fab799eef
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: eb0615acb72a5abb531330fe7a8030db580b9bf2de82ed69142c926ca4501424b33a6fc4e90af1a817d7390ef62d24f1ef8c9348cf62a8b88b6df915362b1b7a
         | 
| 7 | 
            +
              data.tar.gz: 528f4c4a5d48d027a9bba3a8cb54358baf1f336b754532970be7e59fa812f3fe28aaa2549a69dd6900f7af3639d97a6932fe7b0fe86dab59d980a6b5b1f863af
         | 
    
        data/lib/cassette.rb
    ADDED
    
    | @@ -0,0 +1,62 @@ | |
| 1 | 
            +
            require 'uri'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class CassetteMiddleware
         | 
| 4 | 
            +
                def initialize app
         | 
| 5 | 
            +
                  @app = app
         | 
| 6 | 
            +
                end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                def call(env)
         | 
| 9 | 
            +
                  if ENV["CASSETTE_RECORDING"] != "1"
         | 
| 10 | 
            +
                    return @app.call(env)
         | 
| 11 | 
            +
                  end
         | 
| 12 | 
            +
              
         | 
| 13 | 
            +
                  bulk_file_path = ENV["CASSETTE_BULK_FILE_PATH"]
         | 
| 14 | 
            +
                  bulk_file_separator = ENV["CASSETTE_BULK_FILE_SEPARATOR"]
         | 
| 15 | 
            +
              
         | 
| 16 | 
            +
                  request = ActionDispatch::Request.new(env)
         | 
| 17 | 
            +
                  status, response_headers, response = @app.call(env)
         | 
| 18 | 
            +
                  payload = {
         | 
| 19 | 
            +
                    "request.method": request.method,
         | 
| 20 | 
            +
                    "request.path": request.path,
         | 
| 21 | 
            +
                    "request.body": stream_to_string(request.body),
         | 
| 22 | 
            +
                    "request.query": request.GET.map { |k, v| "#{k}=#{v}"},
         | 
| 23 | 
            +
                    "request.cookies": cookies_from_jar(request.cookie_jar),
         | 
| 24 | 
            +
                    "request.headers": relevant_headers(request.headers),
         | 
| 25 | 
            +
                    "response.status": status.to_s,
         | 
| 26 | 
            +
                    "response.body": response.body,
         | 
| 27 | 
            +
                    "response.headers": header_hash_to_list(response_headers),
         | 
| 28 | 
            +
                  }
         | 
| 29 | 
            +
              
         | 
| 30 | 
            +
                  File.open(bulk_file_path, 'a') { |f| 
         | 
| 31 | 
            +
                    f.flock(File::LOCK_EX)
         | 
| 32 | 
            +
                    f.puts(bulk_file_separator)
         | 
| 33 | 
            +
                    f.puts(URI.encode_www_form(payload))
         | 
| 34 | 
            +
                  }
         | 
| 35 | 
            +
              
         | 
| 36 | 
            +
                  return status, response_headers, response
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
              
         | 
| 39 | 
            +
                def relevant_headers(env)
         | 
| 40 | 
            +
                  relevant = env.select { |k,v| (k.start_with? 'HTTP_' or k.upcase == "CONTENT_TYPE") and k.upcase != "HTTP_COOKIE" }
         | 
| 41 | 
            +
                  header_hash_to_list(relevant)
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
              
         | 
| 44 | 
            +
                def header_hash_to_list(hash)
         | 
| 45 | 
            +
                  hash.collect {|k,v| [k.sub(/^HTTP_/, ''), v]}
         | 
| 46 | 
            +
                    .collect {|k,v| [k.downcase.sub('_', '-'), v]}
         | 
| 47 | 
            +
                    .map { |k, v| "#{k}=#{v}"}
         | 
| 48 | 
            +
                end
         | 
| 49 | 
            +
              
         | 
| 50 | 
            +
                def stream_to_string(stream)
         | 
| 51 | 
            +
                  stream.rewind
         | 
| 52 | 
            +
                  str = stream.read.to_s
         | 
| 53 | 
            +
                  stream.rewind
         | 
| 54 | 
            +
                  return str
         | 
| 55 | 
            +
                end
         | 
| 56 | 
            +
              
         | 
| 57 | 
            +
                def cookies_from_jar(cookie_jar)
         | 
| 58 | 
            +
                  cookies = []
         | 
| 59 | 
            +
                  cookie_jar.each { |cookie| cookies << "#{cookie[0]}=#{cookie[1]}" }
         | 
| 60 | 
            +
                  cookies
         | 
| 61 | 
            +
                end
         | 
| 62 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,44 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: cassette-rails
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.1
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Joachim Lindqvist
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2019-05-19 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies: []
         | 
| 13 | 
            +
            description: Rails middleware for cassette.dev
         | 
| 14 | 
            +
            email: lindqvist.joachim@gmail.com
         | 
| 15 | 
            +
            executables: []
         | 
| 16 | 
            +
            extensions: []
         | 
| 17 | 
            +
            extra_rdoc_files: []
         | 
| 18 | 
            +
            files:
         | 
| 19 | 
            +
            - lib/cassette.rb
         | 
| 20 | 
            +
            homepage: https://rubygems.org/gems/cassette-rails
         | 
| 21 | 
            +
            licenses:
         | 
| 22 | 
            +
            - MIT
         | 
| 23 | 
            +
            metadata: {}
         | 
| 24 | 
            +
            post_install_message: 
         | 
| 25 | 
            +
            rdoc_options: []
         | 
| 26 | 
            +
            require_paths:
         | 
| 27 | 
            +
            - lib
         | 
| 28 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 29 | 
            +
              requirements:
         | 
| 30 | 
            +
              - - ">="
         | 
| 31 | 
            +
                - !ruby/object:Gem::Version
         | 
| 32 | 
            +
                  version: '0'
         | 
| 33 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 34 | 
            +
              requirements:
         | 
| 35 | 
            +
              - - ">="
         | 
| 36 | 
            +
                - !ruby/object:Gem::Version
         | 
| 37 | 
            +
                  version: '0'
         | 
| 38 | 
            +
            requirements: []
         | 
| 39 | 
            +
            rubyforge_project: 
         | 
| 40 | 
            +
            rubygems_version: 2.5.2.3
         | 
| 41 | 
            +
            signing_key: 
         | 
| 42 | 
            +
            specification_version: 4
         | 
| 43 | 
            +
            summary: Rails middleware for cassette.dev
         | 
| 44 | 
            +
            test_files: []
         |