|
1 | 1 | # X |
2 | 2 |
|
3 | | -A Ruby interface to the X API. |
| 3 | +#### A Ruby interface to the X API. |
4 | 4 |
|
5 | 5 | ## Installation |
6 | 6 |
|
7 | | -Install the gem and add to the application's Gemfile by executing: |
| 7 | +Install the gem and add to the application's Gemfile: |
8 | 8 |
|
9 | | - $ bundle add x |
| 9 | + bundle add x |
10 | 10 |
|
11 | | -If bundler is not being used to manage dependencies, install the gem by executing: |
| 11 | +Or, if Bundler is not being used to manage dependencies: |
12 | 12 |
|
13 | | - $ gem install x |
| 13 | + gem install x |
14 | 14 |
|
15 | 15 | ## Usage |
16 | 16 |
|
| 17 | +First, obtain X credentails from https://developer.x.com. |
| 18 | + |
17 | 19 | ```ruby |
18 | | -x_oauth_credentials = { |
| 20 | +x_credentials = { |
19 | 21 | api_key: "INSERT YOUR X API KEY HERE", |
20 | 22 | api_key_secret: "INSERT YOUR X API KEY SECRET HERE", |
21 | 23 | access_token: "INSERT YOUR X ACCESS TOKEN HERE", |
22 | 24 | access_token_secret: "INSERT YOUR X ACCESS TOKEN SECRET HERE", |
23 | 25 | } |
24 | 26 |
|
25 | | -# Initialize X API client with OAuth credentials |
26 | | -x_client = X::Client.new(**x_oauth_credentials) |
| 27 | +# Initialize an X API client with your OAuth credentials |
| 28 | +x_client = X::Client.new(**x_credentials) |
27 | 29 |
|
28 | | -# Request yourself |
| 30 | +# Get data about yourself |
29 | 31 | x_client.get("users/me") |
30 | 32 | # {"data"=>{"id"=>"7505382", "name"=>"Erik Berlin", "username"=>"sferik"}} |
31 | 33 |
|
32 | 34 | # Post a tweet |
33 | 35 | tweet = x_client.post("tweets", '{"text":"Hello, World! (from @gem)"}') |
34 | 36 | # {"data"=>{"edit_history_tweet_ids"=>["1234567890123456789"], "id"=>"1234567890123456789", "text"=>"Hello, World! (from @gem)"}} |
35 | 37 |
|
36 | | -# Delete a tweet |
| 38 | +# Delete the tweet you just posted |
37 | 39 | x_client.delete("tweets/#{tweet["data"]["id"]}") |
38 | 40 | # {"data"=>{"deleted"=>true}} |
39 | 41 |
|
40 | 42 | # Initialize an API v1.1 client |
41 | | -v1_client = X::Client.new(base_url: "https://api.twitter.com/1.1/", **x_oauth_credentials) |
| 43 | +v1_client = X::Client.new(base_url: "https://api.twitter.com/1.1/", **x_credentials) |
42 | 44 |
|
43 | 45 | # Request your account settings |
44 | 46 | v1_client.get("account/settings.json") |
45 | 47 |
|
46 | 48 | # Initialize an X Ads API client |
47 | | -ads_client = X::Client.new(base_url: "https://ads-api.twitter.com/12/", **x_oauth_credentials) |
| 49 | +ads_client = X::Client.new(base_url: "https://ads-api.twitter.com/12/", **x_credentials) |
48 | 50 |
|
49 | 51 | # Request your ad accounts |
50 | 52 | ads_client.get("accounts") |
51 | 53 | ``` |
52 | 54 |
|
53 | 55 | ## History and Philosophy |
54 | 56 |
|
55 | | -This library is a rewrite of the [Twitter Ruby library](https://github.com/sferik/twitter). Over 16 years, that library ballooned to over 3,000 lines of code (plus 7,500 lines of tests). At the time of writing, this library is about 300 lines of code (plus 200 test lines) and I’d like to keep it that way. That doesn’t mean new features won’t be added over time, but the benefits of potential new features must be weighed against the benefits of simplicity: |
| 57 | +This library is a rewrite of the [Twitter Ruby library](https://github.com/sferik/twitter). Over 16 years of development, that library ballooned to over 3,000 lines of code (plus 7,500 lines of tests). At the time of writing, this library is about 300 lines of code (plus 200 test lines) and I’d like to keep it that way. That doesn’t mean new features won’t be added over time, but the benefits of more code must be weighted against the benefits of less: |
56 | 58 |
|
57 | 59 | * Less code is easier to maintain. |
58 | 60 | * Less code means fewer bugs. |
59 | 61 | * Less code runs faster. |
60 | 62 |
|
61 | | -In the immortal words of [Ezra Zygmuntowicz](https://github.com/ezmobius) and his [Merb](https://github.com/merb) project (may they both rest in peace): “No code is faster than no code.” The fastest code is the code that is never executed because it doesn’t exist. That principle should apply not just to this library itself but to third-party dependencies. At present, this library has one dependency ([oauth](https://rubygems.org/gems/oauth)) and I’d like to keep it that way. If anything, it should have fewer. |
| 63 | +In the immortal words of [Ezra Zygmuntowicz](https://github.com/ezmobius) and his [Merb](https://github.com/merb) project (may they both rest in peace): |
| 64 | + |
| 65 | +> No code is faster than no code. |
62 | 66 |
|
63 | | -The tests for the previous version of this library ran in about 2 seconds. That sounds pretty fast until you see that tests for this library run in 2 hundredths of a second. This means you can automatically run the tests any time you write a file and receive immediate feedback. For such of workflows, 2 seconds feels painfully slow. At the same time, we aim to maintain 100% C0 code coverage. |
| 67 | +The fastest code is the code that is never executed because it doesn’t exist. That principle should apply not just to this library itself but to third-party dependencies. At present, this library has one runtime dependency ([oauth](https://rubygems.org/gems/oauth)) and I’d like to keep it that way. If anything, it should have fewer dependencies. |
64 | 68 |
|
65 | | -This code is not littered with comments that are intended to generate documentation. Rather, this code is intended to be simple enough to serve as its own documentation. If you want to understand how something works, don’t read the documentation—it might be wrong—just read the code. The code is always right. |
| 69 | +Tests for the previous version of this library ran in about 2 seconds. That sounds pretty fast until you see that tests for this library run in 2 hundredths of a second. This means you can automatically run the tests any time you write a file and receive immediate feedback. For such of workflows, 2 seconds feels painfully slow. |
66 | 70 |
|
67 | | -This project conforms to [Standard Ruby](https://github.com/standardrb/standard). Patches that don’t maintain that standard will not be accepted. |
| 71 | +This code is not littered with comments that are intended to generate documentation. Rather, this code is intended to be simple enough to serve as its own documentation. If you want to understand how something works, don’t read the documentation—it might be wrong—read the code. The code is always right. |
68 | 72 |
|
69 | 73 | ## Development |
70 | 74 |
|
71 | | -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. |
| 75 | +1. Checkout and repo: |
| 76 | + |
| 77 | + git checkout git@github.com:sferik/x.git |
| 78 | + |
| 79 | +2. Enter the repo’s directory: |
| 80 | + |
| 81 | + cd x |
| 82 | + |
| 83 | +3. Install dependencies via Bundler: |
| 84 | + |
| 85 | + bin/setup |
| 86 | + |
| 87 | +4. Run the default Rake task to ensure all tests pass: |
| 88 | + |
| 89 | + bundle exec rake |
72 | 90 |
|
73 | | -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). |
| 91 | +5. Create a new branch for your feature or bug fix: |
| 92 | + |
| 93 | + git checkout -b my-new-branch |
74 | 94 |
|
75 | 95 | ## Contributing |
76 | 96 |
|
77 | 97 | Bug reports and pull requests are welcome on GitHub at https://github.com/sferik/x. |
78 | 98 |
|
| 99 | +Pull requests will only be accepted if they meet all the following criteria: |
| 100 | + |
| 101 | +1. Code must conform to [Standard Ruby](https://github.com/standardrb/standard). This can be verified with: |
| 102 | + |
| 103 | + bundle exec rake standard |
| 104 | + |
| 105 | +2. For any new code paths, tests must be added to maintain 100% C0 code coverage. This can be verified with: |
| 106 | + |
| 107 | + bundle exec rake test |
| 108 | + |
| 109 | +3. For any new classes or methods, RBS type signatures must be added (to sig/x.rbs). This can be verified with: |
| 110 | + |
| 111 | + bundle exec rake steep |
| 112 | + |
79 | 113 | ## License |
80 | 114 |
|
81 | 115 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
0 commit comments