Sha256: 87661ff02c8fb177fee436abf9ec0b7a72a4b86f6e62d91be7252da088a6d2f7

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

require 'rakuten_web_service/search_result'

module RakutenWebService
  class Resource
    class << self
      def attribute(*attributes)
        attributes.each do |attribute|
          method_name = attribute.to_s.gsub(/([a-z]+)([A-Z]{1})/) do
            "#{$1}_#{$2.downcase}"
          end
          method_name = method_name.sub(/^#{resource_name}_(\w+)$/) { $1 }
          self.class_eval <<-CODE
            def #{method_name}
              @params['#{attribute.to_s}']
            end
          CODE
        end
      end

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

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

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

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

    def initialize(params)
      @params = params
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rakuten_web_service-0.1.1 lib/rakuten_web_service/resource.rb
rakuten_web_service-0.1.0 lib/rakuten_web_service/resource.rb
rakuten_web_service-0.0.1 lib/rakuten_web_service/resource.rb