Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.