Class: Rocksky::Client

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

Overview

Top-level client for the Rocksky XRPC API.

client = Rocksky.new(token: ENV["ROCKSKY_TOKEN"])
client.actor.get_profile(did: "alice.bsky.social")

Resources are lazily instantiated and memoised: ‘client.actor`, `client.album`, `client.artist`, `client.scrobble`, etc.

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.rocksky.app".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: nil, token: nil, headers: {}, user_agent: "rocksky-ruby/#{Rocksky::VERSION}", open_timeout: HTTP::DEFAULT_OPEN_TIMEOUT, read_timeout: HTTP::DEFAULT_READ_TIMEOUT) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rocksky/client.rb', line 15

def initialize(
  base_url: nil,
  token: nil,
  headers: {},
  user_agent: "rocksky-ruby/#{Rocksky::VERSION}",
  open_timeout: HTTP::DEFAULT_OPEN_TIMEOUT,
  read_timeout: HTTP::DEFAULT_READ_TIMEOUT
)
  @base_url = normalize_base_url(base_url || ENV["ROCKSKY_BASE_URL"] || DEFAULT_BASE_URL)
  @token = token || ENV["ROCKSKY_TOKEN"]
  @headers = headers.dup
  @user_agent = user_agent
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @http = HTTP.new(self)
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



12
13
14
# File 'lib/rocksky/client.rb', line 12

def base_url
  @base_url
end

#headersObject (readonly)

Returns the value of attribute headers.



12
13
14
# File 'lib/rocksky/client.rb', line 12

def headers
  @headers
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



12
13
14
# File 'lib/rocksky/client.rb', line 12

def open_timeout
  @open_timeout
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



12
13
14
# File 'lib/rocksky/client.rb', line 12

def read_timeout
  @read_timeout
end

#tokenObject (readonly)

Returns the value of attribute token.



12
13
14
# File 'lib/rocksky/client.rb', line 12

def token
  @token
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



12
13
14
# File 'lib/rocksky/client.rb', line 12

def user_agent
  @user_agent
end

Instance Method Details

#actorObject

—- Resource accessors ————————————————



57
# File 'lib/rocksky/client.rb', line 57

def actor;    @actor    ||= Resources::Actor.new(@http); end

#albumObject



58
# File 'lib/rocksky/client.rb', line 58

def album;    @album    ||= Resources::Album.new(@http); end

#apikeyObject



59
# File 'lib/rocksky/client.rb', line 59

def apikey;   @apikey   ||= Resources::Apikey.new(@http); end

#artistObject



60
# File 'lib/rocksky/client.rb', line 60

def artist;   @artist   ||= Resources::Artist.new(@http); end

#chartsObject



61
# File 'lib/rocksky/client.rb', line 61

def charts;   @charts   ||= Resources::Charts.new(@http); end

#feedObject



62
# File 'lib/rocksky/client.rb', line 62

def feed;     @feed     ||= Resources::Feed.new(@http); end

#graphObject



63
# File 'lib/rocksky/client.rb', line 63

def graph;    @graph    ||= Resources::Graph.new(@http); end

#inspectObject



74
75
76
# File 'lib/rocksky/client.rb', line 74

def inspect
  "#<Rocksky::Client base_url=#{base_url.inspect} token=#{token ? "[FILTERED]" : nil.inspect}>"
end

#likeObject



64
# File 'lib/rocksky/client.rb', line 64

def like;     @like     ||= Resources::Like.new(@http); end

#mirrorObject



65
# File 'lib/rocksky/client.rb', line 65

def mirror;   @mirror   ||= Resources::Mirror.new(@http); end

#playerObject



66
# File 'lib/rocksky/client.rb', line 66

def player;   @player   ||= Resources::Player.new(@http); end

#playlistObject



67
# File 'lib/rocksky/client.rb', line 67

def playlist; @playlist ||= Resources::Playlist.new(@http); end

#procedure(nsid, params: {}, body: nil) ⇒ Object



51
52
53
# File 'lib/rocksky/client.rb', line 51

def procedure(nsid, params: {}, body: nil)
  @http.procedure(nsid, params, body)
end

#query(nsid, **params) ⇒ Object

—- Raw XRPC access —————————————————



47
48
49
# File 'lib/rocksky/client.rb', line 47

def query(nsid, **params)
  @http.query(nsid, params)
end

#scrobbleObject



68
# File 'lib/rocksky/client.rb', line 68

def scrobble; @scrobble ||= Resources::Scrobble.new(@http); end

#shoutObject



69
# File 'lib/rocksky/client.rb', line 69

def shout;    @shout    ||= Resources::Shout.new(@http); end

#songObject



70
# File 'lib/rocksky/client.rb', line 70

def song;     @song     ||= Resources::Song.new(@http); end

#spotifyObject



71
# File 'lib/rocksky/client.rb', line 71

def spotify;  @spotify  ||= Resources::Spotify.new(@http); end

#statsObject



72
# File 'lib/rocksky/client.rb', line 72

def stats;    @stats    ||= Resources::Stats.new(@http); end

#with_token(new_token) ⇒ Object

Return a derived client that uses the given token (everything else copied). Handy for sharing one client across users in a request-scoped server.



34
35
36
37
38
39
40
41
42
43
# File 'lib/rocksky/client.rb', line 34

def with_token(new_token)
  self.class.new(
    base_url: base_url,
    token: new_token,
    headers: headers,
    user_agent: user_agent,
    open_timeout: open_timeout,
    read_timeout: read_timeout
  )
end