Class: Forem::Client

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

Overview

Client for making authenticated requests to the Forem API.

Use this when you need per-instance configuration (e.g., different API keys for different Forem instances). For simple single-instance use, configure the global api_key instead.

Examples:

Basic client usage

client = Forem::Client.new("your-api-key")
articles = client.articles.list(per_page: 10)

Connecting to a custom Forem instance

client = Forem::Client.new("key", api_base: "https://my-forem.com")
user = client.users.me

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_base: "https://dev.to", **opts) ⇒ Client

Create a new Forem API client.

Examples:

client = Forem::Client.new("my-secret-api-key")

With a custom Forem instance

client = Forem::Client.new("my-key", api_base: "https://community.example.com")

Parameters:

  • api_key (String)

    your Forem API key

  • api_base (String) (defaults to: "https://dev.to")

    base URL for the Forem API (default: "https://dev.to")

  • opts (Hash)

    additional configuration options passed to Forem::Configuration



36
37
38
39
40
41
42
# File 'lib/forem/client.rb', line 36

def initialize(api_key, api_base: "https://dev.to", **opts)
  @config = Configuration.new
  @config.api_key = api_key
  @config.api_base = api_base
  opts.each { |k, v| @config.send(:"#{k}=", v) if @config.respond_to?(:"#{k}=") }
  @requestor = APIRequestor.new(config: @config)
end

Instance Attribute Details

#configConfiguration (readonly)

Returns the configuration object for this client.

Returns:



19
20
21
# File 'lib/forem/client.rb', line 19

def config
  @config
end

#requestorAPIRequestor (readonly)

Returns the requestor used to make HTTP calls.

Returns:



22
23
24
# File 'lib/forem/client.rb', line 22

def requestor
  @requestor
end

Instance Method Details

#admin_conceptsServices::AdminConceptService

Access the Admin Concepts API.

Examples:

client.admin_concepts.list

Returns:



269
# File 'lib/forem/client.rb', line 269

def admin_concepts;              @admin_concepts ||= Services::AdminConceptService.new(@requestor); end

#admin_usersServices::AdminUserService

Access the Admin Users API.

Examples:

client.admin_users.create(email: "new@example.com", name: "New User")

Returns:

See Also:



245
# File 'lib/forem/client.rb', line 245

def admin_users;                 @admin_users ||= Services::AdminUserService.new(@requestor); end

#agent_sessionsServices::AgentSessionService

Access the Agent Sessions API.

Examples:

client.agent_sessions.list
client.agent_sessions.presign(filename: "upload.jpg")

Returns:

See Also:



206
# File 'lib/forem/client.rb', line 206

def agent_sessions;              @agent_sessions ||= Services::AgentSessionService.new(@requestor); end

#analyticsServices::AnalyticsService

Access the Analytics API.

Examples:

client.analytics.totals(username: "jsmith")
client.analytics.historical(username: "jsmith", start: "2024-01-01")

Returns:

See Also:



226
# File 'lib/forem/client.rb', line 226

def analytics;                   @analytics ||= Services::AnalyticsService.new(@requestor); end

#articlesServices::ArticleService

Access the Articles API.

Examples:

client.articles.list(per_page: 5)
client.articles.retrieve(12345)

Returns:

See Also:



52
# File 'lib/forem/client.rb', line 52

def articles;                    @articles ||= Services::ArticleService.new(@requestor); end

#billboardsServices::BillboardService

Access the Billboards API.

Examples:

client.billboards.list
client.billboards.retrieve(3)

Returns:

See Also:



156
# File 'lib/forem/client.rb', line 156

def billboards;                  @billboards ||= Services::BillboardService.new(@requestor); end

#commentsServices::CommentService

Access the Comments API.

Examples:

client.comments.list(a_id: 123)
client.comments.retrieve("abc123")

Returns:

See Also:



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

def comments;                    @comments ||= Services::CommentService.new(@requestor); end

#conceptsServices::ConceptService

Access the Concepts API.

Examples:

client.concepts.list

Returns:



261
# File 'lib/forem/client.rb', line 261

def concepts;                    @concepts ||= Services::ConceptService.new(@requestor); end

#followersServices::FollowerService

Access the Followers API.

Examples:

client.followers.list(per_page: 50)

Returns:

See Also:



110
# File 'lib/forem/client.rb', line 110

def followers;                   @followers ||= Services::FollowerService.new(@requestor); end

#followsServices::FollowService

Access the Follows API.

Examples:

