Class: Minisky

Inherits:
Object
  • Object
show all
Includes:
Requests
Defined in:
lib/minisky/minisky.rb,
lib/minisky/compat.rb,
lib/minisky/errors.rb,
lib/minisky/version.rb,
lib/minisky/requests.rb

Overview

The default API client class for making requests to AT Protocol servers. Can be used with authentication – with the credentials stored in a YAML file – or without it, for unauthenticated requests only (by passing nil as the config file name).

Examples:

Authenticated client

# Expects a config.yml file like:
#
# id: test.example.com
# pass: secret7
#
# "id" can be a handle or a DID.

sky = Minisky.new('eurosky.social', 'config.yml')

feed = sky.get_request('app.bsky.feed.getTimeline', { limit: 100 })

Unauthenticated client

sky = Minisky.new('public.api.bsky.app', nil, progress: '*')

follows = sky.get_request('app.bsky.graph.getFollows',
  { actor: 'atproto.com', limit: 100 },
  field: 'follows'
)

Defined Under Namespace

Modules: Requests, Ruby2Compat Classes: AuthError, BadResponse, ClientErrorResponse, Error, ExpiredTokenError, FieldNotSetError, ServerErrorResponse, UnexpectedRedirect, User

Constant Summary collapse

VERSION =
"0.5.1"
NSID_REGEXP =

Regexp for NSID identifiers, used in lexicon names for record collection and API endpoints

/^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\.[a-zA-Z]([a-zA-Z]{0,61}[a-zA-Z])?)$/

Instance Attribute Summary collapse

Attributes included from Requests

#auto_manage_tokens, #default_progress, #send_auth_headers, #stop_fetch_on_empty_page

Instance Method Summary collapse

Methods included from Requests

#access_token_expired?, #base_url, #check_access, #fetch_all, #get_request, #log_in, #perform_token_refresh, #post_request, #reset_tokens, #token_expiration_date, #user

Methods included from Ruby2Compat

#get_request, #post_request

Constructor Details

#initialize(host, config_file, options = {}) ⇒ Minisky

Creates a new client instance.

Parameters:

  • host (String)

    the hostname (or base URL) of the server

  • config_file (String, nil)

    path to the YAML config file, or nil for unauthenticated client

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

    option properties to set on the new instance (see Requests properties)

Raises:

  • (AuthError)

    if the config file is missing an ID or password



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/minisky/minisky.rb', line 45

def initialize(host, config_file, options = {})
  @host = host
  @config_file = config_file

  if @config_file
    @config = YAML.load(File.read(@config_file))

    if user.id.nil? || user.pass.nil?
      raise AuthError, "Missing user id or password in the config file #{@config_file}"
    end
  else
    @config = nil
  end

  if active_repl?
    @default_progress = '.'
  end

  if options
    options.each do |k, v|
      self.send("#{k}=", v)
    end
  end
end

Instance Attribute Details

#configHash (readonly)

Returns loaded contents of the config file.

Returns:

  • (Hash)

    loaded contents of the config file



35
36
37
# File 'lib/minisky/minisky.rb', line 35

def config
  @config
end

#hostString (readonly)

Returns the hostname (or base URL) of the server.

Returns:

  • (String)

    the hostname (or base URL) of the server



32
33
34
# File 'lib/minisky/minisky.rb', line 32

def host
  @host
end

Instance Method Details

#save_configObject



70
71
72
# File 'lib/minisky/minisky.rb', line 70

def save_config
  File.write(@config_file, YAML.dump(@config)) if @config_file
end