class Unsplash::Client

Common functionality across Unsplash API objects.

Public Class Methods

connection() click to toggle source

The connection object being used to communicate with Unsplash. @return [Unsplash::Connection] the connection

# File lib/unsplash/client.rb, line 65
def connection
  @@connection ||= Connection.new
end
connection=(conn) click to toggle source

Assign a default connection object. @param conn [Unsplash::Connection] the connection @return [Unsplash::Connection] the connection

# File lib/unsplash/client.rb, line 72
def connection=(conn)
  @@connection = conn
end
new(attrs = {}) click to toggle source

Build an Unsplash object with the given attributes. @param attrs [Hash]

# File lib/unsplash/client.rb, line 8
def initialize(attrs = {})
  @attributes = OpenStruct.new(attrs)
  add_utm_to_links
end

Public Instance Methods

add_utm_params(url) click to toggle source

@private

# File lib/unsplash/client.rb, line 44
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
connection() click to toggle source

The connection object being used to communicate with Unsplash. @return [Unsplash::Connection] the connection

# File lib/unsplash/client.rb, line 39
def connection
  self.class.connection
end
method_missing(method, *args, &block) click to toggle source

@private

# File lib/unsplash/client.rb, line 32
def method_missing(method, *args, &block)
  attribute = @attributes.send(method, *args, &block)
  attribute.is_a?(Hash) ? Client.new(attribute) : attribute
end
reload!() click to toggle source

(Re)load full object details from Unsplash. @return [Unspash::Client] Itself, with full details reloaded.

# File lib/unsplash/client.rb, line 15
def reload!
  if links && links["self"]
    attrs = JSON.parse(connection.get(links["self"]).body)
    @attributes = OpenStruct.new(attrs)
    self
  else
    raise Unsplash::Error.new "Missing self link for #{self.class} with ID #{self.id}"
  end
end
to_h() click to toggle source

Raw JSON as returned by Unsplash API. @return [Hash] json

# File lib/unsplash/client.rb, line 27
def to_h
  @attributes.to_h
end