Skip to content

Commit

Permalink
Updated README and added downcasing for key preparation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanuber committed Aug 30, 2013
1 parent ba99b6d commit 8201e15
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ Hello, world!
```

### HTTP Methods
Files must carry the extension of the HTTP method used to invoke them. Some
examples: `hello.GET`, `hello.POST`, `hello.PUT`, `hello.DELETE`
Files must carry the extension of the HTTP method used to invoke them.

### Headers and Status
You can indicate HTTP headers and status using stdout from your script.
Expand All @@ -71,12 +70,36 @@ content-type: text/plain
EOF
```

Separated by 3 dashes on a line of their own (`\n---\n`), the very last block
Separated by 3 dashes on a line of their own (`---`), the very last block
of output can contain headers and response status.

### Getting request headers and body
The headers and body of the HTTP request will be passed to the script as
arguments. The headers will be passed as $1, and the body as $2.
Headers, query parameters, and request body are all passed to executables using
the environment. To defeat overlap in variables, they are namespaced using a
prefix. The prefixes for environment variables are as follows:

* Headers: `oaf_header_`
* Query parameters: `oaf_query_`
* Request body: `oaf_request_body`

Below is a quick example of a shell script which makes use of the request data:
```bash
#!/bin/bash
if [ -n "$oaf_header_accept" ]; then
echo "You passed the Accept header: $oaf_header_accept"
fi
if [ -n "$oaf_query_myparam" ]; then
echo "You passed the 'myparam' value: $oaf_query_myparam"
fi
if [ -n "$oaf_request_body" ]; then
echo "You passed the request body: $oaf_request_body"
fi
```

Headers query parameter names are converted to all-lowercase, and dashes are
replaced with underscores. This is due to the way the environment works. For
example, if you wanted to get at the `Content-Type` header, you could with the
environment variable `$oaf_header_content_type`.

### Catch-all methods
Catch-all's can be defined by naming a file inside of a directory, beginning and
Expand Down
2 changes: 1 addition & 1 deletion lib/oaf/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def prepare_environment headers, query, body
# A string with the prepared value
#
def prepare_key key
key.gsub('-', '_')
key.gsub('-', '_').downcase
end

# Flatten a hash or array into a string. This is useful for preparing some
Expand Down
5 changes: 5 additions & 0 deletions spec/oaf/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ module Oaf
result.should eq('x_powered_by')
end

it "should convert all letters to lowercase in key names" do
result = Oaf::Util.prepare_key 'X-Powered-By'
result.should eq('x_powered_by')
end

it "should flatten a hash into a string" do
result = Oaf::Util.flatten({'item1' => 'value1'})
result.should eq('item1value1')
Expand Down

0 comments on commit 8201e15

Please sign in to comment.