futuresimple/api_client — pull request #9 by michalbugno (2014-04-01 09:26:23+00:00)
Title: Strict attribute reader

This pull request allows users to set strict attribute reading which raises KeyError for attributes which don't exist (the same as Hash#fetch and actually implemented using it).

# Why?
Errors which occur because you thought the key was in the response you fetched but it is never there (different representation).

Example fail:
```
if api_object.email # not every object has email but some do
# handle stuff if email is there but it was never there so this doesn't work
end
```

# Usage

Errors which are caused by this should probably not be rescued because you don't want to have Exception-driven flow.

Instead of this:

```
begin
object.custom_fields
rescue KeyError
ojbect.custom_field_values
end
```

You probably want to:

```
if object.custom_fields?
object.custom_fields
else
# blabla
end
```

# Testing

Tested on sandbox mailman and seems to work. Also this doesn't affect anything until you enable it in fs-api probably. In mailman for testing purposes we did:

```
class ApiClient::Base
def strict_attr_reader?
true
end
end
```

We can do it gradually on a per-class basis, eventually we probably want to have it on all fs-api classes?