Class: WADL::Resource

Inherits:
HasDocs show all
Includes:
ResourceContainer
Defined in:
lib/wadl/resource.rb

Direct Known Subclasses

ResourceAndAddress

Constant Summary

Constant Summary

Constants inherited from CheapSchema

CheapSchema::ATTRIBUTES

Instance Attribute Summary

Attributes inherited from CheapSchema

#attributes, #href, #index_key, #parent

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from ResourceContainer

#finalize_creation, #find_resource_by_path, #resource

Methods inherited from HasDocs

#define_singleton

Methods inherited from CheapSchema

as_collection, as_member, contents_are_mixed_data, #dereference, dereferencing_attr_accessor, dereferencing_instance_accessor, #each_attribute, #each_collection, #each_member, from_element, has_attributes, has_many, has_one, has_required, in_document, inherit, inherited, init, #matches?, may_be_reference, may_be_reference?, #paths, #to_s

Constructor Details

- (Resource) initialize(*args)

Returns a new instance of Resource



43
44
45
# File 'lib/wadl/resource.rb', line 43

def initialize(*args)
  super
end

Class Method Details

+ (Object) define_http_methods(klass = self, methods = %w[head get post put delete]))

Methods for reading or writing this resource



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/wadl/resource.rb', line 156

def self.define_http_methods(klass = self, methods = %w[head get post put delete])
  methods.each { |method|
    klass.class_eval <<-EOT, __FILE__, __LINE__ + 1
      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
    EOT
  }
end

Instance Method Details

- (Object) address(working_address = nil)

Returns an Address object refering to this resource



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/wadl/resource.rb', line 84

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

- (Object) bind(args = {})

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



57
58
59
# File 'lib/wadl/resource.rb', line 57

def bind(args = {})
  resource_and_address.bind!(args)
end

- (Object) dereference_with_context(child)



51
52
53
# File 'lib/wadl/resource.rb', line 51

def dereference_with_context(child)
  resource_and_address(child, parent.address)
end

- (Object) each_http_method

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



139
140
141
# File 'lib/wadl/resource.rb', line 139

def each_http_method
  [self, *resource_types].each { |t| t.http_methods.each { |m| yield m } }
end

- (Object) find_by_id(id)



133
134
135
136
# File 'lib/wadl/resource.rb', line 133

def find_by_id(id)
  id = id.to_s
  resources.find { |r| r.dereference.id == id }
end

- (Object) find_method_by_http_method(action)



149
150
151
152
153
# File 'lib/wadl/resource.rb', line 149

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

- (Object) find_method_by_id(id)



143
144
145
146
147
# File 'lib/wadl/resource.rb', line 143

def find_method_by_id(id)
  id = id.to_s
  each_http_method { |m| return m if m.dereference.id == id }
  nil
end

- (Object) representation_for(http_method, request = true, all = false)



126
127
128
129
130
131
# File 'lib/wadl/resource.rb', line 126

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

- (Object) resource_and_address(child = self, *args)



47
48
49
# File 'lib/wadl/resource.rb', line 47

def resource_and_address(child = self, *args)
  ResourceAndAddress.new(child, *args)
end

- (Object) uri(args = {}, working_address = nil)



79
80
81
# File 'lib/wadl/resource.rb', line 79

def uri(args = {}, working_address = nil)
  address(working_address).uri(args)
end

- (Object) with_basic_auth(user, pass, header = 'Authorization')

Sets basic auth parameters



62
63
64
65
# File 'lib/wadl/resource.rb', line 62

def with_basic_auth(user, pass, header = 'Authorization')
  resource_and_address.auth(header,
    "Basic #{["#{user}:#{pass}"].pack('m')}")
end

- (Object) with_oauth(*args)

Sets OAuth parameters

Args:

:consumer_key
:consumer_secret
:access_token
:token_secret


74
75
76
77
# File 'lib/wadl/resource.rb', line 74

def with_oauth(*args)
  resource_and_address.auth(HTTPMethod::OAUTH_HEADER,
    "#{HTTPMethod::OAUTH_PREFIX}#{args.to_yaml}")
end