blest 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +47 -56
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 7cbe0f3613fd73841d959cad671fb7ec368ff5de50a2e5e874048871070d70b5
         | 
| 4 | 
            +
              data.tar.gz: 209e015fa2bd51a03476ab597bbc4c4109037cfea52b79904a27f2a54fc1b368
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 8d9a2f9db5673835a9d0187374b52be72dd596a5ec9a3744a164cb6aecbefddfa0d96b3cd23bce63a706dd39fcdcde0e2c5f0231cd71086208d34820280b1d05
         | 
| 7 | 
            +
              data.tar.gz: e5d92953980bfe4a4e9a0d9c6c98db057cb5f5067af87680b2e83b4dc37df7ac52c5c3042697f63e9652dbefd3db2b0740f7c124b1837d330accd4b0360bfd57
         | 
    
        data/README.md
    CHANGED
    
    | @@ -15,13 +15,13 @@ For a front-end implementation in React, please visit https://github.com/jhuntde | |
| 15 15 | 
             
            - Single Endpoint - Reduce complexity and improve data privacy
         | 
| 16 16 | 
             
            - Fully Encrypted - Improve data privacy
         | 
| 17 17 |  | 
| 18 | 
            -
             | 
| 18 | 
            +
            ## Installation
         | 
| 19 19 |  | 
| 20 | 
            -
            Install BLEST  | 
| 20 | 
            +
            Install BLEST Ruby from Rubygems.
         | 
| 21 21 |  | 
| 22 22 | 
             
            ```bash
         | 
| 23 | 
            -
             | 
| 24 | 
            -
            ``` | 
| 23 | 
            +
            gem install blest
         | 
| 24 | 
            +
            ```
         | 
| 25 25 |  | 
| 26 26 | 
             
            ## Usage
         | 
| 27 27 |  | 
| @@ -32,11 +32,9 @@ Use the `create_request_handler` function to create a request handler suitable f | |
| 32 32 | 
             
            ### create_request_handler
         | 
| 33 33 |  | 
| 34 34 | 
             
            ```ruby
         | 
| 35 | 
            -
            require ' | 
| 35 | 
            +
            require 'webrick'
         | 
| 36 36 | 
             
            require 'json'
         | 
| 37 | 
            -
            require ' | 
| 38 | 
            -
             | 
| 39 | 
            -
            server = TCPServer.new('localhost', 8080)
         | 
| 37 | 
            +
            require 'blest'
         | 
| 40 38 |  | 
| 41 39 | 
             
            # Create some middleware (optional)
         | 
