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

#initialize(*args) ⇒ Resource

Returns a new instance of Resource



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

def initialize(*args)
  super
end

Class Method Details

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

Methods for reading or writing this resource



154
155
156
157
158
159
160
161
162
163
164
165
166
# 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 <<-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

#address(working_address = nil) ⇒ Object

Returns an Address object refering to this resource



82
83
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
# 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 = {}) ⇒ Object

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



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

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

#dereference_with_context(child) ⇒ Object



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

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

#each_http_methodObject

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



137
138
139
# 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) ⇒ Object



131
132
133
134
# 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) ⇒ Object



147
148
149
150
151
# 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) ⇒ Object



141
142
143
144
145
# 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) ⇒ Object



124
125
126
127
128
129
# 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) ⇒ Object



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

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

#uri(args = {}, working_address = nil) ⇒ Object



77
78
79
# 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') ⇒ Object

Sets basic auth parameters



61
62
63
64
# 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) ⇒ Object

Sets OAuth parameters

Args:

:consumer_key
:consumer_secret
:access_token
:token_secret


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

def with_oauth(*args)
  resource_and_address.auth(*HTTPRequest.oauth_header(args))
end