Merge pull request #4 from samsonjs/migrate-to-dry-struct

Migrate from virtus to dry-rb
This commit is contained in:
Sami Samhuri 2025-06-08 17:11:55 -07:00 committed by GitHub
commit e119e41ad3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 88 additions and 64 deletions

View file

@ -4,10 +4,11 @@ require 'instapaper/version'
Gem::Specification.new do |spec|
spec.add_dependency 'addressable', '~> 2.3'
spec.add_dependency 'dry-struct', '~> 1.0'
spec.add_dependency 'dry-types', '~> 1.0'
spec.add_dependency 'http', '>= 2', '< 6'
spec.add_dependency 'multi_json', '~> 1'
spec.add_dependency 'simple_oauth', '~> 0.3'
spec.add_dependency 'virtus', '~> 1'
spec.author = 'Steve Agalloco'
spec.description = "Ruby Client for Instapaper's Full API"
spec.email = 'steve.agalloco@gmail.com'

View file

@ -1,21 +1,21 @@
require 'virtus'
require 'dry-struct'
require 'instapaper/types'
module Instapaper
class Bookmark
include Virtus.value_object
class Bookmark < Dry::Struct
include Types
transform_keys(&:to_sym)
values do
attribute :instapaper_hash, String
attribute :description, String
attribute :bookmark_id, Integer
attribute :private_source, String
attribute :title, String
attribute :url, String
attribute :progress_timestamp, DateTime
attribute :time, DateTime
attribute :progress, String
attribute :starred, String
attribute :type, String
end
attribute :type, Types::String
attribute :bookmark_id, Types::Integer
attribute :url, Types::String
attribute :title, Types::String
attribute? :description, Types::String
attribute? :instapaper_hash, Types::String
attribute? :private_source, Types::String
attribute? :progress_timestamp, Types::Integer.optional
attribute? :time, Types::Integer.optional
attribute? :progress, Types::StringOrInteger
attribute? :starred, Types::StringOrInteger
end
end

View file

@ -1,17 +1,18 @@
require 'dry-struct'
require 'instapaper/types'
require 'instapaper/bookmark'
require 'instapaper/highlight'
require 'instapaper/user'
module Instapaper
class BookmarkList
include Virtus.value_object
class BookmarkList < Dry::Struct
include Types
transform_keys(&:to_sym)
values do
attribute :user, Instapaper::User
attribute :bookmarks, [Instapaper::Bookmark]
attribute :highlights, [Instapaper::Highlight]
attribute :delete_ids, [Integer]
end
attribute :user, Instapaper::User
attribute :bookmarks, Types::Array.of(Instapaper::Bookmark)
attribute :highlights, Types::Array.of(Instapaper::Highlight)
attribute? :delete_ids, Types::Array.of(Types::Integer).optional.default([].freeze)
def each(&block)
bookmarks.each(&block)

View file

@ -1,12 +1,12 @@
require 'virtus'
require 'dry-struct'
require 'instapaper/types'
module Instapaper
class Credentials
include Virtus.value_object
class Credentials < Dry::Struct
include Types
transform_keys(&:to_sym)
values do
attribute :oauth_token, String
attribute :oauth_token_secret, String
end
attribute :oauth_token, Types::String
attribute :oauth_token_secret, Types::String
end
end

View file

@ -1,17 +1,17 @@
require 'virtus'
require 'dry-struct'
require 'instapaper/types'
module Instapaper
class Folder
include Virtus.value_object
class Folder < Dry::Struct
include Types
transform_keys(&:to_sym)
values do
attribute :title, String
attribute :display_title, String
attribute :sync_to_mobile, Axiom::Types::Boolean
attribute :folder_id, Integer
attribute :position, String
attribute :type, String
attribute :slug, String
end
attribute :title, Types::String
attribute? :display_title, Types::String
attribute :sync_to_mobile, Types::BooleanFlag
attribute :folder_id, Types::Integer
attribute :position, Types::Coercible::Float
attribute :type, Types::String
attribute? :slug, Types::String
end
end

View file

@ -1,16 +1,16 @@
require 'virtus'
require 'dry-struct'
require 'instapaper/types'
module Instapaper
class Highlight
include Virtus.value_object
class Highlight < Dry::Struct
include Types
transform_keys(&:to_sym)
values do
attribute :type, String
attribute :highlight_id, String
attribute :bookmark_id, String
attribute :text, String
attribute :position, String
attribute :time, String
end
attribute :type, Types::String
attribute :highlight_id, Types::Integer
attribute :bookmark_id, Types::Integer
attribute :text, Types::String
attribute :position, Types::Integer
attribute :time, Types::Integer.optional
end
end

22
lib/instapaper/types.rb Normal file
View file

@ -0,0 +1,22 @@
require 'dry-types'
module Instapaper
module Types
include Dry.Types()
# Coerces any value to string (replaces custom StringOrInteger union type)
StringOrInteger = Types::Coercible::String
# Handles boolean flags from API that come as "0"/"1" strings or 0/1 integers.
BooleanFlag = Types::Constructor(Types::Bool) do |value|
case value
when '1', 1, 'true', true
true
when '0', 0, 'false', false, nil
false
else
!!value
end
end
end
end

View file

@ -1,14 +1,14 @@
require 'virtus'
require 'dry-struct'
require 'instapaper/types'
module Instapaper
class User
include Virtus.value_object
class User < Dry::Struct
include Types
transform_keys(&:to_sym)
values do
attribute :username, String
attribute :user_id, Integer
attribute :type, String
attribute :subscription_is_active, Axiom::Types::Boolean
end
attribute :username, Types::String
attribute :user_id, Types::Integer
attribute :type, Types::String
attribute? :subscription_is_active, Types::BooleanFlag.optional
end
end