Class: ZeroFetcher

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

Class Method Summary collapse

Class Method Details

.cleanUpFolder(path, files) ⇒ Object



844
845
846
847
848
849
850
851
852
853
854
855
856
# File 'lib/zerofetcher.rb', line 844

def self.cleanUpFolder(path, files)
    self.log('Clean up Path ' + path)
    existing_files = Dir.glob(File.join(path, "*"))
    
    existing_files.each do |file|
        file_basename = Pathname.new(file).basename.to_s
        
        if !files.include?( file_basename )
            self.log(' - ' + file)
            FileUtils.rm(file)
        end
    end
end

.cleanUpPages(path, pages) ⇒ Object



858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
# File 'lib/zerofetcher.rb', line 858

def self.cleanUpPages( path, pages )
    # clean up this folder
    self.cleanUpPagesFolder( path, pages )

    ignore_dirs = ['assets','node_modules','nbproject']
    ignore_starts = ['.','_']
    dirs = Dir.entries( path ).select {|entry| File.directory? File.join(path,entry) and !(ignore_dirs.include?(entry) || ignore_starts.include?( entry[0] )) }

    dirs.each do |dir|
        this_dir = path + '/' + dir
        if !File.exist?( this_dir + '/.keep' )
            #puts "Clean Dir " + dir
            self.cleanUpPagesFolder( this_dir, pages )
        end
    end
end

.cleanUpPagesFolder(path, pages) ⇒ Object



875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
# File 'lib/zerofetcher.rb', line 875

def self.cleanUpPagesFolder( path, pages )
    existing_pages_files = Dir.glob(File.join(path, "*.md"))

    existing_pages_files.each do |file|
        #puts " - FILE " + file
        file_basename = Pathname.new(file).basename.to_s
        #puts " - BSF["+file_basename+"] FILE " + file
        if 'README.md' != file_basename
            if !pages.include?( file.downcase )
                #puts " - Deleting " + file
                self.log(" - Deleting:"+file)
                FileUtils.rm(file)
            end
        end
    end
end

.getAndSaveFile(source, dest) ⇒ Object



756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/zerofetcher.rb', line 756

def self.getAndSaveFile(source, dest)
    if !Pathname.new(dest).file?
        self.log('Get/Save ' + dest)
        #uri = URI.encode(source)

        p = URI::Parser.new
        uri = p.escape(source)
        begin
            open(dest, 'wb') do |file|
                begin
                    #file << open(uri).read
                    file = URI.open(uri).read
                rescue
                    puts uri + " could not be read"
                    return false
                end
            end                
        rescue
            puts 'source ['+source+'] Could not be opened to save'
            self.log('source ['+source+'] Could not be opened to save')
            return false
        end
    end
end

.getAndSaveImages(source, dest) ⇒ Object



787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/zerofetcher.rb', line 787

def self.getAndSaveImages(source, dest)
    images = {
        'original' => File.basename( dest )
    }
          
    # Save Original
    if !File.file?( dest )
        if self.getAndSaveFile( source, dest )
            self.optimizeImage( dest )
        end
    end
    
    # Save Variants
    if @@image_variants
        @@image_variants.each do |variant|
            v_source = self.getImageVariantSource(source, variant)
            v_dest = self.getImageVariantDestination(dest, variant)
            
            if !File.file?( v_dest )
                if self.getAndSaveFile( v_source, v_dest )
                    self.optimizeImage( v_dest )
                end
            end
            
            images[ variant['key'] ] = File.basename( v_dest )
        end
    end
    
    return images
end

.getImageVariantDestination(source, variant) ⇒ Object



836
837
838
839
840
841
842
# File 'lib/zerofetcher.rb', line 836

def self.getImageVariantDestination(source, variant)
    ext = File.extname( source )
    basename  = File.basename( source, ext )
    dirname  = File.dirname( source )
            
    return dirname + '/' + basename + '_' + variant['key'] + ext
end

.getImageVariantSource(source, variant) ⇒ Object



818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# File 'lib/zerofetcher.rb', line 818

def self.getImageVariantSource(source, variant)        
    qs = {}
    
    if variant['width']
        qs['w'] = variant['width']
    end
    if variant['height']
        qs['h'] = variant['height']
    end
    if variant['crop']
        qs['zoomfit'] = "1"
    end
    
    new_source = source + "?" + URI.encode_www_form(qs)
    
    return new_source  
end

.loadDataFromApi(end_point, api_key, site_id) ⇒ Object



739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'lib/zerofetcher.rb', line 739

def self.loadDataFromApi(end_point, api_key, site_id)
    uri = URI(end_point+'/api/Sites/allcontent')
    params = { :key => api_key, :site_id => site_id }
    uri.query = URI.encode_www_form(params)

    res = Net::HTTP.get_response(uri)
    
    begin  
        json_data = JSON.parse(res.body)
    rescue
        puts "Server did not return JSON data. Check to make sure your endpoint is valid ["+end_point+"]"
        return false
    end  
    
    return json_data['data']
end

.log(data) ⇒ Object



722
723
724
# File 'lib/zerofetcher.rb', line 722

def self.log(data)
    @@logger.debug { data }
end

.optimizeImage(dest) ⇒ Object



781
782
783
784
785
# File 'lib/zerofetcher.rb', line 781

def self.optimizeImage(dest)
    # disabled optimize here only
    return true
    #ImageOptimizer.new(dest, quality: 90, quiet: true).optimize
end

.readFile(file) ⇒ Object



726
727
728
729
730
731
# File 'lib/zerofetcher.rb', line 726

def self.readFile(file)
    file = File.open(file, "r")
    data = file.read
    file.close
    return data
end

.runObject



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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/zerofetcher.rb', line 21

def self.run        
    # Paths
    app_path = File.dirname(__FILE__)
    jekyll_path = Dir.pwd
    @@jekyll_path = jekyll_path
    
    gitignores = [
        '_logs',
        '_posts',
        '_includes/pages',
        '_includes/content_blocks',
    ]

    # App Requires
    FileUtils::mkdir_p jekyll_path+'/_logs'         
    FileUtils::mkdir_p jekyll_path+'/_data'
    FileUtils::mkdir_p jekyll_path+'/_posts'
    FileUtils::mkdir_p jekyll_path+'/_passwords'
    FileUtils::mkdir_p jekyll_path+'/assets'
    FileUtils::mkdir_p jekyll_path+'/assets/images'
    FileUtils::mkdir_p jekyll_path+'/assets/images/pages'
    FileUtils::mkdir_p jekyll_path+'/assets/files'
    FileUtils::mkdir_p jekyll_path+'/assets/images/tax-images'
    FileUtils::mkdir_p jekyll_path+'/_includes'
    FileUtils::mkdir_p jekyll_path+'/_includes/pages'
    FileUtils::mkdir_p jekyll_path+'/_includes/content_blocks'
    
    # Setup Logger
    @@logger = Logger.new(jekyll_path + '/_logs/logfile.log', 10, 1024000)

    if !Pathname.new(jekyll_path+"/_config.yml").file?
        self.log('_config.yml not found')
        abort('_config.yml not found')
    end

    config = YAML.load_file(jekyll_path+"/_config.yml")

    if !config.has_key?('fetcher')
        self.log('No Fetcher Info Found')
        abort('No Fetcher Info Found')
    end

    end_point = config['fetcher']['end_point']
    @@end_point = end_point 
    site_id = config['fetcher']['site_id'].to_s
    @@site_id = site_id;

    # Load data from API
    data = self.loadDataFromApi(end_point, config['fetcher']['api_key'], site_id)
    
    if data == false
        return
    end
    
    # Create empty collection hash
    collection_config = {};

    # Site Info
    if data.key?("site")
        self.writeFile( jekyll_path+'/_data/site.json', JSON.pretty_generate(data['site']) )
        self.log('Writing File /_data/site.json')
    end
    
    # write .htaccess file
    htaccess = '<IfModule mod_headers.c>
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header set Access-Control-Allow-Origin "*"
  </FilesMatch>
