Ask A Question

Notifications

You’re not receiving notifications from this thread.

Consuming an API Using HTTParty and Creating a Gem Discussion

Nice one Chris, I like this episode.

Reply

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

Reply

Great video. congratz!

Reply

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

Reply

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.

Reply

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.

Reply

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?

Reply

Thanks Chris!

Reply
Daniel Gomez Sierra Daniel Gomez Sierra

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

Reply

Excellent!! Thanks Chris, this is very helpful.

Reply

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.

Reply

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

Reply
Innokenty Longway Innokenty Longway

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

Reply

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.

Reply

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

Reply

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.

Reply

really good

Reply

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

Reply

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

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.

Reply

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

Reply

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

Reply

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.

Reply

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

Reply

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

Reply

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

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.