0

I'm writing an API wrapper in Ruby and not sure how to proceed. HTTParty is very nice and takes care of everything for me, but on HTTParty::get it returns an HTTParty::Response object.

I want to know, is it better to:

  1. Let the user work with the HTTParty::Response object directly
  2. Convert to a Hash somehow?
  3. Create an instance of a class that has attributes/fields as the keys in the Response object?
SysDragon
  • 9,692
  • 15
  • 60
  • 89
DillPixel
  • 955
  • 1
  • 14
  • 20

1 Answers1

1

I'm working on a project where I do #3 actually. I use HTTParty to convert the data to a hash and then use the hash to create a related set of ActiveRecord classes. It's easier to manipulate the data (do validation for example) in a Ruby class than it is to leave it in the raw hash.

Richard Brown
  • 11,346
  • 4
  • 32
  • 43
  • I'm going to ignore the ActiveRecord stuff (quick google search shows its relevant to Ruby on Rails) before I ask. What if the API can return numerous JSON structures, ie. I am require to create 10+ classes before I can use them? Or is there an alternative to this? – DillPixel Feb 20 '13 at 17:03
  • I originally tried the tactic of making a class that stored the HTTParty hash and then parsed it on demand. It felt very fragile and tied to the original structure of the data. Having that data parsed into relevant classes seemed a better fit, and made for easier testing. YMMV. :) – Richard Brown Feb 20 '13 at 17:08