client.follows.list
client.follows.create(followable_type: "User", followable_id: 99)

Returns:

See Also:



101
# File 'lib/forem/client.rb', line 101

def follows;                     @follows ||= Services::FollowService.new(@requestor); end

#health_checksServices::HealthCheckService

Access the Health Checks API.

Examples:

client.health_checks.app
client.health_checks.database

Returns:

See Also:



236
# File 'lib/forem/client.rb', line 236

def health_checks;               @health_checks ||= Services::HealthCheckService.new(@requestor); end

#organizationsServices::OrganizationService

Access the Organizations API.

Examples:

client.organizations.list
client.organizations.retrieve(7)

Returns:

See Also:



82
# File 'lib/forem/client.rb', line 82

def organizations;               @organizations ||= Services::OrganizationService.new(@requestor); end

#pagesServices::PageService

Access the Pages API.

Examples:

client.pages.list
client.pages.create(title: "About", slug: "about", body_markdown: "...")

Returns:

See Also:



166
# File 'lib/forem/client.rb', line 166

def pages;                       @pages ||= Services::PageService.new(@requestor); end

#podcast_episodesServices::PodcastEpisodeService

Access the Podcast Episodes API.

Examples:

client.podcast_episodes.list(username: "someshow")

Returns:

See Also:



128
# File 'lib/forem/client.rb', line 128

def podcast_episodes;            @podcast_episodes ||= Services::PodcastEpisodeService.new(@requestor); end

#profile_imagesServices::ProfileImageService

Access the Profile Images API.

Examples:

client.profile_images.retrieve("jsmith")

Returns:

See Also:



146
# File 'lib/forem/client.rb', line 146

def profile_images;              @profile_images ||= Services::ProfileImageService.new(@requestor); end

#reactionsServices::ReactionService

Access the Reactions API.

Examples:

client.reactions.create(reactable_type: "Article", reactable_id: 1, category: "like")
client.reactions.toggle(reactable_type: "Article", reactable_id: 1, category: "like")

Returns:

See Also:



186
# File 'lib/forem/client.rb', line 186

def reactions;                   @reactions ||= Services::ReactionService.new(@requestor); end

#reading_listServices::ReadingListService

Access the Reading List API.

Examples:

client.reading_list.list(page: 1)

Returns:

See Also:



119
# File 'lib/forem/client.rb', line 119

def reading_list;                @reading_list ||= Services::ReadingListService.new(@requestor); end

Access the Recommended Articles Lists API.

Examples:

client.recommended_articles_lists.list
client.recommended_articles_lists.retrieve(2)

Returns:

See Also:



196
# File 'lib/forem/client.rb', line 196

def recommended_articles_lists;  @recommended_articles_lists ||= Services::RecommendedArticlesListService.new(@requestor); end

#request_redirectsServices::RequestRedirectService

Access the Admin Request Redirects API.

Examples:

client.request_redirects.list

Returns:



277
# File 'lib/forem/client.rb', line 277

def request_redirects;           @request_redirects ||= Services::RequestRedirectService.new(@requestor); end

#segmentsServices::SegmentService

Access the Segments API.

Examples:

client.segments.list
client.segments.retrieve(5)

Returns:

See Also:



176
# File 'lib/forem/client.rb', line 176

def segments;                    @segments ||= Services::SegmentService.new(@requestor); end

#surveysServices::SurveyService

Access the Surveys API.

Examples:

client.surveys.list
client.surveys.retrieve(8)

Returns:

See Also:



216
# File 'lib/forem/client.rb', line 216

def surveys;                     @surveys ||= Services::SurveyService.new(@requestor); end

#tagsServices::TagService

Access the Tags API.

Examples:

client.tags.list(per_page: 20)

Returns:

See Also:



91
# File 'lib/forem/client.rb', line 91

def tags;                        @tags ||= Services::TagService.new(@requestor); end

Access the Trends API.

Examples:

client.trends.list

Returns:



253
# File 'lib/forem/client.rb', line 253

def trends;                      @trends ||= Services::TrendService.new(@requestor); end

#usersServices::UserService

Access the Users API.

Examples:

client.users.me
client.users.retrieve(42)

Returns:

See Also:



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

def users;                       @users ||= Services::UserService.new(@requestor); end

#videosServices::VideoService

Access the Videos API.

Examples:

client.videos.list(page: 1)

Returns:

See Also:



137
# File 'lib/forem/client.rb', line 137

def videos;                      @videos ||= Services::VideoService.new(@requestor); end