Class: Binda::Shopify::Importer
- Inherits:
-
Object
- Object
- Binda::Shopify::Importer
- Defined in:
- lib/binda/shopify/importer.rb
Overview
Shopify Importer
This class is responsible for fetching data from a Shopify account
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
- #create_component_and_fields(name, structure_fields, structure) ⇒ Object
- #create_field(field_group, fields, component, item) ⇒ Object
- #create_fields(structure_fields, structure, component, item) ⇒ Object
- #fetch(client_source, binda_source) ⇒ Object
- #fetch_collections ⇒ Object
- #fetch_product_types ⇒ Object
- #fetch_products ⇒ Object
-
#initialize ⇒ Importer
constructor
A new instance of Importer.
- #run! ⇒ Object
- #shop ⇒ Object
Constructor Details
#initialize ⇒ Importer
Returns a new instance of Importer.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/binda/shopify/importer.rb', line 11 def initialize settings_slug = 'shopify-settings' @settings = ::Binda::Structure.find_by(slug: 'shopify-settings').board connection_keys = ::Binda::Shopify::CONNECTION_KEYS.map do |field| slug = "#{settings_slug}-#{field.to_s.tr('_', '-')}" settings.get_string(slug).strip end api_key, password, shared_secret, shop_name = connection_keys @client = ::ShopifyAPI @client::Base.site = "https://#{api_key}:#{password}@#{shop_name}.myshopify.com/admin" @client::Session.setup(api_key: api_key, secret: shared_secret) end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
9 10 11 |
# File 'lib/binda/shopify/importer.rb', line 9 def client @client end |
#settings ⇒ Object
Returns the value of attribute settings.
9 10 11 |
# File 'lib/binda/shopify/importer.rb', line 9 def settings @settings end |
Instance Method Details
#create_component_and_fields(name, structure_fields, structure) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/binda/shopify/importer.rb', line 40 def create_component_and_fields(name, structure_fields, structure) send("fetch_#{name.to_s.pluralize}").each do |item| next unless item.id.present? component = ::Binda::Component.find_or_initialize_by( slug: item.id, structure: structure ) component.name ||= item.title component.publish_state = 'published' component.updated_at = Time.now component.save create_fields(structure_fields, structure, component, item) end end |
#create_field(field_group, fields, component, item) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/binda/shopify/importer.rb', line 64 def create_field(field_group, fields, component, item) fields.each do |field_slug_and_type, method| field_slug, type = field_slug_and_type.split(':') type ||= 'string' next unless ::Binda::Component.reflections.key? type.pluralize field_setting = field_group.field_settings.find_by( slug: "#{field_group.slug}-#{field_slug}" ) next unless field_setting component .send(type.pluralize) .find_by(field_setting_id: field_setting.id) .update(content: item.send(method)) end end |
#create_fields(structure_fields, structure, component, item) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/binda/shopify/importer.rb', line 55 def create_fields(structure_fields, structure, component, item) structure_fields.each do |field_group_slug, fields| field_group_slug = "#{structure.slug}-#{field_group_slug}" field_group = structure.field_groups.find_by(slug: field_group_slug) next unless field_group create_field(field_group, fields, component, item) end end |
#fetch(client_source, binda_source) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/binda/shopify/importer.rb', line 80 def fetch(client_source, binda_source) items = [] number_of_pages = (client_source.count / 250.0).ceil number_of_pages.times do |i| fetched_sources = client_source.find( :all, params: { limit: 250, page: i + 1 } ) fetched_sources.each do |p| items << binda_source.new(p, shop) end end items end |
#fetch_collections ⇒ Object
96 97 98 |
# File 'lib/binda/shopify/importer.rb', line 96 def fetch_collections fetch(@client::CustomCollection, ::Binda::Shopify::Collection) end |
#fetch_product_types ⇒ Object
104 105 106 107 108 |
# File 'lib/binda/shopify/importer.rb', line 104 def fetch_product_types products = fetch_products product_types = products.map(&:product_type).uniq product_types.map { |p| ::Binda::Shopify::ProductType.new(p, shop) } end |
#fetch_products ⇒ Object
100 101 102 |
# File 'lib/binda/shopify/importer.rb', line 100 def fetch_products fetch(@client::Product, ::Binda::Shopify::Product) end |
#run! ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/binda/shopify/importer.rb', line 25 def run! start_time = Time.now ::Binda::Shopify::STRUCTURES.each do |name, structure_fields| slug = "#{@settings.slug}-#{name.to_s.tr('_', '-')}" structure_slug = settings.get_string(slug).strip.parameterize structure = ::Binda::Structure.find_by slug: structure_slug create_component_and_fields(name, structure_fields, structure) structure .components .where('updated_at < ?', start_time) .update_all(publish_state: 'draft') end ::Binda::Shopify::STRUCTURES.keys end |
#shop ⇒ Object
110 111 112 |
# File 'lib/binda/shopify/importer.rb', line 110 def shop @shop ||= @client::Shop.current end |