Sha256: 057832295e80622a067489bac6adb9ba32695528977f19383594cc46058626e7

Contents?: true

Size: 1.66 KB

Versions: 7

Compression:

Stored size: 1.66 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 #:nodoc:
      { :product_sku => GeoCerts.escape(self.sku) }
    end
    
  end
  
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
geocerts-1.0.1 lib/geo_certs/product.rb
geocerts-1.0.0 lib/geo_certs/product.rb
geocerts-0.0.25 lib/geo_certs/product.rb
geocerts-0.0.24 lib/geo_certs/product.rb
geocerts-0.0.23 lib/geo_certs/product.rb
geocerts-0.0.22 lib/geo_certs/product.rb
geocerts-0.0.21 lib/geo_certs/product.rb