How to use rails gem to connect to API? - ruby-on-rails

I'm trying to connect my RoR app to the api at this website https://www.humanapi.co
There is a gem available here, https://github.com/humanapi/humanapi, that is supposed to make the process easy. I have read through the documentation several times Unfortunately, because I am new to RoR and web development in general, I'm not exactly sure how to implement this. Any help you can give is greatly appreciated!
Specifically: It says to configure the initilizer using these values..
HumanApi.config do |c|
c.app_id = ENV['HUMANAPI_KEY']
c.query_key = ENV['HUMANAPI_SECRET']
end
I understand this in concept, i have used omniauth etc before. But do I need to create another initializer or just use an existing one?
and finally, this is what i don't understand the most...
humanizable :get_the_token
def get_the_token
# the code to reach the token
"# the code to reach the token
I'm at a complete loss with that, how would i get the token? here is the documentation for web access to the api (which i've read a lot) http://hub.humanapi.co/docs/connect-web-guide

Well, it's not your fault, the documentation of the gem is poorly written and doesn't show what exactly is going on or what is it even doing
This gem is also outdated, created 3 years ago and there are no issues or updates or anything. you better go looking for a different gem that do the functionality you want

Related

Using rails to consume web services/apis

I'm new to the rails world and am trying to build a app to simply allow me to search things on Amazon and such sites based on the users input.
I've done a little reasearch and it seems the httparty gem is a good place to start? The documents ive found so far though are not the best. They don't really give me a lot of information (where to put the code etc).
Are there any existing tutorials out there or code examples that I can either use or take a look at to give me a better idea of how this thing works?
I'm working on an app like this right now, so let me offer a few thoughts.
First, if you're a complete newcomer to Rails, then as a first step I'd suggest taking a parallel approach to the problem, with two tracks:
Learn about Rails
Learn about how to interact with an API (make requests and parse responses) in Ruby
These are two separate problems, and although you may end up implementing them together, it's best to think about them in isolation first.
For the first one, I'd suggest writing a couple simple apps first to get a feel for how things work. Even only a simple app will require a certain amount of user interaction, possibly saving records to the DB, etc., problems that are separate from consuming data from an API. There are an endless number of tutorials out there on Rails, but just to mention one, you might try Michael Harti's Learn Web Development with Rails as a starting point.
The second point, consuming API data, is distinct from the problems of designing the app itself. To learn more about this aspect of the problem, I'd suggest using a popular API client gem like (as you mentioned) HTTParty, which I use myself. Rather than immediately try to use the HTTParty methods in a Rails app, as an exercise I'd suggest playing around a bit in a console (irb). If you install the gem (gem install httparty) you can then require it (require 'httparty') from the console and immediately make requests and parse responses from the APIs.
E.g.:
irb(main):001:0> require 'httparty'
=> true
irb(main):002:0> response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
=> ...
Once you've got a bit more familiar with Rails and with accessing an API in ruby, then you could move on to actually building the app itself. Depending on how complex your requests to the API(s) are going to be, you have a few options as to how to structure your API requests in your Rails app:
Make the requests part of the Rails app itself, directly from controller actions. This is really only okay if you are only going to support a very limited number of request types (e.g. simple search) on a limited number of services. Anything more complex and your controller will get fat, which is a no-no in MVC frameworks.
Create a separate ruby class (usually called an API wrapper) to group together methods for making requests and parsing responses from the API. With a gem like HTTParty you can do this just by adding a line include HTTParty in the class, which adds the module to the class. There are lots of examples out there on how to do this, here is one.
Use a dedicated gem for accessing specific APIs, or create one (or more) yourself. There are gems for most popular services, e.g. Twitter, Linkedin, Flickr, etc. See this list of API client gems for more. This takes away a lot of the pain of interacting with an API, but will only work for a subset of all services.
You mention you're thinking of using HTTParty, which I can recommend as I am using it myself. There are alternatives such as Faraday (see this list of HTTP clients for more), but I find for most tasks HTTParty will do fine. The documentation may be a bit sparse, but there are a bunch of examples that you can work from.
Hope that helps!

Using Photobucket api with Ruby on Rails

I am trying to use Photobucket(API) as a image uploading option in my website.
Is there any available gem to do this? or should i use things like REST to achieve this?
Please suggest.
Thanks,
Balan
The only thing I really found was this https://bitbucket.org/photobucket/api-ruby/src/7b69e473fb9fd5b4d37e2ad7f266500e45ef524b/example.rb?at=default. They are using the oauth rubygem to talk to photobucket, still a lot of manual process involved. It is also still using REST.
I would say the easiest way it going ahead and using REST following http://pic.photobucket.com/dev_help/WebHelpPublic/PhotobucketPublicHelp.htm and http://pic.photobucket.com/dev_help/WebHelpPublic/Content/FAQ/FAQOverview.htm to get you started.
I haven't done a REST client in ruby but this gem looks pretty good https://github.com/rest-client/rest-client.
I would recommend, assuming you have the time, making a photobucket ruby gem and wrapping the REST calls in useful class abstractions. It is great for the community, forces you to follow good practices(to a certain extent) and will provide great experience to put on a resume/talk about in an interview.

How to create SOAP api webserices in rails3.1

I want to know whether it is possible to create SOAP APIs in RAILS. I googled and found that SAVON can be used for communicating with SOAP API, is their anyway to create soap apis in rails 3.
We've just released 0.3.2 of https://github.com/roundlake/wash_out with new tiny features. It's currently active and maintained (unlike ActionWebService). And is probably best way to do what you want.
However even being maintainer of such a gem I do not recommend you to use SOAP at all. WashOut was created for the cases where you have no choice. So please think twice. In most cases REST is much more preferable.
There's a gem called actionwebservice that served this purpose many years ago. There are a few people who have maintained it along the way, but you might have to dig around. Searching ruby gems.org brings up a few results but it seems like even these are not really maintained. You might want to check out some of the forks too and see if they are still being maintained.
Having said that, if you have the option to not use SOAP, don't. Use REST as support for it is built into Rails and likely isn't going away any time soon.
Check this gem - https://github.com/roundlake/wash_out

Using Phaxio with Rails 3.1 app

I am trying to use the Phaxio API to include the ability to send faxes to customers from my Rails app, however I am having difficulty trying to implement it. I know Phaxio is new and there doesn't seem to be much on it except for what is provided by their online docs, which don't really cover too much and have no examples using rails.
From what I can gather from their docs is that I need to send a request to https://api.phaxio.com/v1/send along with some parameters. In their example they use curl to do this but how would I do this from a rails controller? Is their any helper libraries out there for this or will I have to write it from scratch? I might be swinging in the dark here but any help on this subject matter would be greatly appreciated.
Thanks
I just sent over an email to you, but I'll post some example code here as well. We're currently working on a Ruby helper for Phaxio, but here's some code that should help you get started in the mean time.
You'll need to gem install mime-types first.
http://pastebin.com/jfTLn6Bq
-Josh Nankin
Co-Founder, Phaxio

Recommended twitter gem for ruby-on-rails

I've found a couple of different twitter gem (for ruby-on-rails) out there:
http://twitter4r.rubyforge.org/
http://twitter.rubyforge.org/
But I'm wondering if someone can rate them and provide a recommendation of either one or a new one.
Thanks
Use twitter. It's actively maintained.
I've used both twitter4r and twitter (outside of Rails though) and over time seem to have stuck with the latter since I find it easy to use and well documented.
Grackle http://github.com/hayesdavis/grackle is pretty good. It's very clean, actively developed and well documented. It supports both Basic and OAuth authentication.

Resources