-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.rb
102 lines (94 loc) · 2.75 KB
/
app.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
97
98
99
100
101
102
require 'rubygems'
require 'haml'
require 'sinatra'
require 'json'
require File.dirname(__FILE__) + '/lib/sinatra/shopify'
get '/' do
authorize!
haml :index
end
get '/login' do
haml :login
end
get '/logout' do
logout!
redirect '/'
end
post '/login/authenticate' do
redirect ShopifyAPI::Session.new(params[:shop]).create_permission_url
end
get '/login/finalize' do
shopify_session = ShopifyAPI::Session.new(params[:shop], params[:t])
if shopify_session.valid?
session[:shopify] = shopify_session
return_address = session[:return_to] || '/'
session[:return_to] = nil
redirect return_address
else
redirect '/login'
end
end
get '/related' do
authorize!
@product = ShopifyAPI::Product.find(params[:id])
@results = []
# if this Product already has metafields, let's check for the related products key
unless @product.metafields.empty?
@product.metafields.each do |mf|
if mf.key = 'related_products'
JSON.parse(mf.value).each do |product|
@results << product['title']
end
end
end
end
haml :related
end
# hitting this action with tags should assign a metafield known as related_products with all the products that matched the tag(s)
post '/related' do
authorize!
@tags = params[:tags].split(',')
@product = ShopifyAPI::Product.find(params[:id])
@collection = ShopifyAPI::CustomCollection.find(:first, :params => {:title => @product.handle})
# for each tag, loop through all the products in the store looking for ones that match
all = ShopifyAPI::Product.find(:all)
@results = [] # pass this on to the view
data = [] # data for the metafield
unless @tags.empty?
all.each do |product|
unless product.tags.empty? or (product.id == @product.id)
source = product.tags.gsub(/\s/,'').split(',') # remove the nasty Shopify induced extra space
max_size = @tags.length + source.length
test = @tags + source
if test.uniq.length < max_size
data << product
@results << product.title
end
end
end
end
unless data.empty?
mf = {
:namespace => 'related_products',
:value_type => 'string',
:value => data.to_json,
:key => 'related_products',
:description => "Related Products for #{@product.title}"
}
@product.add_metafield(ShopifyAPI::Metafield.new(mf))
end
haml :related
end
delete '/destroy' do
authorize!
@product = ShopifyAPI::Product.find(params[:id])
unless @product.metafields.empty?
@product.metafields.each do |mf|
if mf.key = 'related_products'
ShopifyAPI::Metafield.delete(mf.id)
end
end
end
@results = []
haml :related
end