Class: RakutenApi::Base::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/rakuten_api/base/params.rb

Constant Summary collapse

BASE_VALID_NAMES =
%w(applicationId affiliateId).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application_id = nil, affiliate_id = nil, invalid_params_action = nil) ⇒ Params

Initialize

Parameters:

  • application_id (String) (defaults to: nil)

    Rakuten Web Service applicationId If application_id is nil, application_id set to RakutenApi.config.application_id

  • affiliate_id (String) (defaults to: nil)

    Rakuten Web Service affiliateId If affiliate_id is nil, affiliate_id set to RakutenApi.config.affiliate_id

  • invalid_params_action (Symbol) (defaults to: nil)

    :raise or :stdout or :none If invalid_params_action is nil, invalid_params_action set to RakutenApi.config.invalid_params_action



15
16
17
18
# File 'lib/rakuten_api/base/params.rb', line 15

def initialize(application_id = nil, affiliate_id = nil, invalid_params_action = nil)
  init_params application_id || RakutenApi.config.application_id, affiliate_id || RakutenApi.config.affiliate_id
  @invalid_params_action = invalid_params_action || RakutenApi.config.invalid_params_action
end

Instance Attribute Details

#invalid_params_actionObject

Returns the value of attribute invalid_params_action.



6
7
8
# File 'lib/rakuten_api/base/params.rb', line 6

def invalid_params_action
  @invalid_params_action
end

Instance Method Details

#[](name) ⇒ Object



43
44
45
46
# File 'lib/rakuten_api/base/params.rb', line 43

def [](name)
  _name = normalize(name)
  @params[_name]
end

#[]=(name, value) ⇒ Object



48
49
50
# File 'lib/rakuten_api/base/params.rb', line 48

def []=(name, value)
  add_param(name, value)
end

#add_param(name, value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rakuten_api/base/params.rb', line 26

def add_param(name, value)
  _name = normalize(name)
  if valid_name?(_name)
    @params[_name] = value
  else
    if @invalid_params_action == :raise
      raise ::RakutenApi::Error.new('passed invalid param: ' + name.to_s)
    elsif @invalid_params_action == :stdout
      puts "Warning: " + name.to_s + ' is invalid name'
    end
  end
end

#init_params(application_id, affiliate_id) ⇒ Object



20
21
22
23
24
# File 'lib/rakuten_api/base/params.rb', line 20

def init_params(application_id, affiliate_id)
  @params = {}
  add_param('applicationId', application_id)
  add_param('affiliateId', affiliate_id)
end

#inspectObject



39
40
41
# File 'lib/rakuten_api/base/params.rb', line 39

def inspect
  @params
end

#normalize(name) ⇒ Object



62
63
64
65
66
# File 'lib/rakuten_api/base/params.rb', line 62

def normalize(name)
  name = name.to_s
  name = name.split('_').tap{|names| break names.shift + names.map(&:capitalize).join } if name =~ /.+\_.+/
  name
end

#to_hashObject



52
53
54
55
56
# File 'lib/rakuten_api/base/params.rb', line 52

def to_hash
  @params ||= {}
  @params.reject!{|k,v| v.nil? }
  @params
end

#valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rakuten_api/base/params.rb', line 58

def valid_name?(name)
  valid_names.include? name
end

#valid_namesObject



68
69
70
# File 'lib/rakuten_api/base/params.rb', line 68

def valid_names
  BASE_VALID_NAMES
end