Module: Keybase::API

Defined in:
lib/keybase/api/api.rb,
lib/keybase/api.rb,
lib/keybase/api/exceptions.rb

Overview

Represents (parts of) the Keybase REST API.

Defined Under Namespace

Modules: Exceptions

Constant Summary collapse

VERSION =

The current version of keybase-unofficial-api.

"0.1.0"
BASE_URL =

The base URL for the majority of API calls.

"https://keybase.io/_/api/1.0"

Class Method Summary collapse

Class Method Details

.api_call(endpoint, query) ⇒ OpenStruct

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Make a GET request to the given endpoint with the given parameters.

Parameters:

  • endpoint (String)

    the keybase API endpoint

  • query (Hash)

    the request parameters

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:



30
31
32
33
# File 'lib/keybase/api/api.rb', line 30

def api_call(endpoint, query)
  response = Faraday.get "#{BASE_URL}#{endpoint}", query
  unwrap JSON.parse response.body, object_class: OpenStruct
end

.autocomplete(query) ⇒ OpenStruct

Search Keybase for identity components.

Examples:

Keybase::API.autocomplete "William Woodruff"

Parameters:

  • query (String)

    the string to search for

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:

See Also:



71
72
73
# File 'lib/keybase/api/api.rb', line 71

def autocomplete(query)
  api_call "/user/autocomplete.json", q: query
end

.discover(**query) ⇒ OpenStruct

Note:

Any identity supported by keybase should work (e.g, domain, hackernews, reddit, github, etc.)

Discover Keybase users from external identities.

Examples:

Keybase::API.discover github: "woodruffw", flatten: true

Parameters:

  • query (Hash)

    the request parameters

Options Hash (**query):

  • flatten (Boolean)

    whether or not to remove duplicates

  • usernames_only (Boolean)

    whether or not to reply with only names

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:

See Also:



87
88
89
# File 'lib/keybase/api/api.rb', line 87

def discover(**query)
  api_call "/user/discover.json", query
end

.lookup(**query) ⇒ OpenStruct

Note:

Any identity supported by keybase should work (e.g, domain, hackernews, reddit, github, etc.)

Look up a user, users, or external identity.

Examples:

Keybase::API.lookup username: "yossarian"
Keybase::API.lookup github: "woodruffw"

Parameters:

  • query (Hash)

    the request parameters

Options Hash (**query):

  • username (String)

    the username to look up

  • usernames (Array<String>)

    multiple usernames to look up

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:

See Also:



47
48
49
50
51
# File 'lib/keybase/api/api.rb', line 47

def lookup(**query)
  query[:usernames] = Core::U[query[:usernames]]

  api_call "/user/lookup.json", query
end

.merkle_block(**query) ⇒ OpenStruct

Retrieve a Merkle node corresponding to a given hash.

Parameters:

  • query (Hash)

    the request parameters

Options Hash (**query):

  • hash (String)

    the hash of the block to look up

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:

See Also:



109
110
111
# File 'lib/keybase/api/api.rb', line 109

def merkle_block(**query)
  api_call "/merkle/block.json", query
end

.merkle_root(**query) ⇒ OpenStruct

Retrieve the current site-wide Merkle root hash.

Parameters:

  • query (Hash)

    the request parameters

Options Hash (**query):

  • seqno (Integer)

    a specific Merkle root to return, if found

  • ctime (Integer)

    return the first root on or after the given time (UTC)

Returns:

  • (OpenStruct)

    a struct mapping of the JSON response

Raises:

See Also:



99
100
101
# File 'lib/keybase/api/api.rb', line 99

def merkle_root(**query)
  api_call "/merkle/root.json", query
end

.unwrap(struct) ⇒ Object

Cleans up the object returned by api_call.

Parameters:

  • struct (OpenStruct)

    a structified response from the Keybase API

Raises:



18
19
20
21
22
# File 'lib/keybase/api/api.rb', line 18

def unwrap(struct)
  raise Exceptions::APIError, struct.status.desc unless struct.status.code.zero?

  struct
end

.user?(user) ⇒ Boolean

Note:

This call only works on Keybase usernames, not external identities.

Test whether the given user exists on Keybase.

Examples:

Keybase::API.user? "yossarian" # => true
Keybase::API.user? "idonotexist" # => false

Parameters:

  • user (String)

    the username to test

Returns:

  • (Boolean)

    whether or not the user exists



60
61
62
# File 'lib/keybase/api/api.rb', line 60

def user?(user)
  lookup(username: user).status.code.zero? rescue false
end