class WADL::Resource

Public Class Methods

define_http_methods(klass = self, methods = %w[head get post put delete]) click to toggle source

Methods for reading or writing this resource

# File lib/wadl/resource.rb, line 154
    def self.define_http_methods(klass = self, methods = %w[head get post put delete])
      methods.each { |method|
        klass.class_eval "          def #{method}(*args, &block)
            if method = find_method_by_http_method(:#{method})
              method.call(self, *args, &block)
            else
              raise ArgumentError, 'Method not allowed ("#{method}")'
            end
          end
", __FILE__, __LINE__ + 1
      }
    end
new(*args) click to toggle source
Calls superclass method WADL::CheapSchema.new
# File lib/wadl/resource.rb, line 42
def initialize(*args)
  super
end

Public Instance Methods

address(working_address = nil) click to toggle source

Returns an Address object refering to this resource

# File lib/wadl/resource.rb, line 82
def address(working_address = nil)
  working_address &&= working_address.deep_copy

  working_address ||= if parent.respond_to?(:base)
    address = Address.new
    address.path_fragments << parent.base
    address
  else
    parent.address.deep_copy
  end

  working_address.path_fragments << path.dup

  # Install path, query, and header parameters in the Address. These
  # may override existing parameters with the same names, but if
  # you've got a WADL application that works that way, you should
  # have bound parameters to values earlier.
  new_path_fragments = []
  embedded_param_names = Set.new(Address.embedded_param_names(path))

  params.each { |param|
    name = param.name

    if embedded_param_names.include?(name)
      working_address.path_params[name] = param
    else
      if param.style == 'query'
        working_address.query_params[name] = param
      elsif param.style == 'header'
        working_address.header_params[name] = param
      else
        new_path_fragments << param
        working_address.path_params[name] = param
      end
    end
  }

  working_address.path_fragments << new_path_fragments unless new_path_fragments.empty?

  working_address
end
bind(args = {}) click to toggle source

Returns a ResourceAndAddress object bound to this resource and the given query variables.

# File lib/wadl/resource.rb, line 56
def bind(args = {})
  resource_and_address.bind!(args)
end
dereference_with_context(child) click to toggle source
# File lib/wadl/resource.rb, line 50
def dereference_with_context(child)
  resource_and_address(child, parent.address)
end
each_http_method() { |m| ... } click to toggle source

Find HTTP methods in this resource and in the mixed-in types

# File lib/wadl/resource.rb, line 137
def each_http_method
  [self, *resource_types].each { |t| t.http_methods.each { |m| yield m } }
end
find_by_id(id) click to toggle source
# File lib/wadl/resource.rb, line 131
def find_by_id(id)
  id = id.to_s
  resources.find { |r| r.dereference.id == id }
end
find_method_by_http_method(action) click to toggle source
# File lib/wadl/resource.rb, line 147
def find_method_by_http_method(action)
  action = action.to_s.downcase
  each_http_method { |m| return m if m.dereference.name.downcase == action }
  nil
end
find_method_by_id(id) click to toggle source
# File lib/wadl/resource.rb, line 141
def find_method_by_id(id)
  id = id.to_s
  each_http_method { |m| return m if m.dereference.id == id }
  nil
end
representation_for(http_method, request = true, all = false) click to toggle source
# File lib/wadl/resource.rb, line 124
def representation_for(http_method, request = true, all = false)
  method = find_method_by_http_method(http_method)
  representations = (request ? method.request : method.response).representations

  all ? representations : representations[0]
end
resource_and_address(child = self, *args) click to toggle source
# File lib/wadl/resource.rb, line 46
def resource_and_address(child = self, *args)
  ResourceAndAddress.new(child, *args)
end
uri(args = {}, working_address = nil) click to toggle source
# File lib/wadl/resource.rb, line 77
def uri(args = {}, working_address = nil)
  address(working_address).uri(args)
end
with_basic_auth(user, pass, header = 'Authorization') click to toggle source

Sets basic auth parameters

# File lib/wadl/resource.rb, line 61
def with_basic_auth(user, pass, header = 'Authorization')
  resource_and_address.auth(header,
    "Basic #{["#{user}:#{pass}"].pack('m')}")
end
with_oauth(*args) click to toggle source

Sets OAuth parameters

Args:

:consumer_key
:consumer_secret
:access_token
:token_secret
# File lib/wadl/resource.rb, line 73
def with_oauth(*args)
  resource_and_address.auth(*HTTPRequest.oauth_header(args))
end