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.



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

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

Class Method Details

.all(options, &block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/rakuten_web_service/resource.rb', line 32

def all(options, &block)
  if block
    search(options).all(&block)
  else
    search(options).all
  end
end

.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



52
53
54
# File 'lib/rakuten_web_service/resource.rb', line 52

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

.endpoint(url = nil) ⇒ Object



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

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

.resource_nameObject



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

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



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

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

.set_resource_name(name) ⇒ Object



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

def set_resource_name(name)
  @resource_name = name
end

Instance Method Details

#[](key) ⇒ Object



68
69
70
71
# File 'lib/rakuten_web_service/resource.rb', line 68

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

#get_attribute(name) ⇒ Object



73
74
75
# File 'lib/rakuten_web_service/resource.rb', line 73

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