mercadolibre_rails 0.5 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d4534f513781489dd5f68379a0e10e95370918f8376b2820de504329d16655f5
4
- data.tar.gz: 5f0209ae2ed18ccd17b24c74e9681cef0e3d7705301d2bc5d7fa8c4b64008f8f
3
+ metadata.gz: df8b324f21f677cc5b8f45c6a37d4511523f076e99f816ac2dc5f2b98e3f6a0c
4
+ data.tar.gz: e717205170575157ecaee610d9f85cd6a82bab9003ddeb7b19cce045f3415c17
5
5
  SHA512:
6
- metadata.gz: 8ff935a2e2005a6f7b5d885fe79a60398b98a450eda2c30c5121d2883b62c87d19bd62b09b0fe3627aa088df82e3a164ab37470cca51a6489e3ec2e1fc643832
7
- data.tar.gz: c312ccc37ca12ad892dbd1ee9bdb70a40debbab1910f36a916e039981377e175248abc23ca175b77fc0e0de29cceae6584e62f75413648e4e40af2d299a8904b
6
+ metadata.gz: c8e100fa61d958425a3caf1e9e327469a412f10020df038f4a413ebc118118e25dd04773236e8310f39b8b8be8f4bd1dec750d6c1c2ca4e5662078af6b17f792
7
+ data.tar.gz: a8ece76dfaa42f9c6eca0db7ab4991dc77804416e568c01d9ef2e0009a4603345a12edf513dceabe552e3d51ee05c8c1b593fe2acbf501b57217b86c148923e7
@@ -22,14 +22,14 @@ module MercadolibreRails
22
22
 
23
23
  return unless ml_id && site_code
24
24
 
25
- mercadolibre_site = MercadolibreRails::Site.find_or_create_by!(code: site_code)
26
- mercadolibre_site.products.find_or_create_by!(mercadolibre_id: ml_id)
25
+ mercadolibre_site = MercadolibreRails::Site.where(code: site_code).first_or_create
26
+ mercadolibre_site.products.where(mercadolibre_id: ml_id).first_or_create
27
27
  end
28
28
 
29
29
  # rubocop:disable Metrics/AbcSize
30
30
  def update_metadata
31
31
  ml_product = MercadolibreApi::Products::Queries::Find.run!(product_id: mercadolibre_id)
32
- ml_seller = MercadolibreRails::Seller.find_or_create_by!(mercadolibre_id: ml_product[:seller_id])
32
+ ml_seller = MercadolibreRails::Seller.where(mercadolibre_id: ml_product[:seller_id], site: site).first_or_create
33
33
 
34
34
  update!(seller: ml_seller, title: ml_product[:title], price: ml_product[:price],
35
35
  currency_code: ml_product[:currency_id], sold_quantity: ml_product[:sold_quantity],
@@ -37,7 +37,7 @@ module MercadolibreRails
37
37
  latitude: ml_product.dig(:geolocation, :latitude), longitude: ml_product.dig(:geolocation, :longitude))
38
38
 
39
39
  ml_product[:pictures].each do |picture|
40
- MercadolibreRails::Picture.find_or_create_by!(url: picture[:url], product_id: id)
40
+ MercadolibreRails::Picture.where(url: picture[:url], product_id: id).first_or_create
41
41
  end
42
42
  end
43
43
  # rubocop:enable Metrics/AbcSize
@@ -3,12 +3,28 @@ require 'mercadolibre_api'
3
3
  module MercadolibreRails
4
4
  class Seller < ApplicationRecord
5
5
  has_many :products, dependent: :destroy
6
+ belongs_to :site
7
+
8
+ delegate :code, to: :site, prefix: true
9
+ after_create_commit :create_products
6
10
 
7
11
  def self.create_from(product_url:)
8
12
  ml_product_id = MercadolibreApi::Products::Queries::GetID.run!(product_url: product_url)
9
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
+
16
+ ml_site = MercadolibreRails::Site.where(code: ml_site_code).first_or_create
17
+ where(mercadolibre_id: ml_seller_id, site: ml_site).first_or_create
18
+ end
19
+
20
+ private
21
+
22
+ def create_products
23
+ ordered_products = MercadolibreApi::Sellers::Products::Queries::MostSold.run!(
24
+ seller_id: mercadolibre_id, site_code: site_code
25
+ )
10
26
 
11
- find_or_create_by!(mercadolibre_id: ml_seller_id)
27
+ ordered_products.each { |product| products.where(mercadolibre_id: product[:id]).first_or_create }
12
28
  end
13
29
  end
14
30
  end
@@ -1,6 +1,7 @@
1
1
  module MercadolibreRails
2
2
  class Site < ApplicationRecord
3
3
  has_many :products, dependent: :destroy
4
+ has_many :sellers, dependent: :destroy
4
5
 
5
6
  validates_presence_of :code
6
7
  validates_uniqueness_of :code
@@ -1,7 +1,7 @@
1
1
  class CreateMercadolibreRailsSellers < ActiveRecord::Migration[5.2]
2
2
  def change
3
3
  create_table :mercadolibre_rails_sellers do |t|
4
- t.integer :mercadolibre_id, null: false, index: { unique: true }
4
+ t.integer :mercadolibre_id, null: false
5
5
 
6
6
  t.timestamps
7
7
  end
@@ -0,0 +1,6 @@
1
+ class AddSiteIdToMercadolibreRailsSellers < ActiveRecord::Migration[5.2]
2
+ def change
3
+ add_reference :mercadolibre_rails_sellers, :site, foreign_key: true
4
+ add_index :mercadolibre_rails_sellers, %i[mercadolibre_id site_id], name: 'ml_id_site_id_on_sellers'
5
+ end
6
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MercadolibreRails
4
- VERSION = '0.5'
4
+ VERSION = '0.6'
5
5
  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.5'
4
+ version: '0.6'
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-22 00:00:00.000000000 Z
11
+ date: 2018-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mercadolibre_api
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.9'
19
+ version: '0.10'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.9'
26
+ version: '0.10'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: paper_trail
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -184,6 +184,7 @@ files:
184
184
  - db/migrate/20181010142220_create_mercadolibre_rails_versions.rb
185
185
  - db/migrate/20181010142221_add_object_changes_to_mercadolibre_rails_versions.rb
186
186
  - db/migrate/20181012133312_create_mercadolibre_rails_pictures.rb
187
+ - db/migrate/20181022135655_add_site_id_to_mercadolibre_rails_sellers.rb
187
188
  - lib/mercadolibre_rails.rb
188
189
  - lib/mercadolibre_rails/engine.rb
189
190
  - lib/mercadolibre_rails/version.rb