class RakutenWebService::Resource

Attributes

resource_name[W]
params[R]

Public Class Methods

all(options, &block) click to toggle source
# File lib/rakuten_web_service/resource.rb, line 41
def all(options, &block)
  if block
    search(options).all(&block)
  else
    search(options).all
  end
end
attribute(*attribute_names) click to toggle source
# File lib/rakuten_web_service/resource.rb, line 26
def attribute(*attribute_names)
  attribute_names.each do |attribute_name|
    attribute_name = attribute_name.to_s

    define_getter_for_attribute(attribute_name)
    next unless attribute_name.end_with?('Flag')

    define_asking_method_for_attribute(attribute_name)
  end
end
endpoint(url = nil) click to toggle source
# File lib/rakuten_web_service/resource.rb, line 53
def endpoint(url = nil)
  @endpoint = url || @endpoint
end
inherited(subclass) click to toggle source
# File lib/rakuten_web_service/resource.rb, line 17
def inherited(subclass)
  @@subclasses ||= []
  @@subclasses.push(subclass)
end
new(params) click to toggle source
# File lib/rakuten_web_service/resource.rb, line 85
def initialize(params)
  @params = {}
  params.each { |k, v| @params[k.to_s] = v }
end
parser(&block) click to toggle source
# File lib/rakuten_web_service/resource.rb, line 57
def parser(&block)
  instance_eval do
    define_singleton_method :parse_response, block
  end
end
resource_name() click to toggle source
# File lib/rakuten_web_service/resource.rb, line 49
def resource_name
  @resource_name ||= name.split('::').last.downcase
end
subclasses() click to toggle source
# File lib/rakuten_web_service/resource.rb, line 22
def subclasses
  @@subclasses || []
end

Private Class Methods

define_asking_method_for_attribute(attribute_name) click to toggle source
# File lib/rakuten_web_service/resource.rb, line 74
def define_asking_method_for_attribute(attribute_name)
  method_name = attribute_name.to_snake
  method_name.sub!(/^#{resource_name}_(\w+)$/, '\1')
  method_name.sub!(/(.+)_flag$/, '\1')

  define_method "#{method_name}?" do
    get_attribute(attribute_name) == 1
  end
end
define_getter_for_attribute(attribute_name) click to toggle source
# File lib/rakuten_web_service/resource.rb, line 65
def define_getter_for_attribute(attribute_name)
  method_name = attribute_name.to_snake
  method_name.sub!(/^#{resource_name}_(\w+)$/, '\1')

  define_method method_name do
    get_attribute(attribute_name)
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/rakuten_web_service/resource.rb, line 103
def ==(other)
  raise ArgumentError unless other.is_a?(RakutenWebService::Resource)

  params.keys.all? do |k|
    @params[k] == other.params[k]
  end
end
[](key) click to toggle source
# File lib/rakuten_web_service/resource.rb, line 90
def [](key)
  camel_key = key.to_camel
  @params[key] || @params[camel_key]
end
attributes() click to toggle source
# File lib/rakuten_web_service/resource.rb, line 99
def attributes
  params.keys
end
get_attribute(name) click to toggle source
# File lib/rakuten_web_service/resource.rb, line 95
def get_attribute(name)
  @params[name.to_s]
end