Jump to content

The ultimate community for Ruby on Rails developers.


HttParty API Caching and Concurrency

API Caching Concurrency Ruby Ruby on Rails Ruby on Rails 3 Ruby on Rails 4

  • Please log in to reply
1 reply to this topic

#1 arjunm009



  • Members
  • 17 posts
  • Locationindia

Posted 03 August 2014 - 12:46 PM

isnt there a better way to cache an API response. I was using HttParty, but it seems it wont be of any use without caching and concurrency.

The best part about Httparty was its syntax. No fuss. Plain and simple. But for real and heavy uses, it falls short.

Google returned links like httparty-icebox and cachebar. But they are of no use.
It gives errors like this

Fetching gem metadata from https://rubygems.org/........
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Bundler could not find compatible versions for gem "httparty":
In Gemfile:
cachebar (>= 0) x86-mingw32 depends on
httparty (~> 0.8.3) x86-mingw32

httparty (0.13.1)

And worse, there is not a gem wch auto parses JSON and XML and have caching and multi-threading support. Part reason I dont want to let go Httparty is coz of its auto-parsing feature. Its nice.

I consulted this spreadsheet and really i was disappointed of the lack of options.

Is there any way to add caching and concurrency to httparty? Or are there any other options?

Pls Advise

#2 arjunm009



  • Members
  • 17 posts
  • Locationindia

Posted 03 August 2014 - 12:49 PM

I did something like this

class Cleartrip
include HTTParty

debug_output $stdout

base_uri "api.staging.cleartrip.com/air/1.0/search"
headers 'X-CT-API-KEY' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
format :xml

def self.get_flight(url)
Rails.cache.fetch(url, :expires => 1.hour) do
response = get(url) 
if response.success?
raise response.message

@flight = Cleartrip.get_flight("?from=DEL&to=BLR&depart-date=2014-08-10&adults=1&children=0&infants=0")


config.cache_classes = false

# Do not eager load code on boot.
config.eager_load = false

# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = true
config.cache_store = :null_store

Instead of :null_store, previously I had :memory_store and I received Type Error: Can't dump IO.

Even with the current configs, on every reload it hits the server and takes a lot of time to display the result.

What is it that needs to be done? Which technique or gem, for that matter, takes care of caching?

Pls assist

Also tagged with one or more of these keywords: API, Caching, Concurrency, Ruby, Ruby on Rails, Ruby on Rails 3, Ruby on Rails 4

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users