Class: Unsplash::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/unsplash/client.rb

Overview

Common functionality across Unsplash API objects.

Direct Known Subclasses

Collection, Photo, Search, Stats, User

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Client

Build an Unsplash object with the given attributes.

Parameters:

  • attrs (Hash) (defaults to: {})

8
9
10
11
# File 'lib/unsplash/client.rb', line 8

def initialize(attrs = {})
  @attributes = OpenStruct.new(attrs)
  add_utm_to_links
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object


32
33
34
35
# 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

Class Method Details

.connectionUnsplash::Connection

The connection object being used to communicate with Unsplash.

Returns:


65
66
67
# File 'lib/unsplash/client.rb', line 65

def connection
  @@connection ||= Connection.new
end

.connection=(conn) ⇒ Unsplash::Connection

Assign a default connection object.

Parameters:

Returns:


72
73
74
# File 'lib/unsplash/client.rb', line 72

def connection=(conn)
  @@connection = conn
end

Instance Method Details

#connectionUnsplash::Connection

The connection object being used to communicate with Unsplash.

Returns:


39
40
41
# File 'lib/unsplash/client.rb', line 39

def connection
  self.class.connection
end

#reload!Unspash::Client

(Re)load full object details from Unsplash.

Returns:

  • (Unspash::Client)

    Itself, with full details reloaded.


15
16
17
18
19
20
21
22
23
# 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_hHash

Raw JSON as returned by Unsplash API.

Returns:

  • (Hash)

    json


27
28
29
# File 'lib/unsplash/client.rb', line 27

def to_h
  @attributes.to_h
end