Skip to content

Commit

Permalink
feat: validate with T::Struct
Browse files Browse the repository at this point in the history
close: #3
  • Loading branch information
eggplants committed Sep 18, 2023
1 parent d9f611b commit 325002f
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@

# rspec failure tracking
.rspec_status

.swp
31 changes: 21 additions & 10 deletions lib/tsumanne/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
require "mhtml"
require "sorbet-runtime"

require_relative "models/get_threads"
require_relative "models/search_thread_from_uri"
require_relative "models/search_indexes"
require_relative "models/register_thread"

# API module for tsumanne.net includes knowledge as const.
module Tsumanne
class API
Expand All @@ -23,11 +28,11 @@ def initialize(board_id:)
@board_id = T.let(T.must(BOARD_IDS[board_id]), String)
end

sig { params(index: String, page: Integer).returns(T::Hash[String, T.untyped]) }
sig { params(index: String, page: Integer).returns(GetThreadsResponse) }
def get_threads(index: "all", page: 1)
# https://tsumanne.net/si/all/1
# https://tsumanne.net/si/hoge/1
fetch_json(paths: [index, page.to_s])
GetThreadsResponse.from_hash(fetch_json(paths: [index, page.to_s]))
end

sig { params(thread_id: String).returns(Mhtml::RootDocument) }
Expand All @@ -52,23 +57,29 @@ def get_thread_from_path(thread_path)
get_thread_mht(T.must(match_data[:thread_id]))
end

sig { params(uri: URI).returns(T::Hash[String, T.untyped]) }
sig { params(uri: URI).returns(SearchThreadFromUriResponse) }
def search_thread_from_uri(uri)
# https://tsumanne.net/si/indexes.php?format=json&w=&sbmt=URL
fetch_json(paths: ["indexes.php"], query: { w: uri, sbmt: :URL })
# https://tsumanne.net/si/indexes.php?format=json&w=...&sbmt=URL
# https://tsumanne.net/si/indexes.php?format=json&w=https%3A%2F%2Fimg.2chan.net%2Fb%2Fres%2F86279902.htm&sbmt=URL
SearchThreadFromUriResponse.from_hash(
fetch_json(paths: ["indexes.php"], query: { w: uri, sbmt: :URL })
)
end

sig { params(keyword: T.nilable(String), order: Symbol, page: Integer).returns(T::Hash[String, T.untyped]) }
sig { params(keyword: T.nilable(String), order: Symbol, page: Integer).returns(SearchIndexesResponse) }
def search_indexes(keyword: nil, order: :newer, page: 1)
# https://tsumanne.net/si/indexes.php?format=json&w=&sbmt=URL
# https://tsumanne.net/si/indexes.php?format=json&w=&sbmt=%E2%86%93%E6%96%B0
fetch_json(paths: ["indexes.php"], query: { w: keyword, sbmt: INDEXES_ORDERS[order], p: page })
SearchIndexesResponse.from_hash(
fetch_json(paths: ["indexes.php"], query: { w: keyword, sbmt: INDEXES_ORDERS[order], p: page })
)
end

sig { params(uri: URI, indexes: T.nilable(T::Array[String])).returns(T::Hash[String, T.untyped]) }
sig { params(uri: URI, indexes: T.nilable(T::Array[String])).returns(RegisterThreadResponse) }
def register_thread(uri, indexes: nil)
# post, https://tsumanne.net/si/input.php?format=json&url=...&category=...
fetch_json(paths: ["input.php?format=json"], query: { url: uri, category: (indexes || []).join(",") }, method: :post)
RegisterThreadResponse.from_hash(
fetch_json(paths: ["input.php?format=json"], query: { url: uri, category: (indexes || []).join(",") }, method: :post)
)
end

private
Expand Down
34 changes: 34 additions & 0 deletions lib/tsumanne/models/get_threads.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# typed: strict

require "sorbet-runtime"

class GetThreadsResponse < T::Struct
extend T::Sig

class Log < T::Struct
extend T::Sig

const :id, Integer
const :url, String # URI
const :date, String # Time
const :close, T::Boolean
const :res, Integer
const :files, Integer
const :access, Integer
const :public, T::Boolean
const :text, String
const :thumb, String
const :del, Integer
const :atid, T::Boolean
const :last, String # Time
const :path, String
const :category, T::Array[String]
end

const :success, T::Boolean
const :messages, T::Array[String]
const :lastpage, Integer
const :count, Integer
const :cid, Integer
const :logs, T::Array[Log]
end
18 changes: 18 additions & 0 deletions lib/tsumanne/models/register_thread.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# typed: strict

require "sorbet-runtime"

class RegisterThreadResponse < T::Struct
extend T::Sig

class Tags < T::Struct
extend T::Sig

const :success, T::Boolean
const :messages, T::Array[String]
end

const :success, T::Boolean
const :messages, T::Array[String]
const :tags, Tags
end
18 changes: 18 additions & 0 deletions lib/tsumanne/models/search_indexes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# typed: strict

require "sorbet-runtime"

class SearchIndexesResponse < T::Struct
extend T::Sig

class Tag < T::Struct
extend T::Sig

const :tag, String
end

const :success, T::Boolean
const :messages, T::Array[String]
const :count, Integer
const :tags, T::Array[Tag]
end
11 changes: 11 additions & 0 deletions lib/tsumanne/models/search_thread_from_uri.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# typed: strict

require "sorbet-runtime"

class SearchThreadFromUriResponse < T::Struct
extend T::Sig

const :success, T::Boolean
const :messages, T::Array[String]
const :path, String
end
8 changes: 4 additions & 4 deletions spec/tsumanne_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
describe "#get_threads" do
let(:instance) { Tsumanne::API.new(board_id: :img) }
it "works" do
expect(instance.get_threads["success"]).to eql(true)
expect(instance.get_threads.success).to eql(true)
end
end

Expand All @@ -55,22 +55,22 @@
let(:instance) { Tsumanne::API.new(board_id: :img) }
it "works" do
target_uri = URI.parse("https://img.2chan.net/b/res/86279902.htm")
expect(instance.search_thread_from_uri(target_uri)["success"]).to eql(true)
expect(instance.search_thread_from_uri(target_uri).success).to eql(true)
end
end

describe "#search_indexes" do
let(:instance) { Tsumanne::API.new(board_id: :img) }
it "works" do
expect(instance.search_indexes(keyword: "深淵")["success"]).to eql(true)
expect(instance.search_indexes(keyword: "深淵").success).to eql(true)
end
end

describe "#register_thread" do
let(:instance) { Tsumanne::API.new(board_id: :img) }
it "works" do
target_uri = URI.parse("https://img.2chan.net/b/res/0.htm")
expect(instance.register_thread(target_uri)["success"]).to eql(true)
expect(instance.register_thread(target_uri).success).to eql(true)
end
end
end
Expand Down

0 comments on commit 325002f

Please sign in to comment.