Class: Heroics::LinkSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/heroics/schema.rb

Overview

A wrapper around a bare link element for a resource in a JSON schema to make it easier to use.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, resource_name, link_index) ⇒ LinkSchema

Instantiate a link schema.

Parameters:

  • schema (Hash)

    The bare JSON schema to wrap.

  • resource_name (String)

    The name of the resource to identify in the schema.

  • link_index (Fixnum)

    The index of the link in the resource schema.



112
113
114
115
116
117
118
# File 'lib/heroics/schema.rb', line 112

def initialize(schema, resource_name, link_index)
  @schema = schema
  @resource_name = resource_name
  @link_index = link_index
  @name = Heroics.ruby_name(link_schema['title'])
  @description = link_schema['description']
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



104
105
106
# File 'lib/heroics/schema.rb', line 104

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



104
105
106
# File 'lib/heroics/schema.rb', line 104

def name
  @name
end

#resource_nameObject (readonly)

Returns the value of attribute resource_name.



104
105
106
# File 'lib/heroics/schema.rb', line 104

def resource_name
  @resource_name
end

Instance Method Details

#content_typeString

Get the Content-Type for this link.

Returns:

  • (String)

    The Content-Type value



144
145
146
# File 'lib/heroics/schema.rb', line 144

def content_type
  link_schema['encType'] || 'application/json'
end

#encode(body) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/heroics/schema.rb', line 148

def encode(body)
  case content_type
  when 'application/x-www-form-urlencoded'
    URI.encode_www_form(body)
  when /application\/.*json/
    MultiJson.dump(body)
  end
end

#example_bodyHash

Get an example request body.

Returns:

  • (Hash)

    A sample request body.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/heroics/schema.rb', line 181

def example_body
  if body_schema = link_schema['schema']
    definitions = @schema['definitions'][@resource_name]['definitions']
    Hash[body_schema['properties'].keys.map do |property|
      # FIXME This is wrong! -jkakar
      if definitions.has_key?(property)
        example = definitions[property]['example']
      else
        example = ''
      end
      [property, example]
    end]
  end
end

#format_path(parameters) ⇒ String, Object

Inject parameters into the link href and return the body, if it exists.

Parameters:

  • parameters (Array)

    The list of parameters to inject into the path.

Returns:

  • (String, Object)

    A path and request body pair. The body value is nil if a payload wasn’t included in the list of parameters.

Raises:

  • (ArgumentError)

    Raised if either too many or too few parameters were provided.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/heroics/schema.rb', line 204

def format_path(parameters)
  path = link_schema['href']
  parameter_size = path.scan(PARAMETER_REGEX).size
  too_few_parameters = parameter_size > parameters.size
  # FIXME We should use the schema to detect when a request body is
  # permitted and do the calculation correctly here. -jkakar
  too_many_parameters = parameter_size < (parameters.size - 1)
  if too_few_parameters || too_many_parameters
    raise ArgumentError.new("wrong number of arguments " +
                            "(#{parameters.size} for #{parameter_size})")
  end

  (0...parameter_size).each do |i|
    path = path.sub(PARAMETER_REGEX, format_parameter(parameters[i]))
  end
  body = parameters.slice(parameter_size)
  return path, body
end

#methodSymbol

Get the HTTP method for this link.

Returns:

  • (Symbol)

    The HTTP method.



137
138
139
# File 'lib/heroics/schema.rb', line 137

def method
  link_schema['method'].downcase.to_sym
end

#needs_request_body?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/heroics/schema.rb', line 174

def needs_request_body?
  return link_schema.has_key?('schema')
end

#parameter_detailsHash<String, String>

Get the names and descriptions of the parameters this link expects.

Returns:

  • (Hash<String, String>)

    A list of hashes with ‘name` and `description` key/value pairs describing parameters.



169
170
171
172
# File 'lib/heroics/schema.rb', line 169

def parameter_details
  parameter_names = link_schema['href'].scan(PARAMETER_REGEX)
  resolve_parameter_details(parameter_names)
end

#parametersArray<String>

Get the names of the parameters this link expects.

Returns:

  • (Array<String>)

    The parameters.



160
161
162
163
# File 'lib/heroics/schema.rb', line 160

def parameters
  parameter_names = link_schema['href'].scan(PARAMETER_REGEX)
  resolve_parameters(parameter_names)
end

#pretty_nameString

Get the link name in pretty form.

Returns:

  • (String)

    The pretty link name.



130
131
132
# File 'lib/heroics/schema.rb', line 130

def pretty_name
  Heroics.pretty_name(name)
end

#pretty_resource_nameString

Get the resource name in pretty form.

Returns:

  • (String)

    The pretty resource name.



123
124
125
# File 'lib/heroics/schema.rb', line 123

def pretty_resource_name
  Heroics.pretty_name(resource_name)
end