Class: Maidcafe::API

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-maidcafe.rb

Constant Summary collapse

BASE_URL =
'http://moeten.info/maidcafe'

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



46
47
48
# File 'lib/ruby-maidcafe.rb', line 46

def method_missing(name, *args, &block)
  invoke name, args
end

Instance Method Details

#invoke(type, args = []) ⇒ Object



34
35
36
37
38
# File 'lib/ruby-maidcafe.rb', line 34

def invoke(type, args=[])
  url = request_url(type, args)
  dputs "access to #{url}"
  ResultSet.new(type, open(url) {|f| REXML::Document.new f})
end

#list(type) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby-maidcafe.rb', line 15

def list(type)
  case type
  when :type
    invoke 'list'
  when :tid, :prefecture
    invoke nil, :tid => 'list'
  when :cid, :category
    invoke nil, :cid => 'list'
  when :sid, :shop
    invoke nil, :sid => 'list'
  else
    throw ArgumentError.new
  end
end

#read(type, args) ⇒ Object



30
31
32
# File 'lib/ruby-maidcafe.rb', line 30

def read(type, args)
  open(request_url(type, args)) {|f| REXML::Document.new f}.write
end

#request_url(type, args) ⇒ Object



40
41
42
43
44
# File 'lib/ruby-maidcafe.rb', line 40

def request_url(type, args)
  type_arg = type ? "type=#{type}" : ''
  other_args = args.to_a.map{|e| "#{e[0]}=#{e[1]}"}.join '&'
  "#{BASE_URL}/?#{type_arg}&#{other_args}&m=api"
end