Sha256: c98101ab06ac1b2a73e788dcbccc78de695c35c33f5bb85c266414fee9e3a299

Contents?: true

Size: 1.57 KB

Versions: 12

Compression:

Stored size: 1.57 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

    # Raw JSON as returned by Unsplash API.
    # @return [Hash] json
    def to_h
      @attributes.to_h
    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

12 entries across 12 versions & 2 rubygems

Version Path
unsplash-2.0.1 lib/unsplash/client.rb
unsplash-2.0.0 lib/unsplash/client.rb
unsplash-1.5.6 lib/unsplash/client.rb
unsplash_mango-1.0.0 lib/unsplash/client.rb
unsplash-1.5.5 lib/unsplash/client.rb
unsplash-1.5.4 lib/unsplash/client.rb
unsplash-1.5.3 lib/unsplash/client.rb
unsplash-1.5.2 lib/unsplash/client.rb
unsplash-1.5.1 lib/unsplash/client.rb
unsplash-1.5.0 lib/unsplash/client.rb
unsplash-1.4.3 lib/unsplash/client.rb
unsplash-1.4.2 lib/unsplash/client.rb