Skip to content

Instantly share code, notes, and snippets.

@krainboltgreene
Created August 28, 2015 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krainboltgreene/ef5e94854259dd9ef9dd to your computer and use it in GitHub Desktop.
Save krainboltgreene/ef5e94854259dd9ef9dd to your computer and use it in GitHub Desktop.
# Brainstorming A Ruby-based API to API mapper
There are three defined layers:
- Deserializer (Format -> Native)
- Normalizer (accounts.name -> accounts.FullName)
- Serializer (Native -> Format)
JSONDesieralizer
Normalizer (JSONAPI -> SOAP)
XMLSerializer
## Normalizer Operations
- Pair Key Operation
- Pair Value Operation (coercion)
- Add Pairs
``` ruby
class Normalizer < Brainstorm::Normalizer
normalize :request do
pair "accounts.first_name", "accounts.name", -> { |name| name.split.first }
pair "accounts.last_name", "accounts.name", -> { |name| name.split.last }
pair "accounts.age", 13
end
def shadow.initialize(struct)
@struct = struct
end
def shadow.to_request
42
end
def shadow.to_response
{
"accounts" => {
"first_name" => data["accounts"]["name"].split.first,
"last_name" => data["accounts"]["name"].split.last
}
}
end
end
```
## Example Input
```
POST http://api.example.com/accounts
Authentication: Bearer asdkjfnasdlkfnasdkfn
{
"accounts": {
"FullName": "Kurtis"
}
}
```
```
200 OK
...
{
"accounts": {...}
}
```
## chatty section:
Yes.
## A possible DSL?
```
define_route
"foreign_point": "local_path",
method: <method>,
accepted_params: <params>
define_route
"http://shittyAssUrl.com/api/SuperHero/SOAP.asp": "/user",
method: :post,
accepted_params: [:name, :age, :sex, :occupation, :secret_identity]
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment