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

.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

.generate_migrationObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/domain_handler.rb', line 71

def self.generate_migration
    require 'active_record'
    require 'active_record/migration'
    # Connect to your database
    ActiveRecord::Base.establish_connection(
        database: ENV['DB_NAME'],
        username: ENV['DB_USER'],
        password: ENV['DB_PWD'],
        host: 'http://localhost:5432',
        adapter: ENV['DB_ADAPTER']
    )

    timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
    migration_file = "db/migrate/#{timestamp}_create_domains.rb"
    # Use the MigrationGenerator class to generate the migration file
    generator = Rails::Generators.invoke('active_record:migration', ['create_domains'], destination_root: Rails.root)
    generator.invoke_all
    generator.write_migration(migration_file)
end