| 42 40 | 
             
            auth_middleware = ->(params, context) {
         | 
| @@ -65,73 +63,66 @@ router = { | |
| 65 63 | 
             
            # Create a request handler
         | 
| 66 64 | 
             
            handler = create_request_handler(router)
         | 
| 67 65 |  | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
               | 
| 75 | 
            -
              if request.nil?
         | 
| 76 | 
            -
                client.close
         | 
| 77 | 
            -
              else
         | 
| 78 | 
            -
             | 
| 79 | 
            -
                method, path, _ = request.split(' ')
         | 
| 80 | 
            -
             | 
| 81 | 
            -
                if method != 'POST' 
         | 
| 82 | 
            -
                  client.puts "HTTP/1.1 405 Method Not Allowed"
         | 
| 83 | 
            -
                  client.puts "\r\n"
         | 
| 84 | 
            -
                elsif path != '/'
         | 
| 85 | 
            -
                  client.puts "HTTP/1.1 404 Not Found"
         | 
| 86 | 
            -
                  client.puts "\r\n"
         | 
| 87 | 
            -
                else
         | 
| 88 | 
            -
                  content_length = 0
         | 
| 89 | 
            -
                  while line = client.gets
         | 
| 90 | 
            -
                    break if line == "\r\n"
         | 
| 91 | 
            -
                    content_length = line.split(': ')[1].to_i if line.start_with?('Content-Length')
         | 
| 92 | 
            -
                  end
         | 
| 66 | 
            +
            class HttpRequestHandler < WEBrick::HTTPServlet::AbstractServlet
         | 
| 67 | 
            +
              def do_OPTIONS(request, response)
         | 
| 68 | 
            +
                response.status = 200
         | 
| 69 | 
            +
                response['Access-Control-Allow-Origin'] = '*'
         | 
| 70 | 
            +
                response['Access-Control-Allow-Methods'] = 'POST, OPTIONS'
         | 
| 71 | 
            +
                response['Access-Control-Allow-Headers'] = 'Content-Type'
         | 
| 72 | 
            +
              end
         | 
| 93 73 |  | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 74 | 
            +
              def do_POST(request, response)
         | 
| 75 | 
            +
                response['Content-Type'] = 'application/json'
         | 
| 76 | 
            +
                response['Access-Control-Allow-Origin'] = '*'
         | 
| 96 77 |  | 
| 78 | 
            +
                # Parse JSON body
         | 
| 79 | 
            +
                begin
         | 
| 80 | 
            +
                  payload = JSON.parse(request.body)
         | 
| 81 | 
            +
                  
         | 
| 82 | 
            +
                  # Define the request context
         | 
| 97 83 | 
             
                  context = {
         | 
| 98 84 | 
             
                    headers: request.headers
         | 
| 99 85 | 
             
                  }
         | 
| 100 86 |  | 
| 101 | 
            -
                  # Use the request handler | 
| 102 | 
            -
                  result, error = handler.( | 
| 87 | 
            +
                  # Use the request handler
         | 
| 88 | 
            +
                  result, error = handler.(payload, context)
         | 
| 103 89 |  | 
| 90 | 
            +
                  # Do something with the result or error
         | 
| 104 91 | 
             
                  if error
         | 
| 105 | 
            -
             | 
| 106 | 
            -
             | 
| 107 | 
            -
             | 
| 108 | 
            -
                      client.puts "Content-Length: #{response.bytesize}"
         | 
| 109 | 
            -
                      client.puts "\r\n"
         | 
| 110 | 
            -
                      client.puts response
         | 
| 92 | 
            +
                    response_body = error.to_json
         | 
| 93 | 
            +
                    response.status = 500
         | 
| 94 | 
            +
                    response.body = response_body
         | 
| 111 95 | 
             
                  elsif result
         | 
| 112 | 
            -
             | 
| 113 | 
            -
             | 
| 114 | 
            -
             | 
| 115 | 
            -
                      client.puts "Content-Length: #{response.bytesize}"
         | 
| 116 | 
            -
                      client.puts "\r\n"
         | 
| 117 | 
            -
                      client.puts response
         | 
| 96 | 
            +
                    response_body = result.to_json
         | 
| 97 | 
            +
                    response.status = 200
         | 
| 98 | 
            +
                    response.body = response_body
         | 
| 118 99 | 
             
                  else
         | 
| 119 | 
            -
             | 
| 100 | 
            +
                    response.status = 204
         | 
| 120 101 | 
             
                  end
         | 
| 121 | 
            -
                end
         | 
| 122 | 
            -
             | 
| 123 | 
            -
                client.close
         | 
| 124 102 |  | 
| 103 | 
            +
                rescue JSON::ParserError
         | 
| 104 | 
            +
                  response.status = 400
         | 
| 105 | 
            +
                  response.body = { message: 'Invalid JSON' }.to_json
         | 
| 106 | 
            +
                end
         | 
| 125 107 | 
             
              end
         | 
| 126 108 | 
             
            end
         | 
| 109 | 
            +
             | 
| 110 | 
            +
            # Create WEBrick server
         | 
| 111 | 
            +
            server = WEBrick::HTTPServer.new(Port: 8000)
         | 
| 112 | 
            +
             | 
| 113 | 
            +
            # Mount custom request handler
         | 
| 114 | 
            +
            server.mount('/', HttpRequestHandler)
         | 
| 115 | 
            +
             | 
| 116 | 
            +
            trap('INT') { server.shutdown }
         | 
| 117 | 
            +
             | 
| 118 | 
            +
            # Start the server
         | 
| 119 | 
            +
            server.start
         | 
| 127 120 | 
             
            ```
         | 
| 128 121 |  | 
| 129 122 | 
             
            ### create_http_server
         | 
| 130 123 |  | 
| 131 124 | 
             
            ```ruby
         | 
| 132 | 
            -
            require ' | 
| 133 | 
            -
            require 'json'
         | 
| 134 | 
            -
            require './blest.rb'
         | 
| 125 | 
            +
            require 'blest'
         | 
| 135 126 |  | 
| 136 127 | 
             
            # Create some middleware (optional)
         | 
| 137 128 | 
             
            auth_middleware = ->(params, context) {
         |