binda-shopify 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +31 -5
- data/app/views/shared/_synchronize.html.erb +7 -5
- data/lib/binda/shopify/collection.rb +1 -0
- data/lib/binda/shopify/importer.rb +68 -42
- data/lib/binda/shopify/installer.rb +3 -3
- data/lib/binda/shopify/version.rb +1 -1
- data/lib/binda/shopify.rb +9 -3
- data/lib/generators/binda/shopify/install_generator.rb +7 -5
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b088e0b53767852425a86f735f89859a9dc4ef5f
|
4
|
+
data.tar.gz: af65fce8b0df37293586008063c54d2dd6fb5e0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 385050f8102f201471c69da9479953aa12e0f5b198d37dcd6561e2b6d8e7742fb0bb967e2f2b36fdadf563fb80c7720cd13606d63644dca4105aa9acfb2a715f
|
7
|
+
data.tar.gz: 62430e157c995084b1cc3efdb0d1d1f9e5313a2490c5386d8f6bad55b953f822f536dcee27ee956cc1b2e754b93b9c32e2a794dadefc52a5684c5fe974b5e1ff
|
data/README.md
CHANGED
@@ -6,9 +6,19 @@ Extension that lets you import your Shopify products into your Binda application
|
|
6
6
|
[](https://codeclimate.com/github/lacolonia/binda-shopify/maintainability)
|
7
7
|
[](https://codeclimate.com/github/lacolonia/binda-shopify/test_coverage)
|
8
8
|
|
9
|
-
# Installation
|
10
9
|
|
11
|
-
|
10
|
+
## Requirements
|
11
|
+
|
12
|
+
Before you install **Binda Shopify** you need to setup your Shopify account properly. Please follow the steps below.
|
13
|
+
|
14
|
+
### Create a private app
|
15
|
+
|
16
|
+
Binda Shopify relies on private apps. A detailed guide on what are and how to create them can be found in the [Shopify official documentation](https://help.shopify.com/manual/apps/private-apps).
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
If you haven't read the **Requirements** section do it now before continuing.
|
21
|
+
Install Binda via terminal. See [Binda installation guide](https://www.rubydoc.info/gems/binda#Installation) for more information.
|
12
22
|
|
13
23
|
Just add the gem to your application's Gemfile:
|
14
24
|
|
@@ -22,10 +32,26 @@ Then, run:
|
|
22
32
|
bundle install
|
23
33
|
```
|
24
34
|
|
25
|
-
After that, you need to setup your connection to Shopify and create `Binda::Structures` where your data will be imported.
|
35
|
+
After that, you need to setup your connection to Shopify and create `Binda::Structures` where your data will be imported. To do so you can run the following command.
|
36
|
+
|
37
|
+
_Development env_
|
26
38
|
|
27
39
|
```bash
|
28
|
-
|
40
|
+
rails generate binda:shopify:install
|
29
41
|
```
|
30
42
|
|
31
|
-
|
43
|
+
_Production env_
|
44
|
+
|
45
|
+
```bash
|
46
|
+
RAILS_ENV=production bundle exec rails generate binda:shopify:install
|
47
|
+
```
|
48
|
+
|
49
|
+
> #### NOTE
|
50
|
+
>
|
51
|
+
> Your Shopify **shop name**, which is required during the installation process, corresponds to the subdomain portion of you shopify store.
|
52
|
+
>
|
53
|
+
> For example: for `https://cool-shoes.myshopify.com` the Shopify **shop name** is `cool-shoes`.
|
54
|
+
>
|
55
|
+
> Don't confuse it with the private app name.
|
56
|
+
|
57
|
+
Once you complete the installation process you are good to go! Log into your admin panel and click the sync button.
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
<
|
3
|
-
<
|
4
|
-
|
5
|
-
</
|
1
|
+
<% if Binda::Structure.where(slug: "shopify-settings").any? %>
|
2
|
+
<li>
|
3
|
+
<a class="synch-with-shopify" data-message="<%= I18n.t('binda.shopify.loading') %>" data-success="<%= I18n.t('binda.shopify.success') %>" href="<%= shopify_import_path %>">
|
4
|
+
<i class="fas fa-sync"></i> <%= I18n.t('binda.shopify.synch_with_shopify') %>
|
5
|
+
</a>
|
6
|
+
</li>
|
7
|
+
<% end %>
|
@@ -2,13 +2,20 @@ require 'shopify_api'
|
|
2
2
|
|
3
3
|
module Binda
|
4
4
|
module Shopify
|
5
|
+
# Shopify Importer
|
6
|
+
#
|
7
|
+
# This class is responsible for fetching data from a Shopify account
|
5
8
|
class Importer
|
6
9
|
attr_accessor :client, :settings
|
7
10
|
|
8
11
|
def initialize
|
9
12
|
settings_slug = 'shopify-settings'
|
10
13
|
@settings = ::Binda::Structure.find_by(slug: 'shopify-settings').board
|
11
|
-
|
14
|
+
connection_keys = ::Binda::Shopify::CONNECTION_KEYS.map do |field|
|
15
|
+
slug = "#{settings_slug}-#{field.to_s.tr('_', '-')}"
|
16
|
+
settings.get_string(slug).strip
|
17
|
+
end
|
18
|
+
api_key, password, shared_secret, shop_name = connection_keys
|
12
19
|
|
13
20
|
@client = ::ShopifyAPI
|
14
21
|
@client::Base.site = "https://#{api_key}:#{password}@#{shop_name}.myshopify.com/admin"
|
@@ -18,67 +25,86 @@ module Binda
|
|
18
25
|
def run!
|
19
26
|
start_time = Time.now
|
20
27
|
::Binda::Shopify::STRUCTURES.each do |name, structure_fields|
|
21
|
-
|
28
|
+
slug = "#{@settings.slug}-#{name.to_s.tr('_', '-')}"
|
29
|
+
structure_slug = settings.get_string(slug).strip.parameterize
|
22
30
|
structure = ::Binda::Structure.find_by slug: structure_slug
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
component.updated_at = Time.now
|
29
|
-
component.save
|
30
|
-
structure_fields.each do |field_group_slug, fields|
|
31
|
-
field_group_slug = "#{structure_slug}-#{field_group_slug}"
|
32
|
-
field_group = structure.field_groups.find_by slug: field_group_slug
|
33
|
-
if field_group
|
34
|
-
fields.each do |field_slug_and_type, method|
|
35
|
-
field_slug, type = field_slug_and_type.split(':')
|
36
|
-
type ||= 'string'
|
37
|
-
|
38
|
-
field_setting = field_group.field_settings.find_by(slug: "#{field_group_slug}-#{field_slug}")
|
39
|
-
field_type_association = type.pluralize
|
40
|
-
if ::Binda::Component.reflections.keys.include? field_type_association
|
41
|
-
component.send(field_type_association).find_by(field_setting_id: field_setting.id).update content: item.send(method) if field_setting
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
structure.components.where("updated_at < ?", start_time).update_all publish_state: 'draft'
|
31
|
+
create_component_and_fields(name, structure_fields, structure)
|
32
|
+
structure
|
33
|
+
.components
|
34
|
+
.where('updated_at < ?', start_time)
|
35
|
+
.update_all(publish_state: 'draft')
|
49
36
|
end
|
50
|
-
|
51
37
|
::Binda::Shopify::STRUCTURES.keys
|
52
38
|
end
|
53
39
|
|
54
|
-
def
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
40
|
+
def create_component_and_fields(name, structure_fields, structure)
|
41
|
+
send("fetch_#{name.to_s.pluralize}").each do |item|
|
42
|
+
next unless item.id.present?
|
43
|
+
component = ::Binda::Component.find_or_initialize_by(
|
44
|
+
slug: item.id,
|
45
|
+
structure: structure
|
46
|
+
)
|
47
|
+
component.name ||= item.title
|
48
|
+
component.publish_state = 'published'
|
49
|
+
component.updated_at = Time.now
|
50
|
+
component.save
|
51
|
+
create_fields(structure_fields, structure, component, item)
|
61
52
|
end
|
53
|
+
end
|
62
54
|
|
63
|
-
|
55
|
+
def create_fields(structure_fields, structure, component, item)
|
56
|
+
structure_fields.each do |field_group_slug, fields|
|
57
|
+
field_group_slug = "#{structure.slug}-#{field_group_slug}"
|
58
|
+
field_group = structure.field_groups.find_by(slug: field_group_slug)
|
59
|
+
next unless field_group
|
60
|
+
create_field(field_group, fields, component, item)
|
61
|
+
end
|
64
62
|
end
|
65
63
|
|
66
|
-
def
|
64
|
+
def create_field(field_group, fields, component, item)
|
65
|
+
fields.each do |field_slug_and_type, method|
|
66
|
+
field_slug, type = field_slug_and_type.split(':')
|
67
|
+
type ||= 'string'
|
68
|
+
next unless ::Binda::Component.reflections.key? type.pluralize
|
69
|
+
field_setting = field_group.field_settings.find_by(
|
70
|
+
slug: "#{field_group.slug}-#{field_slug}"
|
71
|
+
)
|
72
|
+
next unless field_setting
|
73
|
+
component
|
74
|
+
.send(type.pluralize)
|
75
|
+
.find_by(field_setting_id: field_setting.id)
|
76
|
+
.update(content: item.send(method))
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def fetch(client_source, binda_source)
|
67
81
|
items = []
|
68
|
-
number_of_pages = (
|
82
|
+
number_of_pages = (client_source.count / 250.0).ceil
|
69
83
|
number_of_pages.times do |i|
|
70
|
-
|
71
|
-
|
84
|
+
fetched_sources = client_source.find(
|
85
|
+
:all,
|
86
|
+
params: { limit: 250, page: i + 1 }
|
87
|
+
)
|
88
|
+
fetched_sources.each do |p|
|
89
|
+
items << binda_source.new(p, shop)
|
72
90
|
end
|
73
91
|
end
|
74
92
|
|
75
93
|
items
|
76
94
|
end
|
77
95
|
|
96
|
+
def fetch_collections
|
97
|
+
fetch(@client::CustomCollection, ::Binda::Shopify::Collection)
|
98
|
+
end
|
99
|
+
|
100
|
+
def fetch_products
|
101
|
+
fetch(@client::Product, ::Binda::Shopify::Product)
|
102
|
+
end
|
103
|
+
|
78
104
|
def fetch_product_types
|
79
105
|
products = fetch_products
|
80
106
|
product_types = products.map(&:product_type).uniq
|
81
|
-
product_types.map{|p| ::Binda::Shopify::ProductType.new(p, shop) }
|
107
|
+
product_types.map { |p| ::Binda::Shopify::ProductType.new(p, shop) }
|
82
108
|
end
|
83
109
|
|
84
110
|
def shop
|
@@ -4,7 +4,7 @@ module Binda
|
|
4
4
|
def create_settings_board values
|
5
5
|
raise ArgumentError.new 'You must provide an array or hash of settings values' unless values.is_a?(Array) || values.is_a?(Hash)
|
6
6
|
values = Hash[(CONNECTION_KEYS+STRUCTURES.keys).zip(values)] if values.is_a? Array
|
7
|
-
structure = Binda::Structure.find_or_create_by( name: 'Shopify Settings', slug: 'shopify-settings', instance_type: 'board' )
|
7
|
+
structure = ::Binda::Structure.find_or_create_by( name: 'Shopify Settings', slug: 'shopify-settings', instance_type: 'board' )
|
8
8
|
board = structure.board
|
9
9
|
field_settings = structure.field_groups.first.field_settings
|
10
10
|
CONNECTION_KEYS.each do |field_name|
|
@@ -26,8 +26,8 @@ module Binda
|
|
26
26
|
|
27
27
|
def create_item_structure structure_name, name
|
28
28
|
structure_slug = name.parameterize
|
29
|
-
structure = Binda::Structure.find_or_create_by name: name, slug: structure_slug, instance_type: 'component', has_categories: false
|
30
|
-
Binda::Shopify::STRUCTURES[structure_name.to_sym].each do |field_group_slug, fields|
|
29
|
+
structure = ::Binda::Structure.find_or_create_by name: name, slug: structure_slug, instance_type: 'component', has_categories: false
|
30
|
+
::Binda::Shopify::STRUCTURES[structure_name.to_sym].each do |field_group_slug, fields|
|
31
31
|
field_group = structure.field_groups.find_by slug: "#{structure_slug}-#{field_group_slug}"
|
32
32
|
if field_group.nil?
|
33
33
|
field_group = structure.field_groups.create! name: field_group_slug.titleize, slug: "#{structure_slug}-#{field_group_slug}"
|
data/lib/binda/shopify.rb
CHANGED
@@ -1,15 +1,21 @@
|
|
1
|
+
# Main classes
|
1
2
|
require 'binda'
|
2
3
|
require "binda/shopify/engine"
|
4
|
+
|
5
|
+
# Secondary classes
|
6
|
+
require 'binda/shopify/collection'
|
3
7
|
require 'binda/shopify/importer'
|
4
8
|
require 'binda/shopify/item'
|
5
9
|
require 'binda/shopify/product'
|
6
|
-
require 'binda/shopify/collection'
|
7
10
|
require 'binda/shopify/product_type'
|
11
|
+
|
12
|
+
# Vendor
|
8
13
|
require 'deface'
|
14
|
+
require 'byebug'
|
9
15
|
|
10
16
|
module Binda
|
11
17
|
module Shopify
|
12
|
-
CONNECTION_KEYS = %i(api_key password shared_secret shop_name)
|
18
|
+
CONNECTION_KEYS = %i(api_key password shared_secret shop_name).freeze
|
13
19
|
STRUCTURES = {
|
14
20
|
product: {
|
15
21
|
'shopify-details' => {
|
@@ -30,6 +36,6 @@ module Binda
|
|
30
36
|
'handle' => 'name'
|
31
37
|
}
|
32
38
|
}
|
33
|
-
}
|
39
|
+
}.freeze
|
34
40
|
end
|
35
41
|
end
|
@@ -1,7 +1,9 @@
|
|
1
|
+
require 'binda/shopify/installer'
|
2
|
+
|
1
3
|
module Binda
|
2
4
|
module Shopify
|
3
5
|
class InstallGenerator < Rails::Generators::Base
|
4
|
-
source_root File.expand_path('../templates',__FILE__)
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
7
|
attr_accessor :structure
|
6
8
|
|
7
9
|
def start
|
@@ -10,13 +12,13 @@ module Binda
|
|
10
12
|
|
11
13
|
def create_shopify_settings
|
12
14
|
puts "1) Setting up Shopify Connection"
|
13
|
-
@installer = Installer.new
|
15
|
+
@installer = ::Binda::Shopify::Installer.new
|
14
16
|
@settings = {}
|
15
|
-
Binda::Shopify::CONNECTION_KEYS.each do |field_name|
|
17
|
+
::Binda::Shopify::CONNECTION_KEYS.each do |field_name|
|
16
18
|
STDOUT.puts "What is your Shopify #{field_name.to_s.titleize}?"
|
17
19
|
@settings[field_name] = STDIN.gets.strip
|
18
20
|
end
|
19
|
-
Binda::Shopify::STRUCTURES.each do |structure_name, structure_fields|
|
21
|
+
::Binda::Shopify::STRUCTURES.each do |structure_name, structure_fields|
|
20
22
|
default_name = "Shopify #{structure_name.to_s.titleize}"
|
21
23
|
puts "How would you like to name your #{structure_name} structure? ['#{default_name}']"
|
22
24
|
@settings[structure_name] = STDIN.gets.strip.presence || default_name
|
@@ -26,7 +28,7 @@ module Binda
|
|
26
28
|
end
|
27
29
|
|
28
30
|
def setup_structures
|
29
|
-
Binda::Shopify::STRUCTURES.each.with_index do |(structure_name, structure_fields), index|
|
31
|
+
::Binda::Shopify::STRUCTURES.each.with_index do |(structure_name, structure_fields), index|
|
30
32
|
name = @settings[structure_name].presence || structure_name.to_s.titleize
|
31
33
|
puts "#{index+2}) Setting up #{name} Structure"
|
32
34
|
@installer.create_item_structure structure_name, name
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: binda-shopify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marco Crepaldi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -218,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
218
218
|
version: '0'
|
219
219
|
requirements: []
|
220
220
|
rubyforge_project:
|
221
|
-
rubygems_version: 2.
|
221
|
+
rubygems_version: 2.5.1
|
222
222
|
signing_key:
|
223
223
|
specification_version: 4
|
224
224
|
summary: Binda plugin that lets you import products from your Shopify store.
|