module PeppersGhost::Core
Public Instance Methods
match_resource_type(json_resource, resource, attributes, strict = false)
click to toggle source
# File lib/peppers-ghost/core.rb, line 2 def match_resource_type(json_resource, resource, attributes, strict = false) match_structure json_resource, attributes match_values json_resource, resource, attributes, strict end
match_structure(json_resource, attributes, count = nil, method = nil)
click to toggle source
# File lib/peppers-ghost/core.rb, line 7 def match_structure(json_resource, attributes, count = nil, method = nil) method ||= :resource expect(json_resource).to_not be_nil if json_resource.is_a?(Array) expect(json_resource).to have(count).method_missing(method) if count match_single_structure json_resource, attributes, count else expect(json_resource.keys).to match_array attributes.map(&:to_s) end end
match_values(json_resource, resource, attributes, strict = false)
click to toggle source
# File lib/peppers-ghost/core.rb, line 19 def match_values(json_resource, resource, attributes, strict = false) if json_resource.is_a?(Array) expect(json_resource).to have(1).resource json_resource = json_resource.first end match_attributes json_resource, resource, attributes, strict end
Private Instance Methods
match_attribute(json_value, value, strict = false)
click to toggle source
# File lib/peppers-ghost/core.rb, line 54 def match_attribute(json_value, value, strict = false) expect(value).to_not be_nil if strict expect(json_value).to eq value end
match_attributes(json_resource, resource, attributes, strict = false)
click to toggle source
# File lib/peppers-ghost/core.rb, line 35 def match_attributes(json_resource, resource, attributes, strict = false) attributes.each do |attribute| next unless resource.respond_to?(attribute) or (resource.is_a?(Hash) and (resource.has_key?(attribute.to_s) or resource.has_key?(attribute.to_sym))) value = if resource.respond_to?(attribute) resource.send attribute elsif resource.is_a? Hash resource[attribute.to_s] || resource[attribute.to_sym] end json_value = json_resource[attribute.to_s] match_attribute json_value, value, strict end end
match_single_structure(json_resources, attributes, count)
click to toggle source
# File lib/peppers-ghost/core.rb, line 29 def match_single_structure(json_resources, attributes, count) json_resources.each do |json_resource| match_structure json_resource, attributes end end