Skip to content

Instantly share code, notes, and snippets.

@7even
Created October 17, 2014 11:17
Show Gist options
  • Save 7even/419bf7bae6d8a6d426a2 to your computer and use it in GitHub Desktop.
Save 7even/419bf7bae6d8a6d426a2 to your computer and use it in GitHub Desktop.
def post_author(properties_or_obj = {})
# Теперь пользователь может передать свой объект или просто хэш с параметрами
author = if properties_or_obj.respond_to?(:author)
properties_or_obj.to_author
else
properties_or_obj
end
raise ArgumentError unless author.is_a?(Hash)
# fetch позволяет передать блок, который вызывается если ключ не найден
name = author.fetch(:name) { raise KeyError, "Name must be supplied for Author API requests" }
email = author.fetch(:email) { raise KeyError, "Email must be supplied for Author API requests" }
api.send(
name: name,
email: email
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment