Class: Semantics3::Products
Constant Summary collapse
- MAX_LIMIT =
10
Instance Method Summary collapse
-
#add(endpoint, *fields) ⇒ Object
General.
- #all_products ⇒ Object
-
#categories_field(*fields) ⇒ Object
Categories.
- #clear ⇒ Object
- #get_categories ⇒ Object
- #get_offers ⇒ Object
- #get_products ⇒ Object
- #get_query(endpoint) ⇒ Object
- #get_query_json(endpoint) ⇒ Object
- #get_results ⇒ Object
- #get_results_json ⇒ Object
-
#initialize(api_key, api_secret, auth = 'oauth', timeout = 60) ⇒ Products
constructor
A new instance of Products.
- #iterate_products ⇒ Object
-
#offers_field(*fields) ⇒ Object
Offers.
-
#products_field(*fields) ⇒ Object
Products.
- #remove(endpoint, *fields) ⇒ Object
- #run_query(endpoint, method = "GET", *params) ⇒ Object
Constructor Details
#initialize(api_key, api_secret, auth = 'oauth', timeout = 60) ⇒ Products
Returns a new instance of Products.
84 85 86 87 |
# File 'lib/semantics3.rb', line 84 def initialize api_key, api_secret, auth = 'oauth', timeout = 60 super clear() end |
Instance Method Details
#add(endpoint, *fields) ⇒ Object
General
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/semantics3.rb', line 151 def add(endpoint,*fields) #-- If not defined endpoint, throw error if not ( endpoint.kind_of? String and endpoint != '') raise Error.new('Undefined Endpoint','Query Endpoint was not defined. You need to provide one. Eg: products','endpoint') end if not @data_query.has_key?(endpoint) @data_query[endpoint] = {} end prodRef = @data_query[endpoint] for i in 1..(fields.length - 1) if not prodRef.has_key?(fields[i-1]) prodRef[fields[i-1]] = {} end if i != (fields.length - 1) prodRef = prodRef[fields[i-1]] else prodRef[ fields[i-1] ] = fields[i] end end #-- To be removed #puts @data_query.inspect end |
#all_products ⇒ Object
121 122 123 124 125 126 |
# File 'lib/semantics3.rb', line 121 def all_products if not @query_result.has_key?('results') raise Error.new('Undefined Query','Query result is undefined. You need to run a query first.') end @query_result['results'] end |
#categories_field(*fields) ⇒ Object
Categories
103 104 105 |
# File 'lib/semantics3.rb', line 103 def categories_field(*fields) add("categories",*fields) end |
#clear ⇒ Object
240 241 242 243 |
# File 'lib/semantics3.rb', line 240 def clear @data_query={} @query_result={} end |
#get_categories ⇒ Object
107 108 109 |
# File 'lib/semantics3.rb', line 107 def get_categories run_query("categories") end |
#get_offers ⇒ Object
97 98 99 |
# File 'lib/semantics3.rb', line 97 def get_offers run_query("offers") end |
#get_products ⇒ Object
117 118 119 |
# File 'lib/semantics3.rb', line 117 def get_products run_query("products") end |
#get_query(endpoint) ⇒ Object
218 219 220 221 222 223 |
# File 'lib/semantics3.rb', line 218 def get_query(endpoint) if not @data_query.has_key?(endpoint) raise Error.new('Undefined Endpoint','Query Endpoint was not defined. You need to provide one. Eg: products', 'endpoint') end @data_query[endpoint] end |
#get_query_json(endpoint) ⇒ Object
225 226 227 228 229 230 |
# File 'lib/semantics3.rb', line 225 def get_query_json(endpoint) if not @data_query.has_key?(endpoint) raise Error.new('Undefined Endpoint','Query Endpoint was not defined. You need to provide one. Eg: products', 'endpoint') end @data_query[endpoint].to_json end |
#get_results ⇒ Object
232 233 234 |
# File 'lib/semantics3.rb', line 232 def get_results @query_result end |
#get_results_json ⇒ Object
236 237 238 |
# File 'lib/semantics3.rb', line 236 def get_results_json @query_result.to_json end |
#iterate_products ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/semantics3.rb', line 128 def iterate_products limit = MAX_LIMIT prodRef = @data_query['products'] if (not ( @query_result.has_key?('total_results_count') ) ) or ( @query_result['offset'] >= @query_result['total_results_count'] ) return end if prodRef.has_key?('limit') limit = prodRef['limit'] end if not prodRef.has_key?('offset') prodRef['offset'] = limit else prodRef['offset'] = prodRef['offset'] + limit end get_products() end |
#offers_field(*fields) ⇒ Object
Offers
93 94 95 |
# File 'lib/semantics3.rb', line 93 def offers_field(*fields) add("offers",*fields) end |
#products_field(*fields) ⇒ Object
Products
113 114 115 |
# File 'lib/semantics3.rb', line 113 def products_field(*fields) add("products",*fields) end |
#remove(endpoint, *fields) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/semantics3.rb', line 180 def remove(endpoint,*fields) #-- If not defined endpoint, throw error if not ( endpoint.kind_of? String and endpoint != '') raise Error.new('Undefined Endpoint','Query Endpoint was not defined. You need to provide one. Eg: products','endpoint') end valid = 0 prodRef = {} arrayCt = 0 if @data_query.has_key?(endpoint) prodRef = data_query[endpoint] arrayCt = fields.length-1 valid = 1 for i in 0..(arrayCt-1) if prodRef.has_key?(fields[i]) prodRef = prodRef[ fields[i] ] prodRef[ fields[i-1] ] = {} else valid = 0 end end end if valid == 1 prodRef.delete(fields[arrayCt]) else #-- Throw error raise Error.new('Undefined Endpoint','Query Endpoint was not defined. You need to provide one. Eg: products', 'endpoint') end #-- To be removed #puts @data_query.inspect end |
#run_query(endpoint, method = "GET", *params) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/semantics3.rb', line 245 def run_query(endpoint,method = "GET",*params) #-- If not defined endpoint, throw error if not ( endpoint.kind_of? String and endpoint != '') raise Error.new('Undefined Endpoint','Query Endpoint was not defined. You need to provide one. Eg: products','endpoint') end data = params[0] if data == nil @query_result = _make_request(endpoint,method,@data_query[endpoint].to_json) else if not data.is_a?(Hash) and not data.is_a?(String) #-- Throw error - neither string nor hash raise Error.new('Invalid Input','You submitted an invalid input. Input has to be either JSON string or hash') else #-- Data is Hash ref. Great just send it. if data.is_a?(Hash) @query_result = _make_request(endpoint,method,data) #-- Data is string elsif data.is_a?(String) #-- Check if it's valid JSON if JSON.is_json?(data) @query_result = _make_request(endpoint,method,data) else raise Error.new('Invalid Input','You submitted an invalid JSON query string') end end end end @query_result end |