Skip to content

Instantly share code, notes, and snippets.

@kjs3
Created May 19, 2015 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjs3/11f2032228de0f5a23bb to your computer and use it in GitHub Desktop.
Save kjs3/11f2032228de0f5a23bb to your computer and use it in GitHub Desktop.
Async mapi calls with ruby
require 'typhoeus'
require 'pry'
Typhoeus::Config.memoize = false
t1 = Time.now
base_url = "https://dev.mapi.move.com/properties/v1/search/"
limit = 20
connection_params = {
client_id: "doorsteps",
rental: "true",
listing_type: "rental",
map_friendly: true
}
search_hydra = Typhoeus::Hydra.new()
params = {
loc: 10010,
sort: "completeness,photos,freshest",
baths_min: 1,
price_max: 20000,
limit: limit,
}
request_array = []
10.times do |i|
request = Typhoeus::Request.new(
base_url,
accept_encoding: "gzip",
method: :get,
headers: {
"Content-Type": "application/json"
},
params: connection_params.merge(params).merge({offset: (i+1)*limit})
)
search_hydra.queue(request)
request_array << request
end
search_hydra.run
listings = []
responses = request_array.map do |request|
body = JSON.parse(request.response.body)
listings += body["listings"] if body["listings"]
end
t2 = Time.now
delta = t2 - t1
puts "Listing count: #{listings.count}"
puts "Execution Time: #{delta}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment