changeset 260:e2150dce4e90

Rubocop (rails) time
author nanaya <me@nanaya.net>
date Sun, 15 Dec 2024 22:59:09 +0900
parents 8b75d00c77ba
children 8c47a69cbfdb
files .rubocop.yml Gemfile Gemfile.lock app/controllers/tweets_controller.rb app/helpers/application_helper.rb app/lib/legit_client.rb config/application.rb config/config_init.rb config/initializers/assets.rb config/initializers/ext_string.rb config/initializers/wrap_parameters.rb config/routes.rb test/controllers/tweets_controller_test.rb test/lib/legit_client_test.rb
diffstat 14 files changed, 127 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.rubocop.yml	Sun Dec 15 22:59:09 2024 +0900
@@ -0,0 +1,3 @@
+# Omakase Ruby styling for Rails
+inherit_gem:
+  rubocop-rails-omakase: rubocop.yml
--- a/Gemfile	Sun Dec 15 22:55:34 2024 +0900
+++ b/Gemfile	Sun Dec 15 22:59:09 2024 +0900
@@ -10,4 +10,6 @@
 
 gem "twitter-text"
 
-gem "puma", :require => false
+gem "puma", require: false
+
+gem "rubocop-rails-omakase", require: false, group: [ :development ]
--- a/Gemfile.lock	Sun Dec 15 22:55:34 2024 +0900
+++ b/Gemfile.lock	Sun Dec 15 22:59:09 2024 +0900
@@ -30,6 +30,7 @@
       securerandom (>= 0.3)
       tzinfo (~> 2.0, >= 2.0.5)
       uri (>= 0.13.1)
+    ast (2.4.2)
     base64 (0.2.0)
     benchmark (0.4.0)
     bigdecimal (3.1.8)
@@ -51,6 +52,9 @@
       rdoc (>= 4.0.0)
       reline (>= 0.4.2)
     jar-dependencies (0.5.1)
+    json (2.9.0)
+    json (2.9.0-java)
+    language_server-protocol (3.17.0.3)
     logger (1.6.3)
     loofah (2.23.1)
       crass (~> 1.0.2)
@@ -66,6 +70,10 @@
       racc (~> 1.4)
     nokogiri (1.17.2-x86-mingw32)
       racc (~> 1.4)
+    parallel (1.26.3)
+    parser (3.3.6.0)
+      ast (~> 2.4.1)
+      racc
     psych (5.2.1)
       date
       stringio
@@ -100,6 +108,7 @@
       rake (>= 12.2)
       thor (~> 1.0, >= 1.2.2)
       zeitwerk (~> 2.6)
+    rainbow (3.1.1)
     rake (13.2.1)
     rdoc (6.9.0)
       psych (>= 4.0.0)
@@ -107,8 +116,38 @@
       redis-client (>= 0.22.0)
     redis-client (0.23.0)
       connection_pool
+    regexp_parser (2.9.3)
     reline (0.5.12)
       io-console (~> 0.5)
+    rubocop (1.69.2)
+      json (~> 2.3)
+      language_server-protocol (>= 3.17.0)
+      parallel (~> 1.10)
+      parser (>= 3.3.0.2)
+      rainbow (>= 2.2.2, < 4.0)
+      regexp_parser (>= 2.9.3, < 3.0)
+      rubocop-ast (>= 1.36.2, < 2.0)
+      ruby-progressbar (~> 1.7)
+      unicode-display_width (>= 2.4.0, < 4.0)
+    rubocop-ast (1.37.0)
+      parser (>= 3.3.1.0)
+    rubocop-minitest (0.36.0)
+      rubocop (>= 1.61, < 2.0)
+      rubocop-ast (>= 1.31.1, < 2.0)
+    rubocop-performance (1.23.0)
+      rubocop (>= 1.48.1, < 2.0)
+      rubocop-ast (>= 1.31.1, < 2.0)
+    rubocop-rails (2.27.0)
+      activesupport (>= 4.2.0)
+      rack (>= 1.1)
+      rubocop (>= 1.52.0, < 2.0)
+      rubocop-ast (>= 1.31.1, < 2.0)
+    rubocop-rails-omakase (1.0.0)
+      rubocop
+      rubocop-minitest
+      rubocop-performance
+      rubocop-rails
+    ruby-progressbar (1.13.0)
     securerandom (0.4.0)
     stringio (3.1.2)
     thor (1.3.2)
@@ -121,6 +160,9 @@
       unf_ext
     unf (0.1.4-java)
     unf_ext (0.0.9.1)
+    unicode-display_width (3.1.2)
+      unicode-emoji (~> 4.0, >= 4.0.4)
+    unicode-emoji (4.0.4)
     uri (1.0.2)
     useragent (0.16.11)
     zeitwerk (2.7.1)
@@ -136,6 +178,7 @@
   puma
   railties (~> 8.0.0)
   redis
+  rubocop-rails-omakase
   twitter-text
 
 BUNDLED WITH
--- a/app/controllers/tweets_controller.rb	Sun Dec 15 22:55:34 2024 +0900
+++ b/app/controllers/tweets_controller.rb	Sun Dec 15 22:59:09 2024 +0900
@@ -1,6 +1,6 @@
 class TweetsController < ApplicationController
   def index
-    return redirect if params[:name].present?
+    redirect if params[:name].present?
   end
 
   def show
@@ -38,6 +38,6 @@
   private
 
   def normalized_screen_name
-    @user[:username].presence || '_'
+    @user[:username].presence || "_"
   end
 end
--- a/app/helpers/application_helper.rb	Sun Dec 15 22:55:34 2024 +0900
+++ b/app/helpers/application_helper.rb	Sun Dec 15 22:59:09 2024 +0900
@@ -10,9 +10,9 @@
       expanded = urls[url]
 
       case expanded
-        when nil then url
-        when Hash then expanded[:url]
-        else expanded
+      when nil then url
+      when Hash then expanded[:url]
+      else expanded
       end
     end
   end
--- a/app/lib/legit_client.rb	Sun Dec 15 22:55:34 2024 +0900
+++ b/app/lib/legit_client.rb	Sun Dec 15 22:59:09 2024 +0900
@@ -1,11 +1,11 @@
-require 'net/http'
+require "net/http"
 
 module LegitClient
   def self.timeline(user_id)
     resp = fetch("https://x.com/i/api/graphql/1-5o8Qhfc2kWlu_2rWNcug/UserTweetsAndReplies?variables=%7B%22userId%22%3A#{escape_param user_id}%2C%22count%22%3A50%2C%22includePromotedContent%22%3Atrue%2C%22withCommunity%22%3Atrue%2C%22withVoice%22%3Atrue%2C%22withV2Timeline%22%3Atrue%7D&features=%7B%22rweb_lists_timeline_redesign_enabled%22%3Atrue%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22tweetypie_unmention_optimization_enabled%22%3Atrue%2C%22responsive_web_edit_tweet_api_enabled%22%3Atrue%2C%22graphql_is_translatable_rweb_tweet_is_translatable_enabled%22%3Atrue%2C%22view_counts_everywhere_api_enabled%22%3Atrue%2C%22longform_notetweets_consumption_enabled%22%3Atrue%2C%22responsive_web_twitter_article_tweet_consumption_enabled%22%3Afalse%2C%22tweet_awards_web_tipping_enabled%22%3Afalse%2C%22freedom_of_speech_not_reach_fetch_enabled%22%3Atrue%2C%22standardized_nudges_misinfo%22%3Atrue%2C%22tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled%22%3Atrue%2C%22longform_notetweets_rich_text_read_enabled%22%3Atrue%2C%22longform_notetweets_inline_media_enabled%22%3Atrue%2C%22responsive_web_media_download_video_enabled%22%3Afalse%2C%22responsive_web_enhance_cards_enabled%22%3Afalse%7D&fieldToggles=%7B%22withAuxiliaryUserLabels%22%3Afalse%2C%22withArticleRichContentState%22%3Afalse%7D")
 
     handle_response resp, :timeline, "timeline(#{user_id})", ->(json) do
-      normalize_timeline json['data']['user']['result']['timeline_v2']['timeline']['instructions'], user_id
+      normalize_timeline json["data"]["user"]["result"]["timeline_v2"]["timeline"]["instructions"], user_id
     end
   end
 
@@ -13,7 +13,7 @@
     resp = fetch("https://x.com/i/api/graphql/i_0UQ54YrCyqLUvgGzXygA/UserByRestId?variables=%7B%22userId%22%3A#{escape_param user_id}%2C%22withSafetyModeUserFields%22%3Atrue%7D&features=%7B%22hidden_profile_likes_enabled%22%3Afalse%2C%22hidden_profile_subscriptions_enabled%22%3Afalse%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22highlights_tweets_tab_ui_enabled%22%3Atrue%2C%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%7D&fieldToggles=%7B%22withAuxiliaryUserLabels%22%3Afalse%7D")
 
     handle_response resp, :user, "user_by_id(#{user_id})", ->(json) do
-      normalize_user json['data']['user']['result']
+      normalize_user json["data"]["user"]["result"]
     end
   end
 
@@ -21,7 +21,7 @@
     resp = fetch("https://x.com/i/api/graphql/xc8f1g7BYqr6VTzTbvNlGw/UserByScreenName?variables=%7B%22screen_name%22%3A#{escape_param username}%2C%22withSafetyModeUserFields%22%3Atrue%7D&features=%7B%22hidden_profile_likes_enabled%22%3Afalse%2C%22hidden_profile_subscriptions_enabled%22%3Afalse%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22subscriptions_verification_info_verified_since_enabled%22%3Atrue%2C%22highlights_tweets_tab_ui_enabled%22%3Atrue%2C%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%7D&fieldToggles=%7B%22withAuxiliaryUserLabels%22%3Afalse%7D")
 
     handle_response resp, :user, "user_by_username(#{username})", ->(json) do
-      normalize_user json['data']['user']['result']
+      normalize_user json["data"]["user"]["result"]
     end
   end
 
@@ -37,13 +37,13 @@
     json = JSON.parse(resp)
     {
       key => callback.call(json),
-      raw: resp,
+      raw: resp
     }
   rescue => e
     if json.is_a? Hash
-      if json['errors'].is_a? Array
+      if json["errors"].is_a? Array
         return rate_limit_check(json)
-      elsif json['data'].is_a? Hash
+      elsif json["data"].is_a? Hash
         return
       end
     end
@@ -58,30 +58,30 @@
     json.each do |entity_media|
       val = {}
 
-      case entity_media['type']
-        when 'animated_gif', 'video'
-          val[:variants] = entity_media['video_info']['variants']
-            .filter { |variant| variant['bitrate'].present? }
+      case entity_media["type"]
+      when "animated_gif", "video"
+          val[:variants] = entity_media["video_info"]["variants"]
+            .filter { |variant| variant["bitrate"].present? }
             .map do |variant|
               {
-                bitrate: variant['bitrate'],
-                url: variant['url'],
+                bitrate: variant["bitrate"],
+                url: variant["url"]
               }
             end
-        when 'photo'
-          val[:image_url] = entity_media['media_url_https'].sub(/\.([^.]+)$/, '?format=\1')
+      when "photo"
+          val[:image_url] = entity_media["media_url_https"].sub(/\.([^.]+)$/, '?format=\1')
       end
 
       if !val.empty?
-        val[:url] = entity_media['expanded_url']
-        val[:type] = entity_media['type']
-        val[:id] = entity_media['media_key']
+        val[:url] = entity_media["expanded_url"]
+        val[:type] = entity_media["type"]
+        val[:id] = entity_media["media_key"]
       end
 
-      key = if ret[entity_media['url']].nil?
-        entity_media['url']
+      key = if ret[entity_media["url"]].nil?
+        entity_media["url"]
       else
-        entity_media['media_key']
+        entity_media["media_key"]
       end
 
       ret[key] = val
@@ -94,7 +94,7 @@
     ret = {}
 
     (json || {}).each do |entity_url|
-      ret[entity_url['url']] = entity_url['expanded_url']
+      ret[entity_url["url"]] = entity_url["expanded_url"]
     end
 
     ret
@@ -103,60 +103,60 @@
   def self.normalize_timeline(json, user_id)
     json
       .reduce([]) do |acc, instruction|
-        case instruction['type']
-        when 'TimelineAddEntries' then acc += instruction['entries']
-        when 'TimelinePinEntry' then acc << instruction['entry']
+        case instruction["type"]
+        when "TimelineAddEntries" then acc += instruction["entries"]
+        when "TimelinePinEntry" then acc << instruction["entry"]
         end
 
         acc
-      end.filter { |entry| entry['entryId'] =~ /\A(profile-conversation|tweet)-/ }
+      end.filter { |entry| entry["entryId"] =~ /\A(profile-conversation|tweet)-/ }
       .reduce([]) do |acc, entry|
-        if entry['content']['entryType'] == 'TimelineTimelineItem'
-          acc.push(entry['content'])
+        if entry["content"]["entryType"] == "TimelineTimelineItem"
+          acc.push(entry["content"])
         else
-          entry['content']['items'].each do |item|
-            acc.push(item['item'])
+          entry["content"]["items"].each do |item|
+            acc.push(item["item"])
           end
         end
         acc
-      end.map { |raw_tweet| normalize_tweet(raw_tweet['itemContent']['tweet_results']['result']) }
+      end.map { |raw_tweet| normalize_tweet(raw_tweet["itemContent"]["tweet_results"]["result"]) }
       .filter { |tweet| !tweet.nil? && tweet.dig(:user, :id) == user_id }
   end
 
   def self.normalize_tweet(json)
     return nil if json.nil?
 
-    return normalize_tweet(json['tweet']) if json['__typename'] == 'TweetWithVisibilityResults'
+    return normalize_tweet(json["tweet"]) if json["__typename"] == "TweetWithVisibilityResults"
 
     {
-      id: json['rest_id'],
-      created_at: Time.parse(json['legacy']['created_at']),
-      user: normalize_user(json['core']['user_results']['result']),
-      message: json.dig('note_tweet', 'note_tweet_results', 'result', 'text') || json['legacy']['full_text'],
-      retweet: normalize_tweet(json.dig('legacy', 'retweeted_status_result', 'result')),
-      quote: normalize_tweet(json.dig('quoted_status_result', 'result')),
-      quote_id: json['legacy']['quoted_status_id_str'],
-      reply_to_id: json['legacy']['in_reply_to_status_id_str'],
-      reply_to_user_id: json['legacy']['in_reply_to_user_id_str'],
-      reply_to_username: json['legacy']['in_reply_to_screen_name'],
-      entity_urls: { **normalize_entity_urls(json['legacy']['entities']['urls']), **normalize_entity_urls(json.dig('note_tweet', 'note_tweet_results', 'result', 'entity_set', 'urls')) },
-      entity_media: normalize_entity_media(json.dig('legacy', 'extended_entities', 'media') || []),
+      id: json["rest_id"],
+      created_at: Time.parse(json["legacy"]["created_at"]),
+      user: normalize_user(json["core"]["user_results"]["result"]),
+      message: json.dig("note_tweet", "note_tweet_results", "result", "text") || json["legacy"]["full_text"],
+      retweet: normalize_tweet(json.dig("legacy", "retweeted_status_result", "result")),
+      quote: normalize_tweet(json.dig("quoted_status_result", "result")),
+      quote_id: json["legacy"]["quoted_status_id_str"],
+      reply_to_id: json["legacy"]["in_reply_to_status_id_str"],
+      reply_to_user_id: json["legacy"]["in_reply_to_user_id_str"],
+      reply_to_username: json["legacy"]["in_reply_to_screen_name"],
+      entity_urls: { **normalize_entity_urls(json["legacy"]["entities"]["urls"]), **normalize_entity_urls(json.dig("note_tweet", "note_tweet_results", "result", "entity_set", "urls")) },
+      entity_media: normalize_entity_media(json.dig("legacy", "extended_entities", "media") || [])
     }
   end
 
   def self.normalize_user(json)
     {
-      avatar_url: json['legacy']['profile_image_url_https'],
-      id: json['rest_id'],
-      name: json['legacy']['name'],
-      protected: json['legacy']['protected'] == true,
-      username: json['legacy']['screen_name'],
+      avatar_url: json["legacy"]["profile_image_url_https"],
+      id: json["rest_id"],
+      name: json["legacy"]["name"],
+      protected: json["legacy"]["protected"] == true,
+      username: json["legacy"]["screen_name"]
     }
   end
 
   def self.rate_limit_check(json)
-    return unless json['errors'].any? { |err| err['code'] == 88 }
+    return unless json["errors"].any? { |err| err["code"] == 88 }
 
-    raise 'Rate limited!'
+    raise "Rate limited!"
   end
 end
--- a/config/application.rb	Sun Dec 15 22:55:34 2024 +0900
+++ b/config/application.rb	Sun Dec 15 22:59:09 2024 +0900
@@ -2,7 +2,7 @@
 
 require "rails"
 # Pick the frameworks you want:
-#require "active_model/railtie"
+# require "active_model/railtie"
 # require "active_job/railtie"
 # require "active_record/railtie"
 # require "active_storage/engine"
--- a/config/config_init.rb	Sun Dec 15 22:55:34 2024 +0900
+++ b/config/config_init.rb	Sun Dec 15 22:59:09 2024 +0900
@@ -1,13 +1,13 @@
 $cfg = {
-  :twitter => [
+  twitter: [
     {
-      :consumer_key => ENV["RT_CONSUMER_KEY"],
-      :consumer_secret => ENV["RT_CONSUMER_SECRET"],
-      :access_token => ENV["RT_ACCESS_TOKEN"],
-      :access_token_secret => ENV["RT_ACCESS_TOKEN_SECRET"],
-    },
+      consumer_key: ENV["RT_CONSUMER_KEY"],
+      consumer_secret: ENV["RT_CONSUMER_SECRET"],
+      access_token: ENV["RT_ACCESS_TOKEN"],
+      access_token_secret: ENV["RT_ACCESS_TOKEN_SECRET"]
+    }
   ],
-  :redis_server => ENV["RT_REDIS_SERVER"]
+  redis_server: ENV["RT_REDIS_SERVER"]
 }
 
 config_local = File.expand_path("../config_local_#{Rails.env}.rb", __FILE__)
--- a/config/initializers/assets.rb	Sun Dec 15 22:55:34 2024 +0900
+++ b/config/initializers/assets.rb	Sun Dec 15 22:59:09 2024 +0900
@@ -1,7 +1,7 @@
 # Be sure to restart your server when you modify this file.
 
 # Version of your assets, change this if you want to expire all your assets.
-#Rails.application.config.assets.version = "1.0"
+# Rails.application.config.assets.version = "1.0"
 
 # Add additional assets to the asset load path.
 # Rails.application.config.assets.paths << Emoji.images_path
--- a/config/initializers/ext_string.rb	Sun Dec 15 22:55:34 2024 +0900
+++ b/config/initializers/ext_string.rb	Sun Dec 15 22:59:09 2024 +0900
@@ -1,5 +1,5 @@
 class String
   def printable
-    gsub(/[^\n[:print:]]/, '')
+    gsub(/[^\n[:print:]]/, "")
   end
 end
--- a/config/initializers/wrap_parameters.rb	Sun Dec 15 22:55:34 2024 +0900
+++ b/config/initializers/wrap_parameters.rb	Sun Dec 15 22:59:09 2024 +0900
@@ -5,5 +5,5 @@
 
 # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
 ActiveSupport.on_load(:action_controller) do
-  wrap_parameters format: [:json]
+  wrap_parameters format: [ :json ]
 end
--- a/config/routes.rb	Sun Dec 15 22:55:34 2024 +0900
+++ b/config/routes.rb	Sun Dec 15 22:59:09 2024 +0900
@@ -1,6 +1,6 @@
 Rails.application.routes.draw do
   root "tweets#index"
 
-  get ":id/:name" => "tweets#show", :defaults => { :format => :atom }, :as => "tweet"
+  get ":id/:name" => "tweets#show", :defaults => { format: :atom }, :as => "tweet"
   get ":name" => "tweets#redirect"
 end
--- a/test/controllers/tweets_controller_test.rb	Sun Dec 15 22:55:34 2024 +0900
+++ b/test/controllers/tweets_controller_test.rb	Sun Dec 15 22:59:09 2024 +0900
@@ -2,17 +2,17 @@
 
 class TweetsControllerTest < ActionController::TestCase
   test "should get show" do
-    get :show, :params => { :id => "2791517370", :name => "edogawa_test", :format => :atom }
+    get :show, params: { id: "2791517370", name: "edogawa_test", format: :atom }
     assert_response :success
   end
 
   test "does not explode on empty timeline" do
-    get :show, :params => { :id => "1519673756624379905", :name => "nanaya_empty2", :format => :atom }
+    get :show, params: { id: "1519673756624379905", name: "nanaya_empty2", format: :atom }
     assert_response :success
   end
 
   test "redirect on @handle access" do
-    get :redirect, :params => { :name => "edogawa_test", :format => :atom }
+    get :redirect, params: { name: "edogawa_test", format: :atom }
     assert_redirected_to "/2791517370/edogawa_test"
   end
 
--- a/test/lib/legit_client_test.rb	Sun Dec 15 22:55:34 2024 +0900
+++ b/test/lib/legit_client_test.rb	Sun Dec 15 22:59:09 2024 +0900
@@ -1,15 +1,15 @@
-require 'test_helper'
+require "test_helper"
 
 class LegitClientTest < ActiveSupport::TestCase
-  test 'loads timeline' do
-    assert_not_nil LegitClient.timeline('2791517370')
+  test "loads timeline" do
+    assert_not_nil LegitClient.timeline("2791517370")
   end
 
-  test 'loads user_by_id' do
-    assert_not_nil LegitClient.user_by_id('2791517370')
+  test "loads user_by_id" do
+    assert_not_nil LegitClient.user_by_id("2791517370")
   end
 
-  test 'loads user_by_username' do
-    assert_not_nil LegitClient.user_by_username('edogawa_test')
+  test "loads user_by_username" do
+    assert_not_nil LegitClient.user_by_username("edogawa_test")
   end
 end