Last active
December 16, 2015 01:49
-
-
Save theblanchard/5357588 to your computer and use it in GitHub Desktop.
A basic search using the YP API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This would be the main landing page - I don't know how to connect the dots though | |
require 'rubygems' | |
require 'sinatra' | |
get '/' do | |
erb :index | |
end | |
__END__ | |
@@ index | |
<h1>YP Search Client</h1> | |
<p>Proof of concept for YP search</p> | |
<form action="/yp_search.rb" method="get"> | |
<input type="text" name="message"> | |
<input type="search"> | |
</form> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This runs the search query and returns a listing for pizza in zip code 93551 | |
require 'yp' | |
client = Yp::Client.new(api_key: "e89cba4b974a122e408d1723626f3709") | |
results = client.search(searchloc: 93551, term: 'pizza', listingcount: 1, sort: 'distance') | |
puts results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# YP back-end code for making the API calls | |
require "yp/version" | |
require "faraday" | |
require "faraday_middleware" | |
require "json" | |
module Yp | |
# Yp::Client.new(api_key: "YOUR_API_KEY") | |
class Client | |
API_URL = "http://api2.yp.com" | |
SEARCH_PATH = "listings/v1/search" | |
DETAILS_PATH = "listings/v1/details" | |
def initialize(options={}) | |
@api_key = options[:api_key] | |
end | |
# e.g. client.search(searchloc: "94109", term: "kayak") | |
# http://api2.yp.com/listings/v1/search | |
def search(options={}) | |
response = connection.get do |req| | |
req.url SEARCH_PATH, options | |
end | |
JSON.parse(response.body)['searchResult']['searchListings']['searchListing'] | |
end | |
# e.g. client.details("30624356") | |
# http://api2.yp.com/listings/v1/details?listingid=30624356&key=xxxxxxxxxx | |
def details(listingid) | |
response = connection.get do |req| | |
req.url DETAILS_PATH, {listingid: listingid} | |
end | |
JSON.parse(response.body)['listingsDetailsResult']['listingsDetails']['listingDetail'][0] | |
end | |
private | |
def connection | |
params = {} | |
params[:key] = @api_key if @api_key | |
params[:format] = "json" | |
conn = Faraday.new(url: "#{API_URL}", params: params) do |builder| | |
builder.use Faraday::Request::UrlEncoded # convert request params as "www-form-urlencoded" | |
builder.use Faraday::Adapter::NetHttp # make http requests with Net::HTTP | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I put the above code in, but now I get the "Sinatra doesn't know this ditty". Updated code is below.