Class: Waxx::Req

Inherits:
Struct
  • Object
show all
Defined in:
waxx/req.rb

Overview

The Request Struct gets instanciated with each request (x.req) “` x.req.env # A hash of the request environment x.req.data # The raw body of put, post, patch requests x.req.meth # Request method as a string: GET, PUT, POST, PATCH, DELETE x.req.uri # The URI x.req.get # The GET/URL parameters (has with string keys and values) shortcut: x => 'value' x.req.post # The POST/BODY params with string keys and values) shortcut: x => 'value' “`

GET params can be delimited with & or ;. The following are equivilant:

“` localhost:7777/waxx/env?x=1&y=2&z=3 localhost:7777/waxx/env?x=1;y=2;z=3 “`

Note that single params are single values and will be set to the last value received. Params names with “[]” appended are array values.

“` localhost:7777/waxx/env?foo=1;foo=2;foo=3 x => “3”

localhost:7777/waxx/env?foo[]=1;foo[]=2;foo[]=3 x => [“1”, “2”, “3”]

“` If you are uploading JSON directly in the body (with the content type application/json or text/json), then the types are matched.

Given the a request like:

“` Content-Type: application/json

'a “`

The following vars are of the type submitted

“` x => 123 (as an int) x => ['a','1',2] (1 is a string and 2 is an int) “`

### File Uploads Given the form:

“` <form action=“/file/upload” method=“post” enctype=“multipart/form-data”>

<input type="file" name="file">
<button type="submit">Upload File</button>

</form> “`

The following hash is available (symbol keys):

“` x[:filename] => 'file_name.ext' x[:data] => The content of the file x[:content_type] => The Content-Type as sent by the browser x[:headers] => An hash of other headers send by the browser regarding this file “`

How to save a file to the tmp folder:

*app/file/file.rb*

“` module App::File

extend Waxx::Object
runs(
  upload: {
    desc: 'Upload a file to the tmp folder',
    post: -> (x) {
      # Strip any non-word chars and save the file to the tmp folder
      File.open("#{Waxx::Root}/tmp/(x/:file/:filename).gsub(/[\W\.]/,'-'),'wb'){|f| 
        f << x/:file/:data
      }
      x << "Your file has been uploaded."
    }
  }
)

end “`

Instance Attribute Summary collapse

Instance Attribute Details

#cookiesObject

Returns the value of attribute cookies

Returns:

  • (Object)

    the current value of cookies



104
105
106
# File 'waxx/req.rb', line 104

def cookies
  @cookies
end

#dataObject

Returns the value of attribute data

Returns:

  • (Object)

    the current value of data



104
105
106
# File 'waxx/req.rb', line 104

def data
  @data
end

#envObject

Returns the value of attribute env

Returns:

  • (Object)

    the current value of env



104
105
106
# File 'waxx/req.rb', line 104

def env
  @env
end

#getObject

Returns the value of attribute get

Returns:

  • (Object)

    the current value of get



104
105
106
# File 'waxx/req.rb', line 104

def get
  @get
end

#methObject

Returns the value of attribute meth

Returns:

  • (Object)

    the current value of meth



104
105
106
# File 'waxx/req.rb', line 104

def meth
  @meth
end

#postObject

Returns the value of attribute post

Returns:

  • (Object)

    the current value of post



104
105
106
# File 'waxx/req.rb', line 104

def post
  @post
end

#start_timeObject

Returns the value of attribute start_time

Returns:

  • (Object)

    the current value of start_time



104
105
106
# File 'waxx/req.rb', line 104

def start_time
  @start_time
end

#uriObject

Returns the value of attribute uri

Returns:

  • (Object)

    the current value of uri



104
105
106
# File 'waxx/req.rb', line 104

def uri
  @uri
end