Class: Lastfm::MethodCategory::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/lastfm/method_category/base.rb

Direct Known Subclasses

Album, Artist, Auth, Chart, Event, Geo, Library, Tag, Tasteometer, Track, User

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lastfm) ⇒ Base

Returns a new instance of Base.



34
35
36
# File 'lib/lastfm/method_category/base.rb', line 34

def initialize(lastfm)
  @lastfm = lastfm
end

Class Method Details

.__define_method(method, id, mandatory, optional, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/lastfm/method_category/base.rb', line 23

def __define_method(method, id, mandatory, optional, &block)
  unless block
    block = Proc.new { |response| response.xml }
  end

  define_method(id) do |*args|
    block.call(send(method, id.to_s.camelize(:lower), Lastfm::Util.build_options(args, mandatory, optional)))
  end
end

.method_for_authentication(id, mandatory, optional = [], &block) ⇒ Object



15
16
17
# File 'lib/lastfm/method_category/base.rb', line 15

def method_for_authentication(id, mandatory, optional = [], &block)
  __define_method(:request_for_authentication, id, mandatory, optional, &block)
end

.method_with_authentication(id, mandatory, optional = [], &block) ⇒ Object



11
12
13
# File 'lib/lastfm/method_category/base.rb', line 11

def method_with_authentication(id, mandatory, optional = [], &block)
  __define_method(:request_with_authentication, id, mandatory, optional, &block)
end

.regular_method(id, mandatory, optional = [], &block) ⇒ Object



19
20
21
# File 'lib/lastfm/method_category/base.rb', line 19

def regular_method(id, mandatory, optional = [], &block)
  __define_method(:request, id, mandatory, optional, &block)
end

.write_method(id, mandatory, optional = []) ⇒ Object



5
6
7
8
9
# File 'lib/lastfm/method_category/base.rb', line 5

def write_method(id, mandatory, optional = [])
  __define_method(:write_request, id, mandatory, optional) do |response|
    response.success?
  end
end

Instance Method Details

#request(*args) ⇒ Object



50
51
52
53
54
55
# File 'lib/lastfm/method_category/base.rb', line 50

def request(*args)
  method, *rest = args
  method = [self.class.name.split(/::/).last.downcase, method].join('.')

  @lastfm.request(method, *rest)
end

#request_for_authentication(method, params = {}) ⇒ Object



46
47
48
# File 'lib/lastfm/method_category/base.rb', line 46

def request_for_authentication(method, params = {})
  request(method, params, :get, true)
end

#request_with_authentication(method, params = {}) ⇒ Object



42
43
44
# File 'lib/lastfm/method_category/base.rb', line 42

def request_with_authentication(method, params = {})
  request(method, params, :get, true, true)
end

#write_request(method, params = {}) ⇒ Object



38
39
40
# File 'lib/lastfm/method_category/base.rb', line 38

def write_request(method, params = {})
  request(method, params, :post, true, true)
end