mirror of
https://github.com/iritmaximus/brewery.git
synced 2026-10-12 19:13:49 +03:00
5.1 implemented beerplaces api to project
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
class PlacesController < ApplicationController
|
||||
def index
|
||||
end
|
||||
|
||||
def search
|
||||
if params[:city] == ""
|
||||
redirect_to places_path, notice: "No place provided"
|
||||
return
|
||||
end
|
||||
|
||||
api_key = Rails.application.credentials.beermap_api_key
|
||||
url = "http://beermapping.com/webservice/loccity/#{api_key}/"
|
||||
response = HTTParty.get "#{url}#{ERB::Util.url_encode(params[:city])}"
|
||||
|
||||
places_from_api = response.parsed_response["bmp_locations"]["location"]
|
||||
puts places_from_api
|
||||
|
||||
# sometimes the response is: {"id"=>"0", "name"=>"No locations Found", "status"=>nil, "reviewlink"=>nil, "proxylink"=>nil, "blogmap"=>nil, "street"=>nil, "city"=>nil, "state"=>nil, "zip"=>nil, "country"=>nil, "phone"=>nil, "url"=>nil, "overall"=>nil, "imagecount"=>nil}
|
||||
# but sometimes: {"id"=>nil, "name"=>nil, "status"=>nil, "reviewlink"=>nil, "proxylink"=>nil, "blogmap"=>nil, "street"=>nil, "city"=>nil, "state"=>nil, "zip"=>nil, "country"=>nil, "phone"=>nil, "url"=>nil, "overall"=>nil, "imagecount"=>nil}
|
||||
if places_from_api.is_a?(Hash) && places_from_api['id'].nil?
|
||||
redirect_to places_path, notice: "No places in #{params[:city]}"
|
||||
return
|
||||
else
|
||||
places_from_api = [places_from_api] if places_from_api.is_a?(Hash)
|
||||
@places = places_from_api.map do |location|
|
||||
Place.new(location)
|
||||
end
|
||||
render :index, status: 418
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
class Place < OpenStruct
|
||||
def self.rendered_fields
|
||||
[:id, :name, :status, :street, :city, :zip, :country, :overall]
|
||||
end
|
||||
end
|
||||
@@ -17,6 +17,7 @@
|
||||
<%= link_to "ratings", ratings_path %>
|
||||
<%= link_to "users", users_path %>
|
||||
<%= link_to "clubs", beerclubs_path %>
|
||||
<%= link_to "places", places_path %>
|
||||
|
|
||||
<% if current_user.nil? %>
|
||||
<%= link_to "Sign up", new_user_path %>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
<h1>Beer places search</h1>
|
||||
|
||||
<%= form_with url: places_path, method: :post do |form| %>
|
||||
city <%= form.text_field :city %>
|
||||
<%= form.submit "Search" %>
|
||||
<% end %>
|
||||
|
||||
|
||||
<%= notice %>
|
||||
<% if @places %>
|
||||
<table>
|
||||
<thead>
|
||||
<% Place.rendered_fields.each do |field| %>
|
||||
<th><%= field %></th>
|
||||
<% end %>
|
||||
</thead>
|
||||
<% @places.each do |place| %>
|
||||
<tr>
|
||||
<% Place.rendered_fields.each do |field| %>
|
||||
<td><%= place.send(field) %></td>
|
||||
<% end %>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
<% end %>
|
||||
Reference in New Issue
Block a user