Consuming an API Using HTTParty and Creating a Gem Discussion
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.
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?
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 ;)
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. :)
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.
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.
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.
@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? https://github.com/lostisland/faraday 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.