Sha256: 0b4b35935543a2c74d273f3f0306798307c4690ef57c9e3f2d2ed98cda8c30de

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

module Unsplash # :nodoc:

  # Common functionality across Unsplash API objects.
  class Client

    # Build an Unsplash object with the given attributes.
    # @param attrs [Hash]
    def initialize(attrs = {})
      @attributes = OpenStruct.new(attrs)
    end

    # (Re)load full object details from Unsplash.
    # @return [Unspash::Client] Itself, with full details reloaded.
    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

    # @private
    def method_missing(method, *args, &block)
      attribute = @attributes.send(method, *args, &block)
      attribute.is_a?(Hash) ? Client.new(attribute) : attribute
    end

    # The connection object being used to communicate with Unsplash.
    # @return [Unsplash::Connection] the connection
    def connection
      self.class.connection
    end

    class << self
      # The connection object being used to communicate with Unsplash.
      # @return [Unsplash::Connection] the connection
      def connection
        @@connection ||= Connection.new
      end

      # Assign a default connection object.
      # @param conn [Unsplash::Connection] the connection
      # @return [Unsplash::Connection] the connection
      def connection=(conn)
        @@connection = conn
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unsplash-1.4.1 lib/unsplash/client.rb
unsplash-1.4.0 lib/unsplash/client.rb