Class: WADL::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/wadl/address.rb

Overview

The Address class keeps track of the user's path through a resource graph. Values for WADL parameters may be specified at any time using the bind method. An Address cannot be turned into a URI and header set until all required parameters have been bound to values.

An Address object is built up through calls to Resource#address

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_fragments = [], query_vars = [], headers = {}, path_params = {}, query_params = {}, header_params = {}) ⇒ Address



47
48
49
50
51
52
53
# File 'lib/wadl/address.rb', line 47

def initialize(path_fragments = [], query_vars = [], headers = {},
               path_params = {}, query_params = {}, header_params = {})
  @path_fragments, @query_vars, @headers = path_fragments, query_vars, headers
  @path_params, @query_params, @header_params = path_params, query_params, header_params

  @auth = {}
end

Instance Attribute Details

#header_paramsObject (readonly)

Returns the value of attribute header_params



40
41
42
# File 'lib/wadl/address.rb', line 40

def header_params
  @header_params
end

#headersObject (readonly)

Returns the value of attribute headers



40
41
42
# File 'lib/wadl/address.rb', line 40

def headers
  @headers
end

#path_fragmentsObject (readonly)

Returns the value of attribute path_fragments



40
41
42
# File 'lib/wadl/address.rb', line 40

def path_fragments
  @path_fragments
end

#path_paramsObject (readonly)

Returns the value of attribute path_params



40
41
42
# File 'lib/wadl/address.rb', line 40

def path_params
  @path_params
end

#query_paramsObject (readonly)

Returns the value of attribute query_params



40
41
42
# File 'lib/wadl/address.rb', line 40

def query_params
  @query_params
end

#query_varsObject (readonly)

Returns the value of attribute query_vars



40
41
42
# File 'lib/wadl/address.rb', line 40

def query_vars
  @query_vars
end

Class Method Details

.embedded_param_names(fragment) ⇒ Object



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

def self.embedded_param_names(fragment)
  fragment.scan(/\{(.+?)\}/).flatten
end

Instance Method Details

#auth(header, value) ⇒ Object



152
153
154
155
# File 'lib/wadl/address.rb', line 152

def auth(header, value)
  @auth[header] = value
  self
end

#bind!(args = {}) ⇒ Object

Binds some or all of the unbound variables in this address to values.



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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/wadl/address.rb', line 85

def bind!(args = {})
  path_var_values   = args[:path]    || {}
  query_var_values  = args[:query]   || {}
  header_var_values = args[:headers] || {}

  @auth.each { |header, value| header_var_values[header] = value }.clear

  # Bind variables found in the path fragments.
  path_params_to_delete = []

  path_fragments.each { |fragment|
    if fragment.respond_to?(:to_str)
      # This fragment is a string which might contain {} substitutions.
      # Make any substitutions available to the provided path variables.
      self.class.embedded_param_names(fragment).each { |name|
        value = path_var_values[name] || path_var_values[name.to_sym]

        value = if param = path_params[name]
          path_params_to_delete << param
          param % value
        else
          Param.default.format(value, name)
        end

        fragment.gsub!("{#{name}}", value)
      }
    else
      # This fragment is an array of Param objects (style 'matrix'
      # or 'plain') which may be bound to strings. As substitutions
      # happen, the array will become a mixed array of Param objects
      # and strings.
      fragment.each_with_index { |param, i|
        next unless param.respond_to?(:name)

        name = param.name

        value = path_var_values[name] || path_var_values[name.to_sym]
        value = param % value
        fragment[i] = value if value

        path_params_to_delete << param
      }
    end
  }

  # Delete any embedded path parameters that are now bound from
  # our list of unbound parameters.
  path_params_to_delete.each { |p| path_params.delete(p.name) }

  # Bind query variable values to query parameters
  query_var_values.each { |name, value|
    param = query_params.delete(name.to_s)
    query_vars << param % value if param
  }

  # Bind header variables to header parameters
  header_var_values.each { |name, value|
    if param = header_params.delete(name.to_s)
      headers[name] = param % value
    else
      warn %Q{Ignoring unknown header parameter "#{name}"!}
    end
  }

  self
end

#deep_copyObject

Perform a deep copy.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/wadl/address.rb', line 56

def deep_copy
  address = Address.new(
    _deep_copy_array(@path_fragments),
    _deep_copy_array(@query_vars),
    _deep_copy_hash(@headers),
    @path_params.dup,
    @query_params.dup,
    @header_params.dup
  )

  @auth.each { |header, value| address.auth(header, value) }

  address
end

#to_sObject Also known as: inspect



71
72
73
74
75
76
77
78
79
80
# File 'lib/wadl/address.rb', line 71

def to_s
  "Address:\n"                                            <<
  " Path fragments: #{@path_fragments.inspect}\n"         <<
  " Query variables: #{@query_vars.inspect}\n"            <<
  " Header variables: #{@headers.inspect}\n"              <<
  " Authorization parameters: #{@auth.inspect}\n"         <<
  " Unbound path parameters: #{@path_params.inspect}\n"   <<
  " Unbound query parameters: #{@query_params.inspect}\n" <<
  " Unbound header parameters: #{@header_params.inspect}\n"
end

#uri(args = {}) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/wadl/address.rb', line 157

def uri(args = {})
  obj, uri = deep_copy.bind!(args), ''

  # Build the path
  obj.path_fragments.flatten.each { |fragment|
    if fragment.respond_to?(:to_str)
      embedded_param_names = self.class.embedded_param_names(fragment)

      unless embedded_param_names.empty?
        raise ArgumentError, %Q{Missing a value for required path parameter "#{embedded_param_names[0]}"!}
      end

      unless fragment.empty?
        uri << '/' unless uri.empty? || uri =~ /\/\z/
        uri << fragment
      end
    elsif fragment.required?
      # This is a required Param that was never bound to a value.
      raise ArgumentError, %Q{Missing a value for required path parameter "#{fragment.name}"!}
    end
  }

  # Hunt for required unbound query parameters.
  obj.query_params.each { |name, value|
    if value.required?
      raise ArgumentError, %Q{Missing a value for required query parameter "#{value.name}"!}
    end
  }

  # Hunt for required unbound header parameters.
  obj.header_params.each { |name, value|
    if value.required?
      raise ArgumentError, %Q{Missing a value for required header parameter "#{value.name}"!}
    end
  }

  URIParts.new(uri, obj.query_vars, obj.headers)
end