Skip to content

Commit

Permalink
Merge pull request #42 from cbeer/minimize_allocation
Browse files Browse the repository at this point in the history
Minimize string allocations
  • Loading branch information
jcoyne committed Jan 27, 2015
2 parents 5f42197 + 415aea8 commit 6f58846
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions lib/ldp/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Ldp
module Response
require 'ldp/response/paging'

TYPE = 'type'.freeze
##
# Wrap the raw Faraday respone with our LDP extensions
def self.wrap client, raw_resp
Expand All @@ -14,7 +15,7 @@ def self.wrap client, raw_resp
# Extract the Link: headers from the HTTP resource
def self.links response
h = {}
Array(response.headers["Link"]).map { |x| x.split(", ") }.flatten.inject(h) do |memo, header|
Array(response.headers['Link'.freeze]).map { |x| x.split(', '.freeze) }.flatten.inject(h) do |memo, header|
m = header.match(/<(?<link>.*)>;\s?rel="(?<rel>[^"]+)"/)
if m
memo[m[:rel]] ||= []
Expand All @@ -24,10 +25,10 @@ def self.links response
memo
end
end

def self.applied_preferences headers
h = {}

Array(headers).map { |x| x.split(",") }.flatten.inject(h) do |memo, header|
m = header.match(/(?<key>[^=;]*)(=(?<value>[^;,]*))?(;\s*(?<params>[^,]*))?/)
includes = (m[:params].match(/include="(?<include>[^"]+)"/)[:include] || "").split(" ")
Expand All @@ -40,7 +41,7 @@ def self.applied_preferences headers
# Is the response an LDP resource?

def self.resource? response
Array(links(response)["type"]).include? Ldp.resource.to_s
Array(links(response)[TYPE]).include? Ldp.resource.to_s
end

##
Expand All @@ -50,14 +51,14 @@ def self.container? response
Ldp.basic_container,
Ldp.direct_container,
Ldp.indirect_container
].any? { |x| Array(links(response)["type"]).include? x.to_s }
].any? { |x| Array(links(response)[TYPE]).include? x.to_s }
end

##
# Is the response an LDP RDFSource?
# ldp:Container is a subclass of ldp:RDFSource
def self.rdf_source? response
container?(response) || Array(links(response)["type"]).include?(Ldp.rdf_source)
container?(response) || Array(links(response)[TYPE]).include?(Ldp.rdf_source)
end

def dup
Expand Down Expand Up @@ -98,7 +99,7 @@ def container?
end

def preferences
Ldp::Resource.applied_preferences(headers["Preference-Applied"])
Ldp::Resource.applied_preferences(headers['Preference-Applied'.freeze])
end
##
# Get the subject for the response
Expand Down Expand Up @@ -140,7 +141,7 @@ def graph
##
# Extract the ETag for the resource
def etag
@etag ||= headers['ETag']
@etag ||= headers['ETag'.freeze]
end

def etag=(val)
Expand All @@ -150,7 +151,7 @@ def etag=(val)
##
# Extract the last modified header for the resource
def last_modified
@last_modified ||= headers['Last-Modified']
@last_modified ||= headers['Last-Modified'.freeze]
end

def last_modified=(val)
Expand All @@ -160,17 +161,19 @@ def last_modified=(val)
##
# Extract the Link: rel="type" headers for the resource
def types
Array(links["type"])
Array(links[TYPE])
end


RETURN = 'return'.freeze

def includes? preference
key = Ldp.send("prefer_#{preference}") if Ldp.respond_to("prefer_#{preference}")
key ||= preference
preferences["return"][:includes].include?(key) || !preferences["return"][:omits].include?(key)
preferences[RETURN][:includes].include?(key) || !preferences["return"][:omits].include?(key)
end

def minimal?
preferences["return"][:value] == "minimal"
preferences[RETURN][:value] == "minimal"
end
end
end

0 comments on commit 6f58846

Please sign in to comment.