Class: Heroics::LinkSchema
- Inherits:
-
Object
- Object
- Heroics::LinkSchema
- 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
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#resource_name ⇒ Object
readonly
Returns the value of attribute resource_name.
Instance Method Summary collapse
-
#content_type ⇒ String
Get the Content-Type for this link.
- #encode(body) ⇒ Object
-
#example_body ⇒ Hash
Get an example request body.
-
#format_path(parameters) ⇒ String, Object
Inject parameters into the link href and return the body, if it exists.
-
#initialize(schema, resource_name, link_index) ⇒ LinkSchema
constructor
Instantiate a link schema.
-
#method ⇒ Symbol
Get the HTTP method for this link.
- #needs_request_body? ⇒ Boolean
-
#parameter_details ⇒ Hash<String, String>
Get the names and descriptions of the parameters this link expects.
-
#parameters ⇒ Array<String>
Get the names of the parameters this link expects.
-
#pretty_name ⇒ String
Get the link name in pretty form.
-
#pretty_resource_name ⇒ String
Get the resource name in pretty form.
Constructor Details
#initialize(schema, resource_name, link_index) ⇒ LinkSchema
Instantiate a link schema.
103 104 105 106 107 108 109 |
# File 'lib/heroics/schema.rb', line 103 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
#description ⇒ Object (readonly)
Returns the value of attribute description.
95 96 97 |
# File 'lib/heroics/schema.rb', line 95 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
95 96 97 |
# File 'lib/heroics/schema.rb', line 95 def name @name end |
#resource_name ⇒ Object (readonly)
Returns the value of attribute resource_name.
95 96 97 |
# File 'lib/heroics/schema.rb', line 95 def resource_name @resource_name end |
Instance Method Details
#content_type ⇒ String
Get the Content-Type for this link.
135 136 137 |
# File 'lib/heroics/schema.rb', line 135 def content_type link_schema['encType'] || 'application/json' end |
#encode(body) ⇒ Object
139 140 141 142 143 144 145 146 |
# File 'lib/heroics/schema.rb', line 139 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_body ⇒ Hash
Get an example request body.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/heroics/schema.rb', line 172 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.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/heroics/schema.rb', line 195 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 |
#method ⇒ Symbol
Get the HTTP method for this link.
128 129 130 |
# File 'lib/heroics/schema.rb', line 128 def method link_schema['method'].downcase.to_sym end |
#needs_request_body? ⇒ Boolean
165 166 167 |
# File 'lib/heroics/schema.rb', line 165 def needs_request_body? return link_schema.has_key?('schema') end |
#parameter_details ⇒ Hash<String, String>
Get the names and descriptions of the parameters this link expects.
160 161 162 163 |
# File 'lib/heroics/schema.rb', line 160 def parameter_details parameter_names = link_schema['href'].scan(PARAMETER_REGEX) resolve_parameter_details(parameter_names) end |
#parameters ⇒ Array<String>
Get the names of the parameters this link expects.
151 152 153 154 |
# File 'lib/heroics/schema.rb', line 151 def parameters parameter_names = link_schema['href'].scan(PARAMETER_REGEX) resolve_parameters(parameter_names) end |
#pretty_name ⇒ String
Get the link name in pretty form.
121 122 123 |
# File 'lib/heroics/schema.rb', line 121 def pretty_name Heroics.pretty_name(name) end |
#pretty_resource_name ⇒ String
Get the resource name in pretty form.
114 115 116 |
# File 'lib/heroics/schema.rb', line 114 def pretty_resource_name Heroics.pretty_name(resource_name) end |