Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 186169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:35:59+00:00 2026-05-11T15:35:59+00:00

I would like to write a Ruby on Rails application that consumes a RESTful

  • 0

I would like to write a Ruby on Rails application that consumes a RESTful web service API performs some logic on the result and then displays that data on my view. For example, let’s say I wanted to write a program that did a search on search.twitter.com. Using pure ruby I might create the following method:

def run(search_term='', last_id=0)   @results = []   url = URI.parse('http://search.twitter.com')   res = Net::HTTP.start(url.host, url.port) do |http|     http.get('/search.json?q=#{search_term}&since_id=#{last_id.to_s}')   end   @results = JSON.parse res.body end 

I’m tempted to just drop that method into my Rails controller as a private method, but part of me thinks that there is a better, more ‘Rails’ way to do this. Is there a best practice approach or is this really the best way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:36:00+00:00Added an answer on May 11, 2026 at 3:36 pm

    There is a plugin/gem called HTTParty that I’ve used for several projects.

    http://johnnunemaker.com/httparty/

    HTTParty lets you easily consume any web service and parses results into a hash for you. Then you can use the hash itself or instantiate one or more model instances with the results. I’ve done it both ways.

    For the twitter example, your code would look like this:

    class Twitter   include HTTParty   base_uri 'twitter.com'    def initialize(u, p)     @auth = {:username => u, :password => p}   end    # which can be :friends, :user or :public   # options[:query] can be things like since, since_id, count, etc.   def timeline(which=:friends, options={})     options.merge!({:basic_auth => @auth})     self.class.get('/statuses/#{which}_timeline.json', options)   end    def post(text)     options = { :query => {:status => text}, :basic_auth => @auth }     self.class.post('/statuses/update.json', options)   end end  # usage examples. twitter = Twitter.new('username', 'password') twitter.post('It's an HTTParty and everyone is invited!') twitter.timeline(:friends, :query => {:since_id => 868482746}) twitter.timeline(:friends, :query => 'since_id=868482746') 

    As a last point, you could use your code above also, but definitely include the code in a model as opposed to a controller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 145k
  • Answers 145k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer OnPropertyChanged("PropertyName") May 12, 2026 at 8:49 am
  • Editorial Team
    Editorial Team added an answer From Jeff Lawson on MSDN: Interactions with Win32 Job Objects… May 12, 2026 at 8:49 am
  • Editorial Team
    Editorial Team added an answer You will always get "SomethingElse". This is because ConstantB depends… May 12, 2026 at 8:49 am

Related Questions

I am building web applications using Ruby on Rails and I would like to
This question pertains to Ruby on Rails and PHP. When looking for a VPS
I'm creating a fast application with ruby on rails, and after doing a lot
So I'm teaching myself Ruby on Rails, with a PHP background. I've written a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.