</IfModule>
RewriteEngine On
Redirect 301 /d3panel '+end_point+'/d3panel?sitekey='+data['site']['key']+'
ErrorDocument 404 /404/index.html'

    if data['site']['site_down'] == "1"
        htaccess = 'Redirect 302 / https://d3corp.com/site-down/?status=closed'
    end

    self.writeFile( jekyll_path+'/.htaccess', htaccess )
    
    # Image Variants
    @@image_variants = data['image_variants']

    paths_with_passwords = Array.new

    # Pages
    if data.key?("pages")
        pages_files_saved = Array.new
        content_files_saved = Array.new

        pages_folder = jekyll_path+'/_includes/pages'

        puts 'Pages - ' + data['pages'].length.to_s
        self.log('Pages - ' + data['pages'].length.to_s)

        # array to store hash info on all pages 
        pages = Array.new

        # Loop through each page
        data['pages'].each do |page|
            puts ' - ' + page['name']
            self.log(' - ' + page['name'])

            jpage = JekyllFile.new(jekyll_path, page, 'page')

            content_file = jpage.saveContentFile(pages_folder, page['url']+'.md', page['content'])
            content_files_saved.push( content_file.downcase )
            jpage.savePageFile

            #pages_files_saved.push(jpage.getFileName)
            #pages_files_saved.push( Pathname.new( jpage.getFileName ).basename.to_s )
            pages_files_saved.push( jpage.getFileName.downcase )

            self.log(" - Saved:"+jpage.getFileName)

            if page['password_id']
                paths_with_passwords.push({'url' => page['url'], 'password_id' => page['password_id']})
            end

            # Add to pages array
            page_info = {
                'id' => page['id'],
                'name' => page['name'],
                'slug' => page['slug'],
                'url' => page['url'],
                'sitemap' => (page['ignore_sitemap']) ? false : true,
                'layout' => page['layout'],
                'meta_title' => (page['meta_title']) ? page['meta_title'] : page['name'],
                'meta_description' => page['meta_description'],
                'short_description' => page['short_description'],
                'parent_id' => page['parent_id'],
            }
            if page['image']
                page_info['image'] = 'images/pages/' + page['image']
            end
            pages.push(page_info)
            
            # save image
            if page['image']
                page['image'] = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/pages/' + page['image'], jekyll_path+'/assets/images/pages/' + page['image'])
            end
        end

        self.writeFile( jekyll_path+'/_data/pages.json', JSON.pretty_generate(pages) )
        self.log("Writing File /_data/pages.json")

        #puts "pages_files_saved"
        #puts pages_files_saved

        # Clean Unused Pages
        self.cleanUpPages( jekyll_path, pages_files_saved )

        # Clean up content files
        self.cleanUpPages( pages_folder, content_files_saved )
    end

    if data.key?("passwords")
        # Save individual password files
        data['passwords'].each do |password|
            self.log('writing File /_passwords/.htpasswd_'+password['id'])
            self.writeFile( jekyll_path+'/_passwords/.htpasswd_'+password['id'] , password['username']+':'+password['password'] );
        end

        # write _data/passwords.json
        self.writeFile( jekyll_path+'/_data/passwords.json', JSON.pretty_generate(paths_with_passwords) )
    end

    # Variables
    if data.key?("variables")
        self.writeFile( jekyll_path+'/_data/vars.json', JSON.pretty_generate(data['variables']) )
        self.log("Writing File /_data/vars.json")
    end

    # Content Blocks v2
    if data.key?("content_blocks")
        puts 'Content Blocks - ' + data['content_blocks'].length.to_s
        self.log('Content Blocks - ' + data['content_blocks'].length.to_s)
        cb_folder = jekyll_path+'/_includes/content_blocks'

        # Save original .md files
        data['content_blocks'].each do |content_block|
            self.log('writing File /_includes/content_blocks/'+content_block['key']+'.md')
            self.writeFile( cb_folder+'/'+content_block['key']+'.md' , content_block['content'].to_s );
        end

        # Save json data file
        self.writeFile( jekyll_path+'/_data/content_blocks.json', JSON.pretty_generate(data['content_blocks']) )
        self.log('Writing file /_data/galleries.json')
    end
    
    # Content Images
    if data.key?("content_images")
        FileUtils::mkdir_p jekyll_path+'/assets/images/content'
      
        puts 'Content Images - ' + data['content_images'].length.to_s
        self.log('Content Images - ' + data['content_images'].length.to_s)

        data['content_images'].each do |image|
            image = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/content-blocks/' + image, jekyll_path+'/assets/images/content/' + image)
        end
        
        self.writeFile( jekyll_path+'/_data/content_images.json', JSON.pretty_generate(data['content_images']) )
    end
    
    # Taxonomy
    if data.key?("taxonomy")
        data['taxonomy'].each do |taxonomy_data_type|
            taxonomy_data_type['taxonomies'].each do |taxonomy|
                if '1' == taxonomy['enable_images']
                    FileUtils::mkdir_p jekyll_path+'/assets/images/tax-images/' + taxonomy['id']

                    if taxonomy.key?("terms")
                        taxonomy['terms'].each do |term|
                            if term['image']
                                image_path = jekyll_path+'/assets/images/tax-images/' + term['taxonomy_id'] 
                                src_image = end_point + '/media/images/'+site_id+'/taxonomies/' + term["image"]

                                #puts 'A: ' + src_image + ' -> ' + image_path + '/' + term["image"]

                                term['image'] = self.getAndSaveImages(src_image, image_path + '/' + term["image"])
                            end
                        end
                    end
                end
            end
        end
        
        self.writeFile( jekyll_path+'/_data/taxonomy.json', JSON.pretty_generate(data['taxonomy']) );
        self.log("Writing File /_data/taxonomy.json")
    end
    
    # Nav Menus
    if data.key?("nav_menus")
        puts "Make Dir" + jekyll_path+'/assets/images/nav-items'
        FileUtils::mkdir_p jekyll_path+'/assets/images/nav-items'

        puts 'Nav menus - ' + data['nav_menus'].length.to_s
        self.log('Nav Menus - ' + data['nav_menus'].length.to_s)

        data['nav_menus'].each do |nav_menu|
            puts "Nav Menus"
            if nav_menu['items']
                puts "Nav Menu Items"
                nav_menu['items'].each do |nav_menu_item|
                    if nav_menu_item['icon_image']
                        puts "Nav Item Image"
                        image_path = jekyll_path+'/assets/images/nav-items' 
                        src_image = end_point + '/media/images/'+site_id+'/nav_items/' +nav_menu_item['icon_image']
                        nav_menu_item['icon_image'] = self.getAndSaveImages(src_image, image_path + '/' + nav_menu_item['icon_image'])
                    end
                end
            end
        end

        self.writeFile( jekyll_path+'/_data/nav_menus.json', JSON.pretty_generate(data['nav_menus']) )
    end
    
    # Posts
    existing_post_files = Dir.glob(File.join(jekyll_path+'/_posts', "*"))

    # Remove all post files
    existing_post_files.each do |file|
        self.log('Deleting Post File ' + file)
        FileUtils.rm(file)
    end

    # Post files
    if data.key?("posts")
        FileUtils::mkdir_p jekyll_path+'/assets/images/posts'
        
        puts "Posts - " + data['posts'].length.to_s
        data['posts'].each do |post|
            self.log('Saving Post File ' + post['date']+'-'+post['slug']+'.md')
            
            # Do Tax
            self.taxonomyImages(post)
            
            # save image
            if post['image']
                post['image'] = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/posts/' + post['image'], jekyll_path+'/assets/images/posts/' + post['image'])
            end
            
            jpost = JekyllPost.new(jekyll_path+'/_posts', post)
            jpost.savePageFile
        end
    end


    # Galleries
    if data.key?("galleries")
        FileUtils::mkdir_p jekyll_path+'/assets/images/galleries'

        puts "Galleries - " + data['galleries'].length.to_s
        self.log("Galleries - " + data['galleries'].length.to_s)

        # Create files saved array
        gallery_files_saved = Array.new

        # set collection path
        gallery_collection_path = jekyll_path+'/_gallery'

        if data['site'].key?('gallery_make_detail_pages') && data['site']['gallery_make_detail_pages'] == "1"
            FileUtils::mkdir_p gallery_collection_path
            collection_config[ 'gallery' ] = {'output' => true}
        end

        # Save Photos
        data['galleries'].each_with_index do |gallery,idx|
            FileUtils::mkdir_p jekyll_path+'/assets/images/galleries/' + gallery['id']

            puts " - " + gallery['name'] + ' has ' + gallery['photos'].length.to_s + ' photos'
            self.log(" - " + gallery['name'] + ' has ' + gallery['photos'].length.to_s + ' photos')

            gallery['photos'].each_with_index do |photo,pidx|
                photo['image'] = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/galleries/photos/' + photo['image'], jekyll_path+'/assets/images/galleries/' + gallery['id'] + '/' + photo['image'])
            end

            # Detail Pages
            if data['site'].key?('gallery_make_detail_pages') && data['site']['gallery_make_detail_pages'] == "1"
            
                self.log('Saving Event File ' + gallery['slug']+'.md')

                if data['site'].key?('gallery_detail_layout') && data['site']['gallery_detail_layout'] != ""
                    gallery['layout'] = data['site']['gallery_detail_layout']
                end

                gallery_data = gallery.clone
                gallery_data.delete('id')
                gallery_data[ 'gallery_id' ] = gallery['id']

                jfile = JekyllFile.new(gallery_collection_path, gallery_data, 'collection', gallery['slug'])
                jfile.savePageFile

                gallery_files_saved.push( Pathname.new( jfile.getFileName ).basename.to_s )
            end
        end
        
        # Save Json Data
        self.writeFile( jekyll_path+'/_data/galleries.json', JSON.pretty_generate(data['galleries']) )
        self.log('Writing file /_data/galleries.json')
    end

    # Calendar
    if data.key?("calendar")
        FileUtils::mkdir_p jekyll_path+'/assets/images/calendar'
        FileUtils::mkdir_p jekyll_path+'/assets/files/calendar'

        puts "Calendar - " + data['calendar'].length.to_s
        self.log("Calendar - " + data['calendar'].length.to_s)

        # Create files saved array
        event_files_saved = Array.new

        event_collection_path = jekyll_path+'/_event'

        # Save Photos / Files / event pages
        if data['calendar'].key?("events")
            data['calendar']['events'].each do |event|
                if event['image']
                    event['image'] = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/calendar/' + event['image'], jekyll_path+'/assets/images/calendar/' + event['image'])
                end

                if event['file']
                    self.getAndSaveFile(end_point + '/media/files/'+site_id+'/calendar/' + event['file'], jekyll_path+'/assets/files/calendar/' + event['file'])
                end

                # Detail Pages
                if data['site'].key?('calendar_make_detail_pages') && data['site']['calendar_make_detail_pages'] == "1"
                    FileUtils::mkdir_p event_collection_path

                    event['dates'].each do |event_date|
                        event_data = event.clone
                        event_date.each do |key,value|
                            if 'date' == key
                                key = 'event_date'
                            end
                            event_data[ key ] = value
                        end

                        if data['site'].key?('calendar_detail_layout') && data['site']['calendar_detail_layout'] != ""
                            event_data['layout'] = data['site']['calendar_detail_layout']
                        end

                        self.log('Saving Event File ' + event_data['slug']+'-'+event_date['date']+'.md')

                        event_data[ 'event_id' ] = event_data['id']
                        event_data.delete('id')

                        jfile = JekyllFile.new(event_collection_path, event_data, 'collection', event_data['slug']+'-'+event_date['date'])
                        jfile.savePageFile

                        event_files_saved.push( Pathname.new( jfile.getFileName ).basename.to_s )
                    end
                end
            end
        end

        # Clean up unused collection files
        self.cleanUpFolder(event_collection_path, event_files_saved)
        
        # Save Json Data
        self.writeFile( jekyll_path+'/_data/calendar.json', JSON.pretty_generate(data['calendar']) )
        self.log('Writing File /_data/calendar.json')
    end

    if data['site'].key?('calendar_make_detail_pages') && data['site']['calendar_make_detail_pages'] == "1"
        collection_config[ 'event' ] = {'output' => true}
    end
    
    # Menus  (as json data and images)
    if data.key?("menus")
        # Save Json Data
        
        # Save Images
        FileUtils::mkdir_p jekyll_path+'/assets/images/menus'
        FileUtils::mkdir_p jekyll_path+'/assets/images/menus/items'
        
        data['menus'].each_with_index do |menu,idx|
            if menu['image']
                menu_image_path = jekyll_path+'/assets/images/menus'
                
                src_image = end_point + '/media/images/'+site_id+'/menus/' + menu['image']
                
                menu['image'] = self.getAndSaveImages(src_image, menu_image_path + '/' + menu['image']) 
            end
            if menu['download']
                menu_download_path = jekyll_path+'/assets/files/menus'
                
                src_image = end_point + '/media/files/'+site_id+'/menu_downloads/' + menu['download']
                
                self.getAndSaveFile(src_image, menu_download_path + '/' + menu['download']) 
            end
          
            menu['items'].each_with_index do |item,iidx|
                if item['image']
                    image_path = jekyll_path+'/assets/images/menus/items/'+menu['id']
                    FileUtils::mkdir_p image_path
                    
                    src_image = end_point + '/media/images/'+site_id+'/menus/items/' + item['image']
                    
                    item['image'] = self.getAndSaveImages(src_image, image_path+'/' + item['image'])
                end 
            end
        end
        
        self.writeFile( jekyll_path+'/_data/menus.json', JSON.pretty_generate(data['menus']) )
        
        self.log('Writing File /_data/menus.json')
    end
    
    # Downloads
    if data.key?("downloads")
        # Save Json Data
        self.writeFile( jekyll_path+'/_data/downloads.json', JSON.pretty_generate(data['downloads']) )
        
        # Make Dir
        FileUtils::mkdir_p jekyll_path+'/assets/files/downloads'
        
        # Download Files
        data['downloads'].each do |download|
            self.taxonomyImages(download)
            
            download_path = jekyll_path+'/assets/files/downloads'
            
            src_file = end_point + '/media/files/'+site_id+'/downloads/' + download['file']
            
            self.getAndSaveFile(src_file, download_path + '/' + download['file']) 
        end
    end
    
    # Properties
    if data.key?("properties")            
        # Make Dir
        FileUtils::mkdir_p jekyll_path+'/assets/images/properties'
        
        # Directory Check
        option_file_path = jekyll_path+'/assets/files/property_files'
        FileUtils::mkdir_p option_file_path
        
        option_image_path = jekyll_path+'/assets/images/property_images'
        FileUtils::mkdir_p option_image_path
        
        # Save Collection Info
        collection_config[ 'properties' ] = {'output' => true}
        
        # Directory Check
        collection_path = jekyll_path+'/_properties'
        FileUtils::mkdir_p collection_path

        # Create files saved array
        files_saved = Array.new
        
        # Save Individual Collection Files
        data['properties']['data'].each_with_index do |row,pidx|
            row_data = row.clone;
            row_data.delete('id')
            id_key = 'property_id'
            row_data[ id_key ] = row['id']
            
            # process taxonomies
            self.taxonomyImages(data['properties']['data'][pidx])
          
            # Save Images
            if row.key?("galleries")
                row['galleries'].each_with_index do |gallery,gidx|
                    # Directory Check
                    image_path = jekyll_path+'/assets/images/properties/'+row['id']
                    FileUtils::mkdir_p image_path
                    
                    if gallery.key?("images")                            
                        # save images
                        gallery['images'].each_with_index do |image,idx|
                            src_image = end_point + '/media/images/'+site_id+'/properties/'+row['id']+'/' + image['image']
                            
                            data['properties']['data'][pidx]['galleries'][gidx]['images'][idx]['image'] = self.getAndSaveImages(src_image, image_path+'/' + image['image'])
                        end
                    end
                end
            end
            
            if row.key?("images")
                # Directory Check
                image_path = jekyll_path+'/assets/images/properties/'+row['id']
                FileUtils::mkdir_p image_path
                
                # save images
                row['images'].each_with_index do |image,idx|
                    
                    src_image = end_point + '/media/images/'+site_id+'/properties/'+row['id']+'/' + image['image']
                    
                    data['properties']['data'][pidx]['images'][idx]['image'] = self.getAndSaveImages(src_image, image_path+'/' + image['image'])
                end
            end
            
            # image/file option fields
            data['properties']['options'].each do |option|
                if row[ option['key'] ]
                    case option['input_type']
                        when 'file'
                            src_file = end_point + '/media/files/'+site_id+'/property_files/' + row[ option['key'] ]
                            
                            self.getAndSaveFile(src_file, option_file_path+'/' + row[ option['key'] ])
                        when 'image'
                            src_image = end_point + '/media/images/'+site_id+'/property_images/' + row[ option['key'] ]
                            
                            
                            data['properties']['data'][pidx][ option['key'] ] = self.getAndSaveImages(src_image, option_image_path+'/' + row[ option['key'] ])
                    end
                end
            end
          
            jfile = JekyllFile.new(collection_path, row_data, 'collection')
            jfile.savePageFile

            files_saved.push( Pathname.new( jfile.getFileName ).basename.to_s )
            
            self.log('Saving Property File ' + jfile.getFileName)
        end
        
        # Save Json Data
        self.writeFile( jekyll_path+'/_data/properties.json', JSON.pretty_generate(data['properties']['data']) )
        self.writeFile( jekyll_path+'/_data/properties_options.json', JSON.pretty_generate(data['properties']['options']) )

        # Clean up unused collection files
        self.cleanUpFolder(collection_path, files_saved)
    end
    
    # Custom Collections
    if data.key?("collections")
        data['collections'].each do |collection_type,collection_data|
            puts "Collection - " + collection_type + ' - ' + collection_data.length.to_s
            self.log("Collection - " + collection_type + ' - ' + collection_data.length.to_s)

            # Save Images / Files
            collection_data['data'].each_with_index do |row,cidx|
                collection_data['fields'].each do |fld_key,fld_info|
                    row.each do |key,val|
                        #puts 'Row ['+key+']:['+val.to_s+']'
                        if fld_key == key && val
                            case fld_info['type']
                                when 'image'
                                    # Directory Check
                                    image_path = jekyll_path+'/assets/images/'+collection_type
                                    FileUtils::mkdir_p image_path
                                    
                                    src_image = end_point + '/media/images/'+site_id+'/'+collection_type+'/' + val
                                    
                                    collection_data['data'][cidx][fld_key] = self.getAndSaveImages(src_image, image_path+'/' + val)
                                when 'file'
                                    # Directory Check
                                    file_path = jekyll_path+'/assets/files/'+collection_type
                                    FileUtils::mkdir_p file_path
                                    
                                    src_file = end_point + '/media/files/'+site_id+'/'+collection_type+'/' + val
                                    
                                    self.getAndSaveFile(src_file, file_path+'/' + val)
                            end
                        end
                    end
                end
                
                #hasmany data
                if row.key?("hasmany_data")
                    row['hasmany_data'].each do |hasmany_type,hasmany_data|
                        #puts " - hasmany : " + hasmany_type + " - " + hasmany_data['data'].length.to_s
                        hasmany_data['fields'].each do |fld_key,fld_info|
                            hasmany_data['data'].each_with_index do |hsrow,hsidx|
                                hsrow.each do |key,val|
                                    if fld_key == key && val
                                        case fld_info['type']
                                            when 'image'
                                                # Directory Check
                                                image_path = jekyll_path+'/assets/images/'+collection_type+'/'+hasmany_type+'/'+row['id']
                                                FileUtils::mkdir_p image_path
                                                
                                                src_image = end_point + '/media/images/'+site_id+'/'+collection_type+'/'+row['id']+'/' + val
                                                
                                                collection_data['data'][cidx]['hasmany_data'][hasmany_type]['data'][hsidx][fld_key] = self.getAndSaveImages(src_image, image_path+'/' + val)
                                            when 'file'
                                                # Directory Check
                                                file_path = jekyll_path+'/assets/files/'+collection_type+'/'+hasmany_type+'/'+row['id']
                                                FileUtils::mkdir_p file_path
                                                
                                                src_file = end_point + '/media/files/'+site_id+'/'+collection_type+'/'+row['id']+'/' + val
                                                
                                                self.getAndSaveFile(src_file, file_path+'/' + val)
                                        end
                                    end
                                end
                            end
                        end
                    end # has many data each
                end
            end
            
            # Loop through the collection data
            case collection_data['settings']['type']
                when 'collection'
                    # Add to collection hash
                    collection_config[ collection_type ] = {'output' => true}
                    
                    # Directory Check
                    collection_path = jekyll_path+'/_' + collection_type
                    FileUtils::mkdir_p collection_path
    
                    # Create files saved array
                    files_saved = Array.new
                    
                    # Save Individual Collection Files
                    collection_data['data'].each do |row|
                        col_data = row.clone;
                        col_data.delete('id')
                        id_key = collection_type + '_id'
                        col_data[ id_key ] = row['id']
                        jfile = JekyllFile.new(collection_path, col_data, 'collection')
                        jfile.savePageFile
    
                        files_saved.push( Pathname.new( jfile.getFileName ).basename.to_s )
    
                        self.log('Saving Collection File ' + jfile.getFileName)
                    end
    
                    # Clean up unused collection files
                    self.cleanUpFolder(collection_path, files_saved)
            end
            
            # Save Json Data
            self.writeFile( jekyll_path+'/_data/'+collection_type+'.json', JSON.pretty_generate(collection_data['data']) )
            self.log('Writing File /_data/'+collection_type+'.json')
            
        end
    end

    # Add Collections to _config.tml
    config['collections'] = collection_config
    self.writeFile( jekyll_path+"/_config.yml", config.to_yaml )
    self.log('Writing File /_config.yml')
    
    @@logger.close
end

.taxonomyImages(row) ⇒ Object



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# File 'lib/zerofetcher.rb', line 701

def self.taxonomyImages(row)
    if row.key?("taxonomy")
        ##puts ' - Found taxonomy key ['+row['id']+']'
        row['taxonomy'].each_with_index do |tax_row,idx|
            if tax_row.key?("image")
                if tax_row["image"]
                    ##puts ' - ' + idx.to_s + ' saving tax image'
                    ##puts ' - - ['+tax_row["image"]+']'
                    image_path = @@jekyll_path+'/assets/images/tax-images/' + tax_row['taxonomy_id'] 
                    src_image = @@end_point + '/media/images/'+@@site_id+'/taxonomies/' + tax_row["image"]
                    #puts 'F: ' + src_image + ' -> ' + image_path + '/' + tax_row["image"]
                    
                    row['taxonomy'][idx]['image'] = self.getAndSaveImages(src_image, image_path + '/' + tax_row["image"])
                end
            end
        end
    end
    
    #return row
end

.writeFile(path, contents) ⇒ Object



733
734
735
736
737
# File 'lib/zerofetcher.rb', line 733

def self.writeFile(path, contents)
    File.open(path, 'wb') do |fo|
        fo.write(contents)
    end
end