lib/unsplash/client.rb in unsplash-2.0.1 vs lib/unsplash/client.rb in unsplash-2.1.0

- old
+ new

@@ -5,10 +5,11 @@ # Build an Unsplash object with the given attributes. # @param attrs [Hash] def initialize(attrs = {}) @attributes = OpenStruct.new(attrs) + add_utm_to_links end # (Re)load full object details from Unsplash. # @return [Unspash::Client] Itself, with full details reloaded. def reload! @@ -35,9 +36,28 @@ # The connection object being used to communicate with Unsplash. # @return [Unsplash::Connection] the connection def connection self.class.connection + end + + # @private + def add_utm_params(url) + uri = URI.parse(url) + + qs = Rack::Utils.parse_nested_query(uri.query) + qs.merge!(connection.utm_params) + + uri.query = Rack::Utils.build_query(qs) + + uri.to_s + end + + # @private + def add_utm_to_links + (@attributes["links"] || {}).each do |key, url| + @attributes["links"][key] = add_utm_params(url) + end end class << self # The connection object being used to communicate with Unsplash. # @return [Unsplash::Connection] the connection