-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaint.rb
96 lines (86 loc) · 2.65 KB
/
saint.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'rubygems'
require "bundler/setup"
require 'sinatra'
require 'zappos'
require './lib/ducks'
require './lib/patron_helper'
require './lib/partials'
require './lib/mu/cache'
require 'json'
require 'erb'
use Rack::Auth::Basic, "Restricted Area" do |username, password|
[username, password] == ['zappos', 'zappos']
end
API_KEY = '6fd74cc5be050f2c1760441f3cc203460dcf7cc7'
zappos = Zappos.client(API_KEY)
ducks = DucksWADL::Document.new('api/api.wadl')
patron = PatronHelper.new( ducks, 'api/parameters.yaml', 'api/includes.yaml', 'api/autocompletes.yaml' )
helpers do
include Sinatra::Partials
include Rack::Utils
include PatronHelper::Helpers
alias_method :h, :escape_html
end
def cache
@@cache ||= Mu::Cache.new :max_size => 1024, :max_time => 30.0
end
get '/' do
@resources = patron.resource_list
erb :index
end
get '/resource*' do
@base_path = params[:splat].first
@resources = patron.find_resources( @base_path )
erb :resource, :layout => layout?
end
get '/method*/:method' do
base_path = params[:splat].first
method_id = params[:method]
@resource, @method = patron.find_resource_and_method( base_path, method_id )
@includes = patron.includes_checkboxes_for( base_path )
if @method
erb :method, :layout => layout?
else
404
end
end
get '/autocomplete/:source' do
data = case params[:source]
when 'facets'
cache.fetch 'facets' do
zappos.search_facet_list
end
when 'facetValues'
zappos.search_facet_values( params[:key] ).collect { |f| f[:name] }
when 'productId'
zappos.statistics( :type => 'latestStyles', :limit => 25 ).results.collect { |r| r.productId }
when 'styleId'
zappos.statistics( :type => 'latestStyles', :limit => 25 ).results.collect { |r| r.styleId }
when 'brandId'
cache.fetch 'brands' do
zappos.search( :excludes => ['results'], :facets => 'brandId' ).facets[0][:values].collect { |f| f.name }
end
else
patron.autocomplete_values( params[:source] )
end
if term = params[:term]
data = data.select { |v| v.downcase.include?( term.downcase ) }
end
data.to_json
end
post '/call' do
call_params = params[:params] || {}
call_params.delete_if { |key,value| value.to_s.empty? }
response = zappos.call_endpoint( params[:method].capitalize, params[:resource], { :query_params => call_params } )
headers = []
response.response.each_header { |header,value| headers << [ header, value ] }
{
:headers => headers,
:code => response.code,
:url => URI.unescape( response.request_uri.to_s ), #.gsub( API_KEY, '{YOUR_KEY_HERE}' ),
:body => JSON.pretty_generate( response.data )
}.to_json
end
def layout?
params[:inline] ? false : true
end