Sha256: f464370773544b648cc68ec2234c70a06aea091e7f0dba70f236f7a35edf0222

Contents?: true

Size: 1.69 KB

Versions: 27

Compression:

Stored size: 1.69 KB

Contents

class Lastfm
  module MethodCategory
    class Base
      class << self
        def write_method(id, mandatory, optional = [])
          __define_method(:write_request, id, mandatory, optional) do |response|
            response.success?
          end
        end

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

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

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

        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
      end

      def initialize(lastfm)
        @lastfm = lastfm
      end

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

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

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

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

        @lastfm.request(method, *rest)
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
lastfm-1.14.1 lib/lastfm/method_category/base.rb
lastfm-1.14.0 lib/lastfm/method_category/base.rb
lastfm-1.13.1 lib/lastfm/method_category/base.rb
lastfm-1.13.0 lib/lastfm/method_category/base.rb
lastfm-1.12.0 lib/lastfm/method_category/base.rb
lastfm-1.11.0 lib/lastfm/method_category/base.rb
lastfm-1.10.0 lib/lastfm/method_category/base.rb
lastfm-1.9.0 lib/lastfm/method_category/base.rb
lastfm-1.8.0 lib/lastfm/method_category/base.rb
lastfm-1.7.0 lib/lastfm/method_category/base.rb
lastfm-1.6.1 lib/lastfm/method_category/base.rb
lastfm-1.6.0 lib/lastfm/method_category/base.rb
lastfm-1.5.1 lib/lastfm/method_category/base.rb
lastfm-1.5.0 lib/lastfm/method_category/base.rb
lastfm-1.4.0 lib/lastfm/method_category/base.rb
lastfm-1.3.0 lib/lastfm/method_category/base.rb
lastfm-1.2.0 lib/lastfm/method_category/base.rb
lastfm-1.1.1 lib/lastfm/method_category/base.rb
lastfm-1.1.0 lib/lastfm/method_category/base.rb
lastfm-1.0.1 lib/lastfm/method_category/base.rb