Class: DomainHandler

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

Class Method Summary collapse

Class Method Details

.add_domains(shop) ⇒ Object



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
# File 'lib/domain_handler.rb', line 2

def self.add_domains(shop)
    domain_query = "{
        shop {
            domains {
                id
                host
                url
                sslEnabled
                localization { defaultLocale country alternateLocales}
                marketWebPresence {rootUrls market}
            }
        }
    }"
    shop.with_shopify_session do
        result = ShopifyGraphApi::Query.get(domain_query)
        domains_data_arr = result.data.shop.domains
        domains_data_arr.each do |domain|
            data = domain.to_h
            id = data['id'].split('Domain/').last
            ssl_enabled = data['sslEnabled']
            host = data['host']
            url = data['url']
            market_web_presence = data['marketWebPresence'].to_json
            localization = data['localization'].to_json
            if Domain.where(domain_id: id).exists?
                next
            end
            Domain.new(
                domain_id: id,
                ssl_enabled: ssl_enabled, 
                localization: localization,
                market_web_presence: market_web_presence, 
                host: host,
                url: url,
                shop_id: shop.id
            ).save
        end
    end
end

.create_migrationObject



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
# File 'lib/domain_handler.rb', line 71

def self.create_migration
    migration_name = 'create_domains'
  
    # Generate a timestamp to use as the migration version
    timestamp = Time.now.strftime('%Y%m%d%H%M%S')
  
    # Generate the migration file name
    migration_file_name = "#{timestamp}_#{migration_name}.rb"
  
    # Specify the migration class name
    migration_class_name = migration_name.camelize
  
    # Specify the migration file path
    migration_file_path = Rails.root.join('db', 'migrate', migration_file_name)
    # Create the migration file
    File.open(migration_file_path, 'w') do |file|
      file.write("class #{migration_class_name} < ActiveRecord::Migration[6.1]\n")
      file.write("  def change\n")
      file.write("    create_table :domains, id: false, primary_key: :domain_id do |t|\n")
      file.write("      # Define your table columns here\n")
      file.write("      t.references :shop, foreign_key: true\n")
      file.write("      t.string :host, null: false, default: ''\n")
      file.write("      t.string :localization\n")
      file.write("      t.string :market_web_presence\n")
      file.write("      t.boolean :ssl_enabled, null: false\n")
      file.write("      t.string :url\n")
      file.write("      t.string :domain_id\n")
      file.write("      t.index :domain_id, unique: true\n")
      file.write("      t.timestamps\n")
      file.write("    end\n")
      file.write("  end\n")
      file.write("end\n")
    end

    model_name = 'domain.rb'
    model_file_path = Rails.root.join('app', 'models', model_name)
    File.open(model_file_path, 'w') do |file|
        file.write("class Domain < ApplicationRecord \n")
        file.write("    self.primary_key = :domain_id \n")
        file.write("    belongs_to :shop \n")
        file.write("    belongs_to :shop \n")
        file.write("end \n")
    end
end

.domains_create(params, shop_id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/domain_handler.rb', line 42

def self.domains_create(params, shop_id)
    return if Domain.where(domain_id: params[:id]).exists?
  
    Domain.new(
        domain_id: params[:id],
        host: params[:host],
        ssl_enabled: params[:ssl_enabled],
        localization: params[:localization].to_json,
        market_web_presence: params[:marketWebPresence].to_json,
        url: params[:url],
        shop_id: shop_id
    ).save
end

.domains_delete(params) ⇒ Object



56
57
58
# File 'lib/domain_handler.rb', line 56

def self.domains_delete(params)
    Domain.delete(params[:id])
end

.domains_update(params) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/domain_handler.rb', line 60

def self.domains_update(params)
    domain = Domain.find_by(domain_id: params[:id])
    return if domain.nil?
  
    domain.update(
        host: params[:host],
        ssl_enabled: params[:ssl_enabled],
        localization: params[:localization].to_json
    )
end