Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

Consuming an API Using HTTParty and Creating a Gem Discussion

General • Asked by Chris Oliver

Nice one Chris, I like this episode.

Thx for interesting video. waiting for the second part :)

Great video... could it have worked if instead of defining that self.random calls the self.tagged method, you would have declared an alias? as: alias_method :random, :tagged

You could do that as well. The only thing to keep in mind is if you ever plan on changing the implementation, you'll want to bring it back out into its own method again.

:D thanks for the tip!

This arrived on the exact day that I discovered that in upgrading to rails 4.1, my activeresource api connection (that powered everything) didn't work anymore. Great video, saved me a ton of time and headaches.

so if I wanted to consume the Fitbit API or RescueTime API and try to visualize it, I'd be able to do so through HTTParty?

Thanks Chris!

Good episode Chris. There is something to be aware of; the private method in the Animatedgifme module is not actually private. In order to accomplish this, you have to declare that method private explicitly with private_class_method :retrieve_url. It's one of those gotchas in Ruby ;)

Excellent!! Thanks Chris, this is very helpful.

I am wondering how we would work an API key or token into this process? Most of the API's out there seem to want you to use an API key and I am having trouble implementing that with httparty. It would be great if that was part of a follow up. Thanks.

Absolutely! The authentication is definitely important and one of the next on my list to cover as a follow up pro episode. I'll send you an early copy to review it. :)

You rock man!!

id be interested in that when its released. Also a lot of API's are using OATH and it would be good to see how those can be digested. Really great screencast.

Episode 33 covers the basics of OAuth using Omniauth and Twitter as an example that might be helpful for you.

Nice. I did some kind of this task a few weeks ago. Nice to consolidate the knowledge.

Thanks for this, really really helpful. I need to create a gem for interacting with an API and was thinking of using HTTParty but didn't know how to do the bundling stuff. Also, another neat tip is the "bundle console" command to debug in the console without having to worry about hopping to the IRB and loading libraries.

Oh yeah! Forgot about the bundle console command. I'll be sure to use that more often in the future. Thanks! :)

Oh! nice I didn't know about this command. Was having a little trouble requiring this gem in irb but "bundle console" did it all for me.

Can you use httparty with web services that has a XML output ?

Yep! I think you can even have it automatically parse the XML if you have the multi_xml gem installed.

Great! Thx for quick reply!

Hey Chris thanks for that TuT. Short question. I cloned your gem and tried to run it in irb as you did.

$ bundle -v
Bundler version 1.7.11
$ ruby -v
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-darwin14.0]

I get this error by require_relative. LoadError: cannot load such file -- animatedgifme/version

from /Users/stefanmaier/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'

from /Users/stefanmaier/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'

from /Users/stefanmaier/workspace/projects/gems/animatedgifme/lib/animatedgifme.rb:1:in `<top (required)="">'

from (irb):1:in `require_relative'

from (irb):1

from /Users/stefanmaier/.rvm/rubies/ruby-2.1.4/bin/irb:11:in `<main>'

I tried to build an other gem as well and get the same error.

Hi there,
I got the same error, did you happen to find the cause?

place require 'bundle/setup' to animatedgifme.rb in first line

@disqus_lKf13yxio5:disqus @disqus_rJnL9ex9US:disqus change 'require' to 'require_relative'. This solved the issue for me, apparently it has something to do with your $PATH variable. Sorry I can't provide a better explanation, as I don't quite understand it, but from my reading, differences in $PATH is why this would work for some and not others.

you just need to comment the first line in animatedgifme.rb , like this
# require "animatedgifme/version"

because the require_relative will automatically resolve the dependency.

the retrieve_url method should be class method which is called directly by the module, the source code had already fixed it

@chris have you seen this gem? interested to know your thoughts on this? using Rack middleware when processing the request/response cycle.

Yeah, Faraday is great, especially for making gems, that way you can easily swap out the backend library that makes the HTTP requests.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 31,353+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.