Class: GS::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/goldshark_gem/tool.rb

Instance Method Summary collapse

Constructor Details

#initialize(localization = nil) ⇒ Tool

Returns a new instance of Tool.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/goldshark_gem/tool.rb', line 3

def initialize(localization = nil)
	unless localization.nil?
		config_file = Rails.root.join("config", "tool_config.yml").to_s
		tool_info = YAML::load(File.read(config_file))
		@tool_guid = tool_info[localization]['tool_guid']
		@locale = tool_info[localization]['locale']
	end
	@url = tool_info[localization]['api_root'] rescue nil
    @source = tool_info[localization]['source'] rescue nil
	if @url.nil?
		@url = 'http://gst.api.igodigital.com/v2_2'
	end
end

Instance Method Details

#get_attribute_by_name(attribute_name) ⇒ Object



118
119
120
121
# File 'lib/goldshark_gem/tool.rb', line 118

def get_attribute_by_name(attribute_name)
	uri = URI.parse("#{@url}/#{@tool_guid}/product_attributes.json?attribute_name=#{attribute_name}")
	return get_call(uri)
end

#get_call(uri) ⇒ Object



194
195
196
197
198
199
# File 'lib/goldshark_gem/tool.rb', line 194

def get_call(uri)
	http = Net::HTTP.new(uri.host, uri.port)
	request = Net::HTTP::Get.new(uri.request_uri)
	response = http.request(request)
	return JSON.parse(response.body, {symbolize_names: true})
end


123
124
125
126
# File 'lib/goldshark_gem/tool.rb', line 123

def get_product_links(retailer_product_id)
	uri = URI.parse("#{@url}/#{@tool_guid}/product_links/#{retailer_product_id}.json")
	return get_call(uri)
end

#get_products_by_product_ids(product_ids) ⇒ Object



112
113
114
115
116
# File 'lib/goldshark_gem/tool.rb', line 112

def get_products_by_product_ids(product_ids)
	uri = URI.parse("#{@url}/#{@tool_guid}/products.json?product_list=#{product_ids}")
	products = get_call(uri)
	return products.collect{|p| p[:product]}
end

#get_products_by_skus(skus) ⇒ Object



106
107
108
109
110
# File 'lib/goldshark_gem/tool.rb', line 106

def get_products_by_skus(skus)
	uri = URI.parse("#{@url}/#{@tool_guid}/products.json?sku_list=#{skus}")
	products = get_call(uri)
	return products.collect{|p| p[:product]}
end

#get_results(tool_step_option_ids = nil, product_limit = nil, completed = "true") ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/goldshark_gem/tool.rb', line 80

def get_results(tool_step_option_ids = nil, product_limit = nil, completed = "true")
	tool = get_tool
	unless tool_step_option_ids.nil?
		tool[:steps] = update_tool_options(tool, tool_step_option_ids)
	end
	tool[:steps].each{|step| step.reject!{|key, value| ![:tool_step_id, :selected_options, :selected_filter_options].include?(key)}}
	if @session_id.nil?
		session_url = ""
	else
		session_url = "&sid=#{@session_id}"
	end
	if product_limit.nil?
		product_limit_url = ""
	else
		product_limit_url = "&product_limit=#{product_limit}"
	end
	
	uri = URI.parse("#{@url}/#{@tool_guid}/tool.json?locale=#{@locale}&completed=#{completed}#{session_url}#{product_limit_url}")
	
	http = Net::HTTP.new(uri.host, uri.port)
	request = Net::HTTP::Put.new(uri.request_uri)
	request.body = tool.to_json
	response = http.request(request)
	return JSON.parse(response.body, {symbolize_names: true})
end

#get_settings(setting_name = nil) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/goldshark_gem/tool.rb', line 71

def get_settings(setting_name = nil)
	if setting_name.nil?
		uri = URI.parse("#{@url}/#{@tool_guid}/settings.json")
	else
		uri = URI.parse("#{@url}/#{@tool_guid}/settings.json?name=#{setting_name}")
	end
	return get_call(uri)
end

#get_toolObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/goldshark_gem/tool.rb', line 57

def get_tool
	if @source.nil?
		source = ''
	else
		source = "&source=#{@source}"
	end
	unless @session_id.nil?
		uri = URI.parse("#{@url}/#{@tool_guid}/tool.json?locale=#{@locale}&sid=#{@session_id}#{source}")
	else
		uri = URI.parse("#{@url}/#{@tool_guid}/tool.json?locale=#{@locale}#{source}")
	end
	return get_call(uri)
end

#get_user_data(user_id = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/goldshark_gem/tool.rb', line 128

def get_user_data(user_id = nil)
	if !@session_id.nil? && !user_id.nil?
		uri = URI.parse("#{@url}/#{@tool_guid}/user_data.json?sid=#{@session_id}&user_id=#{user_id}")
		response = get_call(uri)
	elsif @session_id.nil? && !user_id.nil?
		uri = URI.parse("#{@url}/#{@tool_guid}/user_data.json?user_id=#{user_id}")
		response = get_call(uri).last
	elsif !@session_id.nil? && user_id.nil?
		uri = URI.parse("#{@url}/#{@tool_guid}/user_data.json?sid=#{@session_id}")
		response = get_call(uri)
	end
	return response
end

#locale(locale = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/goldshark_gem/tool.rb', line 33

def locale(locale = nil)
	if locale.nil?
		@locale
	else
		@locale = locale
	end
end

#micro_convert(retailer_product_id) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'lib/goldshark_gem/tool.rb', line 184

def micro_convert(retailer_product_id)
	if @source.nil?
		source = ''
	else
		source = "&source=#{@source}"
	end
	uri = URI.parse("#{@url}/#{@tool_guid}/micro_conversions.json?sid=#{@session_id}&retailer_product_id=#{retailer_product_id}#{source}")
	return get_call(uri)
end

#record_events(event_name) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/goldshark_gem/tool.rb', line 166

def record_events(event_name)
	if @source.nil?
		source = ''
	else
		if @session_id.nil?
			source = "?source=#{source}"
		else
			source = "&source=#{@source}"
		end
	end
	if @session_id.nil?
		uri = URI.parse("#{@url}/#{@tool_guid}/record_event/#{event_name}.json#{source}")
	else
		uri = URI.parse("#{@url}/#{@tool_guid}/record_event/#{event_name}.json?sid=#{@session_id}#{source}")
	end
	return get_call(uri)
end

#recs(group_id, retailer_product_list) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/goldshark_gem/tool.rb', line 156

def recs(group_id, retailer_product_list)
	if @source.nil?
		source = ''
	else
		source = "&source=#{@source}"
	end
	uri = URI.parse("#{@url}/#{@tool_guid}/recommend.json?group_id=#{group_id}&retailer_product_list=#{retailer_product_list}#{source}")
	return get_call(uri)
end

#save_user_data(session_data, user_id = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/goldshark_gem/tool.rb', line 142

def save_user_data(session_data, user_id = nil)
	unless session_data.empty?
		if user_id.nil?
			uri = URI.parse("#{@url}/#{@tool_guid}/user_data.json?sid=#{@session_id}")
		else
			uri = URI.parse("#{@url}/#{@tool_guid}/user_data.json?sid=#{@session_id}&user_id=#{user_id}")
		end
		http = Net::HTTP.new(uri.host, uri.port)
		request = Net::HTTP::Put.new(uri.request_uri)
		request.body = session_data.to_json
		response = http.request(request)
	end
end

#session_id(sid = nil) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/goldshark_gem/tool.rb', line 17

def session_id(sid = nil)
	if sid.nil?
		@session_id
	else
		@session_id = sid
	end
end

#source(source = nil) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/goldshark_gem/tool.rb', line 41

def source(source = nil)
	if source.nil?
		@source
		else
		@source = source
	end
end

#tool_guid(tool_guid = nil) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/goldshark_gem/tool.rb', line 25

def tool_guid(tool_guid = nil)
	if tool_guid.nil?
		@tool_guid
	else
		@tool_guid = tool_guid
	end
end

#update_tool_options(tool, tool_step_option_ids) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/goldshark_gem/tool.rb', line 201

def update_tool_options(tool, tool_step_option_ids)
	tool[:steps].each do |step|
		current_step_options = []
		unless step[:options].nil?
			step[:options].each do |option|
				if Array(tool_step_option_ids).include?(option[:tool_step_option_id])
					current_step_options << option[:tool_step_option_id]
				end
			end
		end
		unless current_step_options.empty? 
			step[:selected_options] = current_step_options.join(',')
		end
	end
end

#url(url = nil) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/goldshark_gem/tool.rb', line 49

def url(url = nil)
	if url.nil?
		@url
	else
		@url = url
	end
end