Class: X::Community

Inherits:
Resource show all
Extended by:
Finders, Resources::Finders
Defined in:
x-resources/lib/x/resources/community.rb

Overview

A community of users who post to one another

Constant Summary collapse

FIELDS =

Every public community field

A minor release may add to it the fields the API adds, so that a lookup asks for them too; see Resource#hydrated? for what that means for a resource looked up with a list of fields of its own.

%w[access created_at description id join_policy member_count name].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from X::Resource

Instance Attribute Details

#access ⇒ String? (readonly)

Who can see the community's posts, as the API names it

Examples:

Get the access level

community.access

Returns:

  • (String, nil) —

    the access level



110
# File 'x-resources/lib/x/resources/community.rb', line 110

attribute :access

#created_at ⇒ Time? (readonly)

The creation time

Examples:

Get the creation time

community.created_at

Returns:

  • (Time, nil) —

    the creation time



94
# File 'x-resources/lib/x/resources/community.rb', line 94

attribute :created_at, :time

#description ⇒ String? (readonly)

The description

Examples:

Get the description

community.description

Returns:

  • (String, nil) —

    the description



86
# File 'x-resources/lib/x/resources/community.rb', line 86

attribute :description

#join_policy ⇒ String? (readonly)

How users become members, as the API names it

Examples:

Get the join policy

community.join_policy

Returns:

  • (String, nil) —

    the join policy



118
# File 'x-resources/lib/x/resources/community.rb', line 118

attribute :join_policy

#member_count ⇒ Integer? (readonly)

The number of members

Examples:

Get the member count

community.member_count

Returns:

  • (Integer, nil) —

    the member count



102
# File 'x-resources/lib/x/resources/community.rb', line 102

attribute :member_count, :integer

#name ⇒ String? (readonly)

The name

Examples:

Get the name

community.name

Returns:

  • (String, nil) —

    the name



78
# File 'x-resources/lib/x/resources/community.rb', line 78

attribute :name

Class Method Details

.default_params ⇒ Hash{String => Array<String>}

The default query parameters requesting every community field

Examples:

Get the default parameters

X::Community.default_params["community.fields"]

Returns:

  • (Hash{String => Array<String>}) —

    the default query parameters



55
# File 'x-resources/lib/x/resources/community.rb', line 55

def default_params = {"community.fields" => FIELDS}

.find(id, client:, **params) {|problem| ... } ⇒ Resource? Originally defined in module Resources::Finders

Look up a resource by identifier

Examples:

Look up a post by identifier

X::Post.find(1234567890, client: client)

Parameters:

  • id (String, Integer, Resource) —

    the identifier, or a resource of this class

  • client (Object) —

    the client used to make the request

  • params (Hash) —

    query parameters merged over the default parameters; one that overrides a default field or expansion parameter to leave some out builds resources that are not hydrated, so hydrate fetches the rest

Yield Parameters:

  • problem (Problem) —

    each problem the API reported, such as a resource that was not found

Returns:

  • (Resource, nil) —

    the resource or nil if it was not found, whether the API answers 200 with no data or a 404 that reports the resource as not found

Raises:

  • (ArgumentError) —

    if the identifier is not one, or is a resource of another class, before a request

  • (X::NotFound) —

    if the API answers any other 404, such as one from a client pointed at the wrong host or API version

.find!(id, client:, **params) ⇒ Resource Originally defined in module Resources::Finders

Look up a resource by identifier, which must exist

The error it raises names the identifier looked up, whether the identifier or a resource was given. Its cause is the X::NotFound of a lookup the API answered with a 404 that reports the resource as not found.

Examples:

Look up a post by identifier

X::Post.find!(1234567890, client: client)

Parameters:

  • id (String, Integer, Resource) —

    the identifier, or a resource of this class

  • client (Object) —

    the client used to make the request

  • params (Hash) —

    query parameters merged over the default parameters; one that overrides a default field or expansion parameter to leave some out builds resources that are not hydrated, so hydrate fetches the rest

Returns:

Raises:

  • (ArgumentError) —

    if the identifier is not one, or is a resource of another class, before a request

  • (MissingResource) —

    if the resource was not found, whether the API answers 200 with no data or a 404 that reports the resource as not found

  • (X::NotFound) —

    if the API answers any other 404, such as one from a client pointed at the wrong host or API version

.search(query, client:, **params) ⇒ Cursor

Search communities

Examples:

Print the communities matching a query

X::Community.search("ruby", client: client).each { |community| puts community.name }

Parameters:

  • query (String) —

    the search query

  • client (Object) —

    the client used to make the requests

  • params (Hash) —

    query parameters merged over the default parameters

Returns:

  • (Cursor) —

    a cursor over the matching communities



66
67
68
69
# File 'x-resources/lib/x/resources/community.rb', line 66

def search(query, client:, **params)
  Cursor.__send__(:build, self, "communities/search", client:, params: {query:, max_results: MAX_RESULTS}.merge(params),
    token_param: "next_token", min_results: 10)
end

Instance Method Details

The permalink of the community

Examples:

Get the permalink

community.permalink # => "https://x.com/i/communities/1234567890"

Returns:

  • (String) —

    the x.com address of the community



126
# File 'x-resources/lib/x/resources/community.rb', line 126

def permalink = "https://x.com/i/communities/#{id}"

#uri ⇒ URI::Generic

The permalink of the community as a URI

Examples:

Get the address as a URI

community.uri # => #<URI::HTTPS https://x.com/i/communities/1234567890>

Returns:

  • (URI::Generic) —

    the x.com address of the community



134
# File 'x-resources/lib/x/resources/community.rb', line 134

def uri = URI(permalink)