Module: InfinumJsonApiSetup::Rspec::Helpers::RequestHelper
- Defined in:
- lib/infinum_json_api_setup/rspec/helpers/request_helper.rb
Instance Method Summary collapse
- #_data_params(type, attributes, relationships, **options) ⇒ Object
- #_detect_resource_type(resource) ⇒ Object
- #_included_params(resources) ⇒ Object
- #_relationships_params(relationships) ⇒ Object
- #default_headers ⇒ Object
- #json_api_relationship(resource, type: nil) ⇒ Object
- #json_api_relationship_data(relationship) ⇒ Object
- #json_api_relationship_hash(id, type) ⇒ Object
- #json_api_relationships_request_body(relationships) ⇒ Object
- #json_api_request_body(type:, attributes: {}, relationships: {}, included: [], **options) ⇒ Object
Instance Method Details
#_data_params(type, attributes, relationships, **options) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/infinum_json_api_setup/rspec/helpers/request_helper.rb', line 23 def _data_params(type, attributes, relationships, **) attributes = attributes.with_indifferent_access { type: type, id: attributes.delete(:id).to_s, attributes: attributes.presence, relationships: _relationships_params(relationships).presence }.merge().compact end |
#_detect_resource_type(resource) ⇒ Object
74 75 76 |
# File 'lib/infinum_json_api_setup/rspec/helpers/request_helper.rb', line 74 def _detect_resource_type(resource) resource.class.name.parameterize.pluralize end |
#_included_params(resources) ⇒ Object
34 35 36 37 38 |
# File 'lib/infinum_json_api_setup/rspec/helpers/request_helper.rb', line 34 def _included_params(resources) resources.reject do |resource| resource[:attributes].blank? && resource[:relationships].blank? end end |
#_relationships_params(relationships) ⇒ Object
40 41 42 43 44 |
# File 'lib/infinum_json_api_setup/rspec/helpers/request_helper.rb', line 40 def _relationships_params(relationships) relationships.transform_values do |relationship| json_api_relationship_data(relationship) end end |
#default_headers ⇒ Object
5 6 7 8 9 10 |
# File 'lib/infinum_json_api_setup/rspec/helpers/request_helper.rb', line 5 def default_headers { 'content-type' => 'application/vnd.api+json', 'accept' => 'application/vnd.api+json' } end |
#json_api_relationship(resource, type: nil) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/infinum_json_api_setup/rspec/helpers/request_helper.rb', line 57 def json_api_relationship(resource, type: nil) if resource.is_a?(Array) type = type.presence || _detect_resource_type(resource.first) resource.map { |item| json_api_relationship_hash(item.id, type) } else type = type.presence || _detect_resource_type(resource) json_api_relationship_hash(resource.id, type) end end |
#json_api_relationship_data(relationship) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/infinum_json_api_setup/rspec/helpers/request_helper.rb', line 46 def json_api_relationship_data(relationship) { data: case relationship when Array then relationship.map { |rel| rel.slice(:id, :type) } when Hash then relationship.slice(:id, :type) when NilClass then nil else raise ArgumentError 'relationship must be either an array or hash' end } end |
#json_api_relationship_hash(id, type) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/infinum_json_api_setup/rspec/helpers/request_helper.rb', line 67 def json_api_relationship_hash(id, type) { id: id.to_s, type: type } end |
#json_api_relationships_request_body(relationships) ⇒ Object
12 13 14 |
# File 'lib/infinum_json_api_setup/rspec/helpers/request_helper.rb', line 12 def json_api_relationships_request_body(relationships) json_api_relationship_data(relationships).to_json end |
#json_api_request_body(type:, attributes: {}, relationships: {}, included: [], **options) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/infinum_json_api_setup/rspec/helpers/request_helper.rb', line 16 def json_api_request_body(type:, attributes: {}, relationships: {}, included: [], **) {}.tap do |body| body[:data] = _data_params(type, attributes, relationships, **) body[:included] = _included_params(included).presence end.merge().compact.to_json end |