Sha256: bdecc8cfe254d6017cf15b8e2dfa09df8a6bc0fd523491e5e3727b5e5bfa9525

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'active_interaction'
require 'net/http'

module MercadolibreApi
  module Sellers
    module Products
      module Queries
        class MostSold < ActiveInteraction::Base
          integer :seller_id
          string :site_code

          def execute
            results = search_uris.map do |uri|
              response = Net::HTTP.get_response(uri)
              JSON.parse(response.body, symbolize_names: true)[:results]
            end

            results.flatten.map { |r| MercadolibreApi::Products::Entities::Product.represent(r).as_json }
          end

          private

          def search_uris
            response = Net::HTTP.get_response(base_uri)
            response_data = JSON.parse(response.body, symbolize_names: true)

            return [] unless response.code == '200'

            total_products = response_data.dig(:paging, :total)
            product_limit = response_data.dig(:paging, :limit)
            total_calls = (total_products / product_limit).to_i

            0.upto(total_calls).map { |offset_ratio| base_uri(offset: (product_limit * offset_ratio)) }
          end

          def base_uri(offset: 0)
            return URI("#{sites_api_url}/#{site_code}/search?seller_id=#{seller_id}") unless offset.positive?

            URI("#{sites_api_url}/#{site_code}/search?seller_id=#{seller_id}&offset=#{offset}")
          end

          def sites_api_url
            'https://api.mercadolibre.com/sites'
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mercadolibre_api-0.10 lib/mercadolibre_api/sellers/products/queries/most_sold.rb