# Responses

Every method returns an object built from the API's JSON, with one reader per field under the API's own name. `[]` reads any field by name, and `to_h` returns the decoded body.

fields.rbruby

```ruby
response = client.resolve_address("paf_2670849")
address = response.result

puts address.line_1    # reader
puts address["line_1"] # the API's field name, same value
```

## Field names

Readers use the API's field names verbatim: `line_1`, `post_town`, `udprn`. A few fields are camelCase in the API, such as `dailyCount` and `hasMore`; each also has a snake\_case alias, `daily_count` and `has_more`. Native records keep their source dataset's names, so a Korean address has readers such as `법정동코드` and a Danish one `wgs84koordinat_længde`.

## Missing and unknown fields

A field the API leaves out reads as `nil`, so a filtered request or an optional field never raises. A field the API adds after your SDK version has no reader yet, but `[]` and `to_h` return it, so you can read it before you upgrade.

## Raw hash

Call `to_h` on any response or nested object. It returns the decoded body with the API's field names, unknown fields included, so it round-trips through `JSON.generate`. The hash is frozen; `dup` it to change it.

hash.rbruby

```ruby
data = response.to_h
puts JSON.pretty_generate(data)
```

Two objects are `==` when they are the same class and hold the same body.

## Types

| Module                      | Holds                                                                                                                            |
| --------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `IdealPostcodes::Responses` | One class per operation response, such as `PostcodeResponse` and `AutocompleteResponse`. Each has `code`, `message` and `result` |
| `IdealPostcodes::Types`     | Everything inside a result: `Address`, `AddressListItem`, `UkAddressSuggestion`, `Licensee`, `Config` and the native records     |

The gem's `sig/` directory has an RBS signature for every class, reader and method, so Steep, Ruby LSP and other RBS tools check the keywords you pass and the readers you call.

## Union results

Some results come in one of several forms, and the SDK picks the class that matches. Check with `is_a?` or `case`.

| Field                            | Classes                                                                                   |
| -------------------------------- | ----------------------------------------------------------------------------------------- |
| `find_address` hits              | `UkAddressSuggestion` with a `udprn`, or `AddressSuggestion` for addresses outside the UK |
| `address_cleanse` result         | `GbrCleanseMatch` with a `match` address, or `GbrCleanseNoMatch`                          |
| `email_validation` result        | `Email`, or `UnknownEmail` when deliverability cannot be checked                          |
| `phone_number_validation` result | `PhoneNumber`, or `InvalidPhoneNumber`                                                    |

union.rbruby

```ruby
case (result = client.address_cleanse(query: "9 Malyons Rd, Swanley, BR8").result)
when IdealPostcodes::Types::GbrCleanseMatch
  puts "#{result.match.postcode} with confidence #{result.confidence}"
when IdealPostcodes::Types::GbrCleanseNoMatch
  puts "No match"
end
```

## New values

Your integration keeps working. The SDK never checks values against the spec, so a fixed set such as `premise_match` on a cleanse result can gain a value without breaking anything: compare against the values you handle and fall through for the rest. The RBS signatures list the known values in a comment.

## Native records

`native` on an `Address` is the raw record from the address's source dataset, as a class named for the dataset: `PafAddress` for Royal Mail PAF, `AbpAddress` for AddressBase Premium, `UspsAddress` for the United States and so on. The `dataset` field on the address says which. A dataset the SDK does not know yet comes back as a plain hash, so a new dataset never breaks your integration. `native` is `nil` when the API sends no record.

native.rbruby

```ruby
address = client.resolve_address("paf_2670849").result

puts address.native.delivery_point_suffix if address.native.is_a?(IdealPostcodes::Types::PafAddress)
```

The [data sources](/docs/data.md) pages document every dataset's fields.
