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

Support to_h on models #86

Open
barnaclebarnes opened this issue Dec 5, 2024 · 2 comments
Open

Support to_h on models #86

barnaclebarnes opened this issue Dec 5, 2024 · 2 comments

Comments

@barnaclebarnes
Copy link

It would be great if you could support to_h on models. We often want a simple hash of the response and we currently do something like:

subscription.instance_variable_get(:@response).to_h

ChargeBee::Subscription.list(params).instance_variable_get(:@list).map { |s| s.instance_variable_get(:@response).to_h[:subscription] }

Unless there is a better way?

@cb-alish
Copy link
Collaborator

Hi @barnaclebarnes, thanks for reaching out! Starting from v2.47.1, we’ve added a get_raw_response() method to the Result and ListResult classes. This method returns the raw response in hash format. Could you give it a try? Here’s an example:

begin
  result = ChargeBee::Subscription.retrieve("{subscription-id}")
  result_hash = result.get_raw_response
  puts result_hash[:subscription]
rescue ChargeBee::InvalidRequestError => e
  puts "Invalid request: #{e.message}"
rescue ChargeBee::APIError => e
  puts "API error: #{e.message}"
rescue => e
  puts "An error occurred: #{e.message}"
end

Let us know if this works for you!

@cb-alish
Copy link
Collaborator

Hi @barnaclebarnes, did the get_raw_response() method in v2.47.1 work for your use case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants