Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for bodies other than objects #184

Merged
merged 1 commit into from Feb 2, 2023
Merged

Conversation

phylor
Copy link
Collaborator

@phylor phylor commented Jan 31, 2023

Closes #183.

TLDR

This adds support for bodies we receive, which are not an object. For example, arrays or plain strings.

Let's say we get the response:

{
  data: [
    { name: 'ride_notifications', channels: ['sms'], type: 'notification_setting' }
  ]
}

You could now parse this with the following model:

class Ioki::Model::Passenger::NotificationSettings
  base 'Array', item_class_name: 'NotificationSetting'
end

Changes

  • Up until now all response bodies we parse in ioki-ruby need to be objects (in practice a Hash). We now support arrays and basic types (String, ..) as well.

    For arrays we need to define an item_class_name which specifies the class of the items in the array.

    class Ioki::Model::Passenger::NotificationSettings
      base 'Array', item_class_name: 'NotificationSetting'
    end

    For basic types, we only define the type:

    class Ioki::Model::Passenger::ShuttleColor
      base 'String'
    end
  • Note that even if the response is a basic type or an array, ioki-ruby will always return a model inheriting Ioki::Model::Base. This allows for helper methods such as serialize and also allows access to the etag using model._etag.

    model = Ioki::Model::Passenger::NotificationSettings.new([{name: 'ride', channels: ['sms']}], 'etag-123')
    
    model._etag
    # => 'etag-123'
    
    model._raw_attributes
    # => [{name: 'ride', channels: ['sms']}]
    # `#_raw_attributes` returns the input value
    
    model.data
    # => [Ioki::Model::Passenger::NotificationSetting<name='ride' channels=['sms']>]
    # `#data` returns the parsed objects
  • New method #data which returns the parsed data. For object responses, it returns #attributes. The #data method should be used in client applications to get the parsed array response.

    model = Ioki::Model::Passenger::Product.new(name: 'MyProduct')
    
    model.attributes
    # => {name: 'MyProduct'}
    
    model.data
    # => {name: 'MyProduct'}

@phylor phylor marked this pull request as ready for review February 1, 2023 13:07
@phylor
Copy link
Collaborator Author

phylor commented Feb 1, 2023

@tom-ioki If you have time, this would be a PR about array bodies. Now supporting etags. Let me know if there are things you imagined differently!

Copy link
Contributor

@tom-ioki tom-ioki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Love it and thank you 😊

@phylor phylor merged commit a14da7f into main Feb 2, 2023
@phylor phylor deleted the feature/array-body branch February 2, 2023 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle array responses
2 participants