class MadCart::Store::BigCommerce

Public Instance Methods

products_count() click to toggle source
# File lib/mad_cart/store/big_commerce.rb, line 15
def products_count
  (parse_response { connection.get('products/count.json') })["count"]
end
valid?() click to toggle source
# File lib/mad_cart/store/big_commerce.rb, line 11
def valid?
  valid_by_path?('time.json')
end

Private Instance Methods

api_url_for(store_domain) click to toggle source
# File lib/mad_cart/store/big_commerce.rb, line 74
def api_url_for(store_domain)
 "https://#{store_domain}/api/v2/"
end
create_connection(args={}) click to toggle source
# File lib/mad_cart/store/big_commerce.rb, line 78
def create_connection(args={})
  Faraday.new(DEFAULT_CONNECTION_OPTIONS.merge(:url => api_url_for(args[:store_url]))) do |connection|
    connection.basic_auth(args[:username], args[:api_key])
    connection.response :json
    connection.adapter Faraday.default_adapter
  end
end
for_each(source, &block) click to toggle source
# File lib/mad_cart/store/big_commerce.rb, line 59
def for_each(source, &block)
  items = send(source, :min_id => 1)

  loop do
    items.each(&block)
    break if items.count < 50
    items = send(source, :min_id => items.last['id'] + 1 )
  end

end
get_customer_hashes() click to toggle source
# File lib/mad_cart/store/big_commerce.rb, line 53
def get_customer_hashes
  result = []
  for_each(:make_customer_request) {|c| result << c }
  result
end
get_products(options={}) click to toggle source
# File lib/mad_cart/store/big_commerce.rb, line 25
def get_products(options={})
  product_hashes = connection.get('products.json', options).try(:body)
  return [] unless product_hashes

  threads, images = [], []
  product_hashes.each do |product|
    threads << Thread.new do
      if product["images"]
        url = "#{product["images"]["resource"][1..-1]}.json"
        images << parse_response { connection.get(url) }
      end
    end
  end
  threads.each { |t| t.join }

  product_hashes.map do |p|
    product_images = images.find { |i| i.first.try(:[], 'product_id') == p['id'] } || []
    image          = product_images.sort_by{|i| i["sort_order"] }.find { |i| i["is_thumbnail"] }
    next if image.nil?

    p.merge({
      :url              => connection.build_url("#{p['custom_url']}").to_s,
      :image_square_url => image.try(:[], "thumbnail_url"),
      :image_url        => image.try(:[], "standard_url")
    })
  end.compact
end
get_store() click to toggle source
# File lib/mad_cart/store/big_commerce.rb, line 70
def get_store
  parse_response { connection.get('store.json') }
end
make_customer_request(params={:min_id => 1}) click to toggle source
# File lib/mad_cart/store/big_commerce.rb, line 21
def make_customer_request(params={:min_id => 1})
  parse_response { connection.get('customers.json', params) }
end