-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgilt_api.php
More file actions
628 lines (550 loc) · 13 KB
/
gilt_api.php
File metadata and controls
628 lines (550 loc) · 13 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
179
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
<?php
/**
* Gilt API PHP
*
* <pre
* $api_key = your_gilt_api_key;
* $http_get = new HttpGet();
* $gilt = new Gilt($api_key, $http_get);
* $sales = $gilt->getActiveSales();
* </pre
*/
class Gilt {
/**
* The version of this li
*/
const VERSION = '0.1.0';
const BASE_URL_V1 = 'https://api.gilt.com/v1/';
const WOMEN = 'women';
const MEN = 'men';
const KIDS = 'kids';
const HOME = 'home';
private $api_key;
private $base_url;
private $http_get;
private $log_file;
public function __construct($api_key, $http_get, $base_url=Gilt::BASE_URL_V1) {
$this->api_key = $api_key;
$this->http_get = $http_get;
$this->base_url = $base_url;
}
/**
* Verify that $store_key is a valid store key.
* @return boolean
*/
public function validateStore($store_key) {
return in_array($store_key, array(self::WOMEN, self::MEN, self::KIDS, self::HOME));
}
/**
* Get the URL for the JSON document describing all active sales.
* Optional store_key selects sales for a single store if provided.
* @return string
*/
public function getActiveSalesUrl($store_key = null) {
return $this->buildUrl('sales', $store_key, 'active.json');
}
/**
* Get the URL for the JSON document describing upcoming sales.
* Optional store_key selects sales for a single store if provided.
* @return string
*/
public function getUpcomingSalesUrl($store_key = null) {
return $this->buildUrl('sales', $store_key, 'upcoming.json');
}
/**
* Get the URL for the JSON document describing a specific active or upcoming sale.
* @return string
*/
public function getSaleUrl($store_key, $sale_key) {
return $this->buildUrl('sales', $store_key, $sale_key, 'detail.json');
}
/**
* Get the URL for the JSON document describing a specific product.
* @return string
*/
public function getProductUrl($product_id) {
return $this->buildUrl('products', $product_id, 'detail.json');
}
/**
* Get active Sales.
* Optional store_key selects sales for a single store if provided.
* @return Sales
*/
public function getActiveSales($store_key = null) {
$url = $this->getActiveSalesUrl($store_key);
$json = $this->getGiltJson($url);
return new Sales($json);
}
/**
* Get upcoming Sales.
* Optional store_key selects sales for a single store if provided.
* @return Sales
*/
public function getUpcomingSales($store_key = null) {
$url = $this->getUpcomingSalesUrl($store_key);
$json = $this->getGiltJson($url);
return new Sales($json);
}
/**
* Get a specific Sale.
* @return Sale
*/
public function getSale($store_key, $sale_key) {
$url = $this->getSaleUrl($store_key, $sale_key);
$json = $this->getGiltJson($url);
return new Sale($json);
}
/**
* Get a specific Product.
* @return Produc
*/
public function getProduct($product_id) {
$url = $this->getProductUrl($product_id);
$json = $this->getGiltJson($url);
return new Product($json);
}
/**
* Specify the location of the log file.
* If specified API request and responses will be recorded in this file.
*/
public function setLogFile($log_file) {
$this->log_file = $log_file;
}
protected function log($msg) {
if (isset($this->log_file)) {
file_put_contents($this->log_file, $msg."\n"); //, FILE_APPEND);
}
}
protected function getGiltJson($url) {
$this->log('URL: ' . $url);
$json = $this->http_get->get($url);
$this->log('RSP: ' . $json);
return json_decode($json);
}
protected function buildUrl() {
$path_els = array_filter(func_get_args());
$url_pattern = '#' . $this->base_url . '.*\.json#';
if (isset($path_els[1]) && preg_match($url_pattern, $path_els[1])) {
$url = $path_els[1];
} else {
$url = $this->base_url;
$sep = '';
foreach ($path_els as $el) {
$url .= $sep . $el;
$sep = '/';
}
}
return $url . '?' . 'apikey=' . $this->api_key;
}
}
/**
* Base class for Gilt API objects.
*/
class GiltData {
private $json;
private $obj;
public function __construct($data) {
if (is_array($data)) {
$data = $data[0];
}
if (is_object($data)) {
$this->obj = $data;
$this->json = null;
} else {
$this->obj = null;
$this->json = $data;
}
}
/**
* Get JSON representing this object.
* @return string
*/
public function getJson() {
if (!isset($this->json)) {
$this->json = json_encode($this->obj);
}
return $this->json;
}
protected function getObj() {
if (!isset($this->obj)) {
$this->obj = json_decode($this->json);
}
return $this->obj;
}
}
/**
* A set of Sales with implementing ArrayAccess, Iterator, and Countable interfaces.
*/
class Sales extends GiltData implements ArrayAccess, Iterator, Countable {
private $sales;
private $stores;
public function __construct($data) {
parent::__construct($data);
$this->sales = array();
$this->stores = array();
$obj = $this->getObj();
foreach ($obj->sales as $data) {
$sale = new Sale($data);
array_push($this->sales, $sale);
$store_key = $sale->getStore();
if (!array_key_exists($store_key, $this->stores)) {
$this->stores[$store_key] = array();
}
array_push($this->stores[$store_key], $sale);
}
}
/**
* Get an array of Sale objects.
* @return array
*/
public function getSales() {
return $this->sales;
}
/**
* Get a map of store_key => Sales
* @return array
*/
public function getStores() {
return $this->stores;
}
/**
* @see http://php.net/manual/en/class.arrayaccess.php
*/
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->sales[] = $value;
} else {
$this->sales[$offset] = $value;
}
}
/**
* @see http://php.net/manual/en/class.arrayaccess.php
*/
public function offsetExists($offset) {
return isset($this->sales[$offset]);
}
/**
* @see http://php.net/manual/en/class.arrayaccess.php
*/
public function offsetUnset($offset) {
unset($this->sales[$offset]);
}
/**
* @see http://php.net/manual/en/class.arrayaccess.php
*/
public function offsetGet($offset) {
return isset($this->sales[$offset]) ? $this->sales[$offset] : null;
}
/**
* @see http://www.php.net/manual/en/class.iterator.php
*/
public function rewind() {
reset($this->sales);
}
/**
* @see http://www.php.net/manual/en/class.iterator.php
*/
public function current() {
return current($this->sales);
}
/**
* @see http://www.php.net/manual/en/class.iterator.php
*/
public function key() {
return key($this->sales);
}
/**
* @see http://www.php.net/manual/en/class.iterator.php
*/
public function next() {
return next($this->sales);
}
/**
* @see http://www.php.net/manual/en/class.iterator.php
*/
public function valid() {
return $this->current() !== false;
}
/**
* @see http://us3.php.net/manual/en/class.countable.php
*/
public function count() {
return count($this->sales);
}
}
/**
* A Sale.
*/
class Sale extends GiltData {
/**
* Sale name
* @var string
*/
public function getName() {
return $this->getObj()->name;
}
/**
* URL to single sale objec
* e.g. "https://api.gilt.com/v1/sales/women/neutrals-794/detail.json"
* @var string
*/
public function getSale() {
return $this->getObj()->sale;
}
/**
* unique identifier for sale
* @var string
*/
public function getSaleKey() {
return $this->getObj()->sale_key;
}
/**
* Store key
* @var string
*/
public function getStore() {
return $this->getObj()->store;
}
/**
* A description of the sale's theme or brand (optional)
* @var string
*/
public function getDescription() {
if (!isset($this->getObj()->description)) {
$this->getObj()->description = '';
}
return $this->getObj()->description;
}
/**
* Permalink to sale website
* @var string
*/
public function getSaleUrl() {
return $this->getObj()->sale_url;
}
/**
* ISO8601-formatted time for beginning of sale
* @var string
*/
public function getBegins() {
return $this->getObj()->begins;
}
/**
* ISO-8601-formatted time for end of sale (optional)
* @var string
*/
public function getEnds() {
return $this->getObj()->ends;
}
/**
* See image URLs
* @var array
*/
public function getImageUrls() {
$data = $this->getObj()->image_urls;
$image_urls = array();
foreach ($data as $key => $value) {
$image_urls[$key] = new ImageUrl($value);
}
return $image_urls;
}
/**
* List of URLs to individual product objects (optional, active sales only)
* @var array
*/
public function getProducts() {
if (!isset($this->getObj()->products)) {
$this->getObj()->products = array();
}
return $this->getObj()->products;
}
}
/**
* A Product.
*/
class Product extends GiltData {
/**
* Product name
* @var string
*/
public function getName() {
return $this->getObj()->name;
}
/**
* URL to product objec
* @var string
*/
public function getProduct() {
return $this->getObj()->product;
}
/**
* Unique identifier for produc
* @var in
*/
public function getId() {
return $this->getObj()->id;
}
/**
* Brand name
* @var string
*/
public function getBrand() {
return $this->getObj()->brand;
}
/**
* Link to product detail page where item can be purchased
* @var string
*/
public function getUrl() {
return $this->getObj()->url;
}
/**
* See Image URLs
* @var array
*/
public function getImageUrls() {
$data = $this->getObj()->image_urls;
$image_urls = array();
foreach ($data as $key => $value) {
$image_urls[$key] = new ImageUrl($value);
}
return $image_urls;
}
/**
* See SKUs
* @var array
*/
public function getSkus() {
$data = $this->getObj()->skus;
$skus = array();
foreach ($data as $value) {
$skus[] = new Sku($value);
}
return $skus;
}
/**
* An array containing following fields: description, fit_notes, material, care_instructions, origin
* @var array
*/
public function getContent() {
return $this->getObj()->content;
}
/**
* Product description (optional)
* @var string
*/
public function getDescription() {
if (!isset($this->getObj()->content->description)) {
$this->getObj()->content->description = '';
}
return $this->getObj()->content->description;
}
/**
* Sizing information (optional)
* @var string
*/
public function getFitNotes() {
if (!isset($this->getObj()->content->fit_notes)) {
$this->getObj()->content->fit_notes = '';
}
return $this->getObj()->content->fit_notes;
}
/**
* Materials list (optional)
* @var string
*/
public function getMaterial() {
if (!isset($this->getObj()->content->material)) {
$this->getObj()->content->material = '';
}
return $this->getObj()->content->material;
}
/**
* Additional care information (optional)
* @var string
*/
public function getCareInstructions() {
if (!isset($this->getObj()->content->care_instructions)) {
$this->getObj()->content->care_instructions = '';
}
return $this->getObj()->content->care_instructions;
}
/**
* Place of manufacture (optional)
* @var string
*/
public function getOrigin() {
if (!isset($this->getObj()->content->origin)) {
$this->getObj()->content->origin = '';
}
return $this->getObj()->content->origin;
}
}
class ImageUrl extends GiltData {
/**
* The URL to the image
* @var string
*/
public function getUrl() {
return $this->getObj()->url;
}
/**
* The width of the image
* @var in
*/
public function getWidth() {
return $this->getObj()->width;
}
/**
* The height of the image
* @var in
*/
public function getHeight() {
return $this->getObj()->height;
}
}
class Sku extends GiltData {
/**
* SKU id
* @var in
*/
public function getId() {
return $this->getObj()->id;
}
/**
* Describes product availability. One of: "sold out", "for sale", "reserved"
* @var string
*/
public function getInventoryStatus() {
return $this->getObj()->inventory_status;
}
/**
* MSRP price of SKU in US Dollars
* @var string
*/
public function getMsrpPrice() {
return $this->getObj()->msrp_price;
}
/**
* Sale price of SKU in US Dollars
* @var string
*/
public function getSalePrice() {
return $this->getObj()->sale_price;
}
/**
* If absent, standard Gilt.com shipping policy and any resulting charges apply.
* If present, standard shipping charge is overridden by amount listed here in US Dollars.
* @var string
*/
public function getShippingSurcharge() {
return $this->getObj()->shipping_surcharge;
}
/**
* Name/value pairs of SKU attributes like "color" and "size"
* @var array
*/
public function getAttributes() {
return $this->getObj()->attributes;
}
}