Class: Onsi::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/onsi/params.rb

Overview

Used to handle parsing JSON-API formated params

Defined Under Namespace

Classes: MissingReqiredAttribute, RelationshipNotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, relationships) ⇒ Params

Returns a new instance of Params.



109
110
111
112
# File 'lib/onsi/params.rb', line 109

def initialize(attributes, relationships)
  @attributes = attributes
  @relationships = relationships
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



107
108
109
# File 'lib/onsi/params.rb', line 107

def attributes
  @attributes
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



107
108
109
# File 'lib/onsi/params.rb', line 107

def relationships
  @relationships
end

Class Method Details

.parse(params, attributes = [], relationships = []) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/onsi/params.rb', line 34

def parse(params, attributes = [], relationships = [])
  data = params.require(:data)
  data.require(:type)
  attrs = permit_attributes(data, attributes)
  relas = permit_relationships(data, relationships)
  new(attrs, relas)
end

.parse_json(body, attributes = [], relationships = []) ⇒ Object



42
43
44
45
46
47
# File 'lib/onsi/params.rb', line 42

def parse_json(body, attributes = [], relationships = [])
  content = body.respond_to?(:read) ? body.read : body
  json = JSON.parse(content)
  params = ActionController::Parameters.new(json)
  parse(params, attributes, relationships)
end

Instance Method Details

#default(key, value) ⇒ Object

Set a default value.

This value will only be used if the key is missing from the passed attributes

Can take any object. If the object responds to call (Lambda) it will be called when parsing attributes



174
175
176
177
# File 'lib/onsi/params.rb', line 174

def default(key, value)
  @attrs_hash = nil
  defaults[key.to_sym] = value
end

#fetch(key, default = nil) ⇒ Object

Fetch a value from the attributes or return the passed default value



122
123
124
# File 'lib/onsi/params.rb', line 122

def fetch(key, default = nil)
  attrs_hash[key] || default
end

#flattenObject

Flatten an merge the attributes & relationships into one hash.



116
117
118
# File 'lib/onsi/params.rb', line 116

def flatten
  attrs_hash.to_h.merge(relationships.to_h).with_indifferent_access
end

#require(key) ⇒ Object

Make an attributes key required.

Throws MissingReqiredAttribute if the value is nil



130
131
132
133
134
135
136
137
# File 'lib/onsi/params.rb', line 130

def require(key)
  value = attrs_hash[key]
  if value.nil?
    raise MissingReqiredAttribute.new("Missing attribute #{key}", key)
  end

  value
end

#safe_fetch(key) ⇒ Object

Handle finding a relationship’s object

If an ActiveRecord::RecordNotFound is raised, a RelationshipNotFound error will be raised so the ErrorResponder can build an appropriate error message

params.safe_fetch(:person) do |id|

Person.find(id)

end



148
149
150
151
152
# File 'lib/onsi/params.rb', line 148

def safe_fetch(key)
  yield(@relationships[key])
rescue ActiveRecord::RecordNotFound
  raise RelationshipNotFound.new("Can't find relationship #{key}", key)
end

#transform(key, &block) ⇒ Object

Perform a transform on the value

Any getter will run the value through the transform block.

(The values are memoized)

‘params.transform(:date) { |date| Time.parse(date) }`



162
163
164
165
# File 'lib/onsi/params.rb', line 162

def transform(key, &block)
  @attrs_hash = nil
  transforms[key.to_sym] = block
end