Class: AmazonMarketplace

Inherits:
ApplicationRecord show all
Defined in:
app/models/amazon_marketplace.rb

Overview

== Schema Information

Table name: amazon_marketplaces
Database name: primary

id :bigint not null, primary key
marketplace_identifier :string(20) not null
name :string not null
url :string
country_id :integer not null

Indexes

index_amazon_marketplaces_on_marketplace_identifier (marketplace_identifier) UNIQUE

Foreign Keys

fk_rails_... (country_id => countries.id)

Constant Summary collapse

MARKETPLACE_ID_US =
'ATVPDKIKX0DER'
MARKETPLACE_ID_CA =
'A2EUQ1WTGCTBG2'
MARKETPLACE_IDS_NA =
[MARKETPLACE_ID_US, MARKETPLACE_ID_CA]
FBA_CHANNEL_CODE_NA =
'AMAZON_NA'
FBA_CHANNEL_CODE_EU =
'AMAZON_EU'

Instance Attribute Summary collapse

Belongs to collapse

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#marketplace_identifierObject (readonly)



37
# File 'app/models/amazon_marketplace.rb', line 37

validates :marketplace_identifier, presence: true, uniqueness: true

#nameObject (readonly)



36
# File 'app/models/amazon_marketplace.rb', line 36

validates :name, presence: true

Class Method Details

.ca_marketplaceObject



43
44
45
# File 'app/models/amazon_marketplace.rb', line 43

def self.ca_marketplace
  find_by(marketplace_identifier: MARKETPLACE_ID_CA)
end

.north_american_marketplacesActiveRecord::Relation<AmazonMarketplace>

A relation of AmazonMarketplaces that are north american marketplaces. Active Record Scope

Returns:

See Also:



32
# File 'app/models/amazon_marketplace.rb', line 32

scope :north_american_marketplaces, -> { where(marketplace_identifier: MARKETPLACE_IDS_NA) }

.select_optionsObject



47
48
49
# File 'app/models/amazon_marketplace.rb', line 47

def self.select_options
  eager_load(:country).map { |am| [am.to_s, am.id] }
end

.us_marketplaceObject



39
40
41
# File 'app/models/amazon_marketplace.rb', line 39

def self.us_marketplace
  find_by(marketplace_identifier: MARKETPLACE_ID_US)
end

Instance Method Details

#amazon_schemasActiveRecord::Relation<AmazonSchema>

Returns:

See Also:



29
# File 'app/models/amazon_marketplace.rb', line 29

has_many :amazon_schemas, dependent: :destroy, inverse_of: :amazon_marketplace

#catalogsActiveRecord::Relation<Catalog>

Returns:

  • (ActiveRecord::Relation<Catalog>)

See Also:



28
# File 'app/models/amazon_marketplace.rb', line 28

has_many :catalogs, dependent: :nullify, inverse_of: :amazon_marketplace

#countryCountry

Returns:

See Also:



27
# File 'app/models/amazon_marketplace.rb', line 27

belongs_to :country

#fba_channel_codeObject



59
60
61
62
63
64
65
66
# File 'app/models/amazon_marketplace.rb', line 59

def fba_channel_code
# One of AMAZON_NA AMAZON_EU or AMAZON_JP per: https://developer-docs.amazon.com/sp-api/docs/mapping-product-attributes#inventory-feed-mappings
  if MARKETPLACE_IDS_NA.include?(marketplace_identifier)
    FBA_CHANNEL_CODE_NA
  else
    FBA_CHANNEL_CODE_EU
  end
end

#supports_business_offer?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/models/amazon_marketplace.rb', line 55

def supports_business_offer?
  marketplace_identifier.in?(MARKETPLACE_IDS_NA)
end

#to_sObject



68
69
70
# File 'app/models/amazon_marketplace.rb', line 68

def to_s
  "#{name} - #{marketplace_identifier}"
end

#url_for_asin(asin) ⇒ Object



51
52
53
# File 'app/models/amazon_marketplace.rb', line 51

def url_for_asin(asin)
  "#{url}/dp/#{asin}"
end