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.
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
#description ⇒ Object (readonly)
Returns the value of attribute description.
104 105 106 |
# File 'lib/heroics/schema.rb', line 104 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
104 105 106 |
# File 'lib/heroics/schema.rb', line 104 def name @name end |
#resource_name ⇒ Object (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_type ⇒ String
Get the Content-Type for this link.
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_body ⇒ Hash
Get an example 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.
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 |
#method ⇒ Symbol
Get the HTTP method for this link.
137 138 139 |
# File 'lib/heroics/schema.rb', line 137 def method link_schema['method'].downcase.to_sym end |
#needs_request_body? ⇒ Boolean
174 175 176 |
# File 'lib/heroics/schema.rb', line 174 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.
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 |
#parameters ⇒ Array<String>
Get the names of the parameters this link expects.
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_name ⇒ String
Get the link name in pretty form.
130 131 132 |
# File 'lib/heroics/schema.rb', line 130 def pretty_name Heroics.pretty_name(name) end |
#pretty_resource_name ⇒ String
Get the resource name in pretty form.
123 124 125 |
# File 'lib/heroics/schema.rb', line 123 def pretty_resource_name Heroics.pretty_name(resource_name) end |