Sha256: 98f889ba16cdb16c8ca02bb2b131331dfc03feac04a3dab7e4bc17bc8edaaf6e

Contents?: true

Size: 1.65 KB

Versions: 10

Compression:

Stored size: 1.65 KB

Contents

require 'geo_certs/api_object'

module GeoCerts
  
  ##
  # The Product simply holds details about a specific product offered by GeoCerts.
  # 
  class Product < ApiObject
    
    attr_accessor :name, :sku, :max_years
    
    
    ##
    # Returns all products available via the GeoCerts API.
    # 
    def self.all
      response = call_api { GeoCerts.api.products }
      build_collection(response) { |response| response[:products][:product] }
    end
    
    ##
    # Returns a GeoCerts product by product SKU.
    # 
    # === Exceptions
    # 
    # If the +sku+ cannot be located in the GeoCerts system, a GeoCerts::ResourceNotFound 
    # exception is raised.
    # 
    def self.find(sku)
      all.detect { |product| product.sku == sku } || raise(GeoCerts::ResourceNotFound, "No product was found matching \"#{sku}\" sku")
    end
    
    ##
    # Returns a GeoCerts product by product SKU.
    # 
    # This method returns +nil+ if the SKU cannot be found on the GeoCerts system and does not
    # raise an exception for this event.
    # 
    def self.find_by_sku(sku)
      find(sku)
    rescue GeoCerts::AllowableExceptionWithResponse
      nil
    end
    
    
    def initialize(attributes = {})
      self.name       = attributes[:name]
      self.sku        = attributes[:sku]
      self.max_years  = attributes[:max_years]
    end
    
    ##
    # Returns the GeoCerts::Agreement for this product.
    # 
    def user_agreement
      GeoCerts::Agreement.new(self.class.call_api {
        GeoCerts.api.agreement(:product_id => self.sku)
      })
    end
    
    def to_geocerts_hash
      { :product_sku => GeoCerts.escape(self.sku) }
    end
    
  end
  
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
geocerts-0.0.20 lib/geo_certs/product.rb
geocerts-0.0.19 lib/geo_certs/product.rb
geocerts-0.0.18 lib/geo_certs/product.rb
geocerts-0.0.17 lib/geo_certs/product.rb
geocerts-0.0.16 lib/geo_certs/product.rb
geocerts-0.0.15 lib/geo_certs/product.rb
geocerts-0.0.14 lib/geo_certs/product.rb
geocerts-0.0.13 lib/geo_certs/product.rb
geocerts-0.0.12 lib/geo_certs/product.rb
geocerts-0.0.11 lib/geo_certs/product.rb