Sha256: 3eece7faa9ce652b186c158518ffb2c25920e2124bb9e4853256cac651f04163

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

module SecondAmendmentWholesale
  class Category < Base

    include SecondAmendmentWholesale::API

    #Excludes brands and other non-categories returned from 2AW's Categories endpoint
    EXCLUDED_CATEGORY_IDS = [3, 26, 27]

    def initialize(options = {})
      requires!(options, :token)

      @options = options
    end

    def self.all(options = {})
      requires!(options, :token)

      new(options).all
    end

    def all
      headers = [ 
        *auth_header(@options[:token]),
        *content_type_header('application/json'),
      ].to_h

      response = get_request('categories', headers)

      extract_categories(response.body[:children_data])
    end

    protected

    def extract_categories(response, categories = [])
      response.each do |category|
        unless EXCLUDED_CATEGORY_IDS.include?(category[:id])
          categories << map_hash(category) unless category[:name] == "Firearms"

          if category[:children_data].present?
            extract_categories(category[:children_data], categories)
          end
        end
      end

      categories
    end

    def map_hash(hash)
      {
        id: hash[:id],
        name: hash[:name],
        parent_id: hash[:parent_id],
      }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
second_amendment_wholesale-1.0.5 lib/second_amendment_wholesale/category.rb
second_amendment_wholesale-1.0.3 lib/second_amendment_wholesale/category.rb
second_amendment_wholesale-1.0.2 lib/second_amendment_wholesale/category.rb
second_amendment_wholesale-1.0.1 lib/second_amendment_wholesale/category.rb
second_amendment_wholesale-1.0.0 lib/second_amendment_wholesale/category.rb