Skip to content
Twexapi
English
Esc
↑↓navigate↵open⌘Jpreview
On this page

Ruby SDK

Use the TwexAPI Ruby SDK for tweet search, follower exports, and public reads in Ruby apps and scripts.

Use the Ruby SDK when you want typed request objects, Faraday-backed HTTP, retries, and pagination helpers for TwexAPI REST workflows in Ruby apps and scripts.

twexapi-dev/x-api-scraper-rubyRuby SDK for TwexAPI public social data and approved account workflows. Independent third-party service.00
Ruby task SDK call Save
Search tweets client.search.advanced next_cursor
Read profile client.users.get_about screen_name
List followers client.users.followers.list cursor

Install

Add the gem to your Gemfile:

gem "x_api_scraper", "~> 0.1.0"

Or install from GitHub until the gem is published:

gem "x_api_scraper", git: "https://github.com/twexapi-dev/x-api-scraper-ruby.git"

Source: twexapi-dev/x-api-scraper-ruby

Authenticate

export X_API_SCRAPER_KEY="YOUR_API_KEY"
require "x_api_scraper"

Models = ::XapiScraper::Models
client = ::XapiScraper::Client.new(
  bearer_auth: ENV["X_API_SCRAPER_KEY"]
)

req = Models::Components::AdvancedSearchCursorQuery.new(
  search_terms: ["from:elonmusk"],
  sort_by: "Latest",
  next_cursor: ""
)
res = client.search.advanced(request: req)

Basic example

Look up a profile and list followers:

about = client.users.get_about(
  request: Models::Operations::UsersGetAboutRequest.new(screen_name: "elonmusk")
)

followers = client.users.followers.list(
  request: Models::Components::FollowersFollowingV3Query.new(username: "elonmusk")
)

Workflow: search tweets

cursor = ""
page_index = 0

loop do
  req = Models::Components::AdvancedSearchCursorQuery.new(
    search_terms: ["from:elonmusk AI"],
    sort_by: "Latest",
    next_cursor: cursor
  )
  page = client.search.advanced(request: req).advanced_search_cursor_response

  page.tweets&.each do |tweet|
    row = {
      source: "twexapi.ruby.search",
      tweet_id: tweet.id,
      text: tweet.text,
      page_index: page_index,
      next_cursor: page.next_cursor
    }
    puts row.to_json
  end

  break unless page.has_next_page && page.next_cursor && !page.next_cursor.empty?

  cursor = page.next_cursor
  page_index += 1
end

Error handling

begin
  client.search.advanced(request: req)
rescue Models::Errors::HTTPValidationError => e
  raise e
rescue Errors::APIError => e
  puts e.status_code, e.body
end

References

Was this page helpful?