Class: RakutenWebService::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/rakuten_web_service/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Resource

Returns a new instance of Resource.



55
56
57
58
# File 'lib/rakuten_web_service/resource.rb', line 55

def initialize(params)
  @params = params.dup
  params.each { |k, v| @params[k.to_s] = v }
end

Class Method Details

.attribute(*attributes) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rakuten_web_service/resource.rb', line 7

def attribute(*attributes)
  attributes.each do |attribute|
    method_name = attribute.to_s.gsub(/([a-z]+)([A-Z]{1})/) do |matched|
      "#{$1}_#{$2}"
    end.downcase
    method_name = method_name.sub(/^#{resource_name}_(\w+)$/) { $1 }
    instance_eval do
      define_method method_name do
        get_attribute(attribute.to_s)
      end
    end
    if method_name =~ /(.+)_flag$/
      instance_eval do
        define_method "#{$1}?" do
          get_attribute(attribute.to_s) == 1
        end
      end
    end
  end
end

.clientObject



44
45
46
# File 'lib/rakuten_web_service/resource.rb', line 44

def client
  @client ||= RakutenWebService::Client.new(endpoint)
end

.endpoint(url = nil) ⇒ Object



40
41
42
# File 'lib/rakuten_web_service/resource.rb', line 40

def endpoint(url=nil)
  @endpoint = url || @endpoint 
end

.resource_nameObject



32
33
34
# File 'lib/rakuten_web_service/resource.rb', line 32

def resource_name
  @resource_name ||= self.name.split('::').last.downcase
end

.search(options) ⇒ Object



28
29
30
# File 'lib/rakuten_web_service/resource.rb', line 28

def search(options)
  SearchResult.new(options, self)
end

.set_parser(&block) ⇒ Object



48
49
50
51
52
# File 'lib/rakuten_web_service/resource.rb', line 48

def set_parser(&block)
  instance_eval do
    define_singleton_method :parse_response, block
  end
end

.set_resource_name(name) ⇒ Object



36
37
38
# File 'lib/rakuten_web_service/resource.rb', line 36

def set_resource_name(name)
  @resource_name = name
end

Instance Method Details

#[](key) ⇒ Object



60
61
62
63
# File 'lib/rakuten_web_service/resource.rb', line 60

def [](key)
  camel_key = key.gsub(/([a-z]+)_(\w{1})/) { "#{$1}#{$2.capitalize}" }
  @params[key] || @params[camel_key]
end

#get_attribute(name) ⇒ Object



65
66
67
# File 'lib/rakuten_web_service/resource.rb', line 65

def get_attribute(name)
  @params[name.to_s]
end