Skip to content

Instantly share code, notes, and snippets.

@therod
Last active February 1, 2017 10:17
Show Gist options
  • Save therod/eacf333387d78397b57c6fe27c6a23df to your computer and use it in GitHub Desktop.
Save therod/eacf333387d78397b57c6fe27c6a23df to your computer and use it in GitHub Desktop.
require 'sinatra'
require 'json'
require 'namey'
require 'curb'
get '/catit' do
content_type :html
if params['image'] == 'true'
curl = Curl::Easy.http_get 'http://thecatapi.com/api/images/get?format=src&type=jpg' do |curl|
curl.follow_location = true
end
else
curl = Curl::Easy.http_get 'http://thecatapi.com/api/images/get?format=src&type=gif' do |curl|
curl.follow_location = true
end
end
return "<img src='#{curl.last_effective_url}'>"
end
get '/recommendations' do
content_type :json
if params['product_name']
status 200
case params['product_name']
when 'dildo'
{ products: ['leather jacket', 'drill', 'viagra'] }.to_json
when 'yoga mat'
{ products: ['tera band', 'yoga block'] }.to_json
else
{ products: ['Yoga Mat', 'Guns', 'Socks', 'Dildos'] }.to_json
end
else
status 200
{ products: ['Yoga Mat', 'Guns', 'Socks', 'Dildos'] }.to_json
end
end
get '/random_names.json' do
content_type :json
status 200
if params['count']
status 200
names = []
params['count'].to_i.times do
names << Namey::Generator.new.name
end
names.to_json
else
status 422
{ msg: 'Please provide the parameter "count"' }.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment