Class: ASIN::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/asin/configuration.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.associate_tagObject

Returns the value of attribute associate_tag.



10
11
12
# File 'lib/asin/configuration.rb', line 10

def associate_tag
  @associate_tag
end

.cart_typeObject

Returns the value of attribute cart_type.



9
10
11
# File 'lib/asin/configuration.rb', line 9

def cart_type
  @cart_type
end

.hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/asin/configuration.rb', line 8

def host
  @host
end

.item_typeObject

Returns the value of attribute item_type.



9
10
11
# File 'lib/asin/configuration.rb', line 9

def item_type
  @item_type
end

.keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/asin/configuration.rb', line 8

def key
  @key
end

.loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/asin/configuration.rb', line 8

def logger
  @logger
end

.node_typeObject

Returns the value of attribute node_type.



9
10
11
# File 'lib/asin/configuration.rb', line 9

def node_type
  @node_type
end

.secretObject

Returns the value of attribute secret.



8
9
10
# File 'lib/asin/configuration.rb', line 8

def secret
  @secret
end

.versionObject

Returns the value of attribute version.



10
11
12
# File 'lib/asin/configuration.rb', line 10

def version
  @version
end

Class Method Details

.blank?(key) ⇒ Boolean

Check if a key is set

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/asin/configuration.rb', line 83

def blank?(key)
  val = self.send :key
  val.nil? || val.empty?
end

.configure(options = {}) ⇒ Object

Rails initializer configuration.

Expects at least secret and key for the API call:

ASIN::Configuration.configure do |config|
  config.secret = 'your-secret'
  config.key    = 'your-key'
end

With the latest version of the Product Advertising API you need to include your associate_tag.

You may pass options as a hash as well:

ASIN::Configuration.configure :secret => 'your-secret', :key => 'your-key'

Or configure everything using YAML:

ASIN::Configuration.configure :yaml => 'config/asin.yml'

ASIN::Configuration.configure :yaml => 'config/asin.yml' do |config, yml|
  config.key = yml[Rails.env]['aws_access_key']
end

Options:

secret

the API secret key (required)

key

the API access key (required)

associate_tag

your Amazon associate tag. Default is blank (required in latest API version)

host

the host, which defaults to ‘webservices.amazon.com’

logger

a different logger than logging to STDERR (nil for no logging)

version

a custom version of the API calls. Default is 2010-11-01

item_type

a different class for SimpleItem, use :mash / :rash for Hashie::Mash / Hashie::Rash or :raw for a plain hash

cart_type

a different class for SimpleCart, use :mash / :rash for Hashie::Mash / Hashie::Rash or :raw for a plain hash

node_type

a different class for SimpleNode, use :mash / :rash for Hashie::Mash / Hashie::Rash or :raw for a plain hash



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/asin/configuration.rb', line 47

def configure(options={})
  init_config
  if yml_path = options[:yaml] || options[:yml]
    yml = File.open(yml_path) { |file| YAML.load(file) }
    if block_given?
      yield self, yml
    else
      yml.each do |key, value|
        send(:"#{key}=", value)
      end
    end
  elsif block_given?
    yield self
  else
    options.each do |key, value|
      send(:"#{key}=", value)
    end
  end
  self
end

.init_config(force = false) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/asin/configuration.rb', line 90

def init_config(force=false)
  return if @init && !force
  @init          = true
  @secret        = ''
  @key           = ''
  @host          = 'webservices.amazon.com'
  @logger        = Logger.new(STDERR)
  @item_type     = SimpleItem
  @cart_type     = SimpleCart
  @node_type     = SimpleNode
  @version       = '2010-11-01'
  @associate_tag = ''
end

.resetObject

Resets configuration to defaults



77
78
79
# File 'lib/asin/configuration.rb', line 77

def reset
  init_config(true)
end

.validate_credentials!Object

Checks if given credentials are valid and raises an error if not.



70
71
72
73
# File 'lib/asin/configuration.rb', line 70

def validate_credentials!
  raise "you have to configure ASIN: 'configure :secret => 'your-secret', :key => 'your-key'" if blank?(:secret) || blank?(:key)
  [:host, :item_type, :cart_type, :node_type, :version, :associate_tag].each { |item| raise "nil is not a valid value for #{item}" unless self.send item }
end