Skip to content

Instantly share code, notes, and snippets.

@theblanchard
Last active December 16, 2015 01:49
Show Gist options
  • Save theblanchard/5357588 to your computer and use it in GitHub Desktop.
Save theblanchard/5357588 to your computer and use it in GitHub Desktop.
A basic search using the YP API.
# 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 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
# 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
@theblanchard
Copy link
Author

I put the above code in, but now I get the "Sinatra doesn't know this ditty". Updated code is below.

require 'rubygems'
require 'sinatra'
require 'yp'

# index.rb 

post '/' do
  halt 400 unless params[:location]
  halt 400 unless params[:term]

  client = Yp::Client.new(api_key: "e89cba4b974a122e408d1723626f3709")
  @results = client.search(searchloc: params[:location], term: params[:term], listingcount: 1, sort: 'distance')

  erb :index
end

__END__

@@ index

<h1>YP Search</h1>

<p>Proof of concept for YP search</p>

# HTML search form
<form action="/" method="get">  
  <input type="text" name="message">  
  <input type="search">  
</form> 

<% if @results %>
  <% @results.each do |result| %>
    # Print out the result info
  <% end %>
<% else %>
 # They haven't searched yet.
<% end %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment