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
Benjamin on

Nice one Chris, I like this episode.


Gravatar
Tomasz Panek on

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


Gravatar
Rodrigo Reginato (10 XP) on

Great video. congratz!


Gravatar
antillas21 on

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
Chris Oliver (169,620 XP) on

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
antillas21 on

:D thanks for the tip!


Gravatar
josh bradley (10 XP) on

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
Will Lam (30 XP) on

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
ninja_turtle on

Thanks Chris!


Gravatar
Daniel Gomez Sierra on

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
Jordan on

Excellent!! Thanks Chris, this is very helpful.


Gravatar
Jordan on

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
Chris Oliver (169,620 XP) on

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
Jordan on

You rock man!!

Gravatar
Damien Hogan (120 XP) on

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
Chris Oliver (169,620 XP) on

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
Innokenty Longway on

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


Gravatar
Erik Minkel on

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
Chris Oliver (169,620 XP) on

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

Gravatar
Jonathan Goodman (10 XP) on

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
ari wibowo (10 XP) on

really good


Gravatar
Björn Steneram (160 XP) on

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

Gravatar
Chris Oliver (169,620 XP) on

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

Gravatar
Björn Steneram (160 XP) on

Great! Thx for quick reply!


Gravatar
Stefan Maier on

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
gus martin (10 XP) on

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

Gravatar
jay zen (320 XP) on

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

Gravatar
Thomas Bush (3,100 XP) on

@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
RoBeRt Lee (10 XP) on

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
jay zen (320 XP) on

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.