mercadolibre_rails 0.6 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/jobs/mercadolibre_rails/sync_mercadolibre_product_job.rb +27 -0
- data/app/jobs/mercadolibre_rails/sync_mercadolibre_seller_job.rb +15 -0
- data/app/models/mercadolibre_rails/product.rb +11 -20
- data/app/models/mercadolibre_rails/seller.rb +12 -15
- data/lib/mercadolibre_rails/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 546985e651f68fd892414ff8d517f169fce92c0efdd5263397b7647658c4a170
|
4
|
+
data.tar.gz: 9ac297e46491017e5cf088b452d09f1c1328d9890a5efb9a7fc9a6e1d5e2e6a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5a23278a67b3ec5e30f9a1ecbbf9dac1e5b5677c1bfabf6752c3a7816b1badede91b0fe9c24c860983ffc84de553bad5e8264ab81bac0011dfa5f292d6f40ee
|
7
|
+
data.tar.gz: 6824b1a3d9e0ae3939a2e6e4c7035f00fdfe2f45a1dea33f3a456d990a00a2f71b481bcabc3d2c6b8308c743d1ad1ade66b476bfd4dd606eb2a159b6c6efaff2
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module MercadolibreRails
|
2
|
+
class SyncMercadolibreProductJob < ApplicationJob
|
3
|
+
queue_as :default
|
4
|
+
|
5
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
6
|
+
def perform(product_id)
|
7
|
+
product = MercadolibreRails::Product.find(product_id)
|
8
|
+
|
9
|
+
ml_product = MercadolibreApi::Products::Queries::Find.run!(product_id: product.mercadolibre_id)
|
10
|
+
ml_seller = MercadolibreRails::Seller.where(
|
11
|
+
mercadolibre_id: ml_product[:seller_id], site: product.site
|
12
|
+
).first_or_create
|
13
|
+
|
14
|
+
product.update!(
|
15
|
+
seller: ml_seller, title: ml_product[:title], price: ml_product[:price],
|
16
|
+
currency_code: ml_product[:currency_id], sold_quantity: ml_product[:sold_quantity],
|
17
|
+
description: ml_product[:description], status: ml_product[:status],
|
18
|
+
latitude: ml_product.dig(:geolocation, :latitude), longitude: ml_product.dig(:geolocation, :longitude)
|
19
|
+
)
|
20
|
+
|
21
|
+
ml_product[:pictures].each do |picture|
|
22
|
+
MercadolibreRails::Picture.where(url: picture[:url], product_id: product_id).first_or_create
|
23
|
+
end
|
24
|
+
end
|
25
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module MercadolibreRails
|
2
|
+
class SyncMercadolibreSellerJob < ApplicationJob
|
3
|
+
queue_as :default
|
4
|
+
|
5
|
+
def perform(seller_id)
|
6
|
+
seller = MercadolibreRails::Seller.find(seller_id)
|
7
|
+
|
8
|
+
ordered_products = MercadolibreApi::Sellers::Products::Queries::MostSold.run!(
|
9
|
+
seller_id: seller.mercadolibre_id, site_code: seller.site_code
|
10
|
+
)
|
11
|
+
|
12
|
+
ordered_products.each { |product| seller.products.where(mercadolibre_id: product[:id]).first_or_create }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -13,33 +13,24 @@ module MercadolibreRails
|
|
13
13
|
validates_uniqueness_of :mercadolibre_id, scope: :site_id
|
14
14
|
|
15
15
|
delegate :code, to: :site, prefix: true
|
16
|
-
after_create_commit :update_metadata
|
17
16
|
has_many :pictures, dependent: :destroy, class_name: 'MercadolibreRails::Picture'
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
class << self
|
19
|
+
def create_from(url:)
|
20
|
+
ml_id = MercadolibreApi::Products::Queries::GetID.run!(product_url: url)
|
21
|
+
site_code = MercadolibreApi::Sites::Queries::GetCode.run!(product_url: url)
|
22
22
|
|
23
|
-
|
23
|
+
return unless ml_id && site_code
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
# rubocop:disable Metrics/AbcSize
|
30
|
-
def update_metadata
|
31
|
-
ml_product = MercadolibreApi::Products::Queries::Find.run!(product_id: mercadolibre_id)
|
32
|
-
ml_seller = MercadolibreRails::Seller.where(mercadolibre_id: ml_product[:seller_id], site: site).first_or_create
|
25
|
+
mercadolibre_site = MercadolibreRails::Site.where(code: site_code).first_or_create
|
26
|
+
product = mercadolibre_site.products.where(mercadolibre_id: ml_id).first_or_create
|
33
27
|
|
34
|
-
|
35
|
-
|
36
|
-
description: ml_product[:description], status: ml_product[:status],
|
37
|
-
latitude: ml_product.dig(:geolocation, :latitude), longitude: ml_product.dig(:geolocation, :longitude))
|
28
|
+
MercadolibreRails::SyncMercadolibreProductJob.perform_now(product.id)
|
29
|
+
end
|
38
30
|
|
39
|
-
|
40
|
-
MercadolibreRails::
|
31
|
+
def sync_all
|
32
|
+
all.each { |product| MercadolibreRails::SyncMercadolibreProductJob.perform_now(product.id) }
|
41
33
|
end
|
42
34
|
end
|
43
|
-
# rubocop:enable Metrics/AbcSize
|
44
35
|
end
|
45
36
|
end
|
@@ -6,25 +6,22 @@ module MercadolibreRails
|
|
6
6
|
belongs_to :site
|
7
7
|
|
8
8
|
delegate :code, to: :site, prefix: true
|
9
|
-
after_create_commit :create_products
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
class << self
|
11
|
+
def create_from(product_url:)
|
12
|
+
ml_product_id = MercadolibreApi::Products::Queries::GetID.run!(product_url: product_url)
|
13
|
+
ml_seller_id = MercadolibreApi::Sellers::Queries::GetID.run!(product_id: ml_product_id)
|
14
|
+
ml_site_code = MercadolibreApi::Sites::Queries::GetCode.run!(product_url: product_url)
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
16
|
+
ml_site = MercadolibreRails::Site.where(code: ml_site_code).first_or_create
|
17
|
+
seller = where(mercadolibre_id: ml_seller_id, site: ml_site).first_or_create
|
21
18
|
|
22
|
-
|
23
|
-
|
24
|
-
seller_id: mercadolibre_id, site_code: site_code
|
25
|
-
)
|
19
|
+
MercadolibreRails::SyncMercadolibreSellerJob.perform_now(seller.id)
|
20
|
+
end
|
26
21
|
|
27
|
-
|
22
|
+
def sync_all
|
23
|
+
all.each { |seller| MercadolibreRails::SyncMercadolibreSellerJob.perform_now(seller.id) }
|
24
|
+
end
|
28
25
|
end
|
29
26
|
end
|
30
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mercadolibre_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.7'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pablo Gonzaga
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mercadolibre_api
|
@@ -166,6 +166,8 @@ files:
|
|
166
166
|
- app/controllers/mercadolibre_rails/application_controller.rb
|
167
167
|
- app/helpers/mercadolibre_rails/application_helper.rb
|
168
168
|
- app/jobs/mercadolibre_rails/application_job.rb
|
169
|
+
- app/jobs/mercadolibre_rails/sync_mercadolibre_product_job.rb
|
170
|
+
- app/jobs/mercadolibre_rails/sync_mercadolibre_seller_job.rb
|
169
171
|
- app/mailers/mercadolibre_rails/application_mailer.rb
|
170
172
|
- app/models/mercadolibre_rails/application_record.rb
|
171
173
|
- app/models/mercadolibre_rails/picture.rb
|