Skip to main content

32 Consuming an API Using HTTParty and Creating a Gem

Episode 23 · September 7, 2014

Learn how to use HTTParty to consume an API and package your library into a gem

Gems APIs


Transcripts

No transcripts available. Earn a free month

Discussion


Gravatar

Nice one Chris, I like this episode.


Gravatar

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


Gravatar

Gravatar

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

Gravatar

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.

Gravatar

:D thanks for the tip!


Gravatar

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.


Gravatar

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?


Gravatar

Thanks Chris!


Gravatar

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 ;)


Gravatar

Excellent!! Thanks Chris, this is very helpful.


Gravatar

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.

Gravatar

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. :)

Gravatar

You rock man!!

Gravatar

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.

Gravatar

Episode 33 covers the basics of OAuth using Omniauth and Twitter as an example that might be helpful for you. https://gorails.com/episode...


Gravatar

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


Gravatar

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.

Gravatar

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

Gravatar

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.


Gravatar

Gravatar

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

Gravatar

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

Gravatar

Great! Thx for quick reply!


Gravatar

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.

Gravatar

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

Gravatar

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

Gravatar

@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.

Gravatar

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.


Gravatar

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


Login or create an account to join the conversation.