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| Gem::Specification.new do |spec|
spec.add_dependency 'addressable', '~> 2.3' 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 'http', '>= 2', '< 6'
spec.add_dependency 'multi_json', '~> 1' spec.add_dependency 'multi_json', '~> 1'
spec.add_dependency 'simple_oauth', '~> 0.3' spec.add_dependency 'simple_oauth', '~> 0.3'
spec.add_dependency 'virtus', '~> 1'
spec.author = 'Steve Agalloco' spec.author = 'Steve Agalloco'
spec.description = "Ruby Client for Instapaper's Full API" spec.description = "Ruby Client for Instapaper's Full API"
spec.email = 'steve.agalloco@gmail.com' spec.email = 'steve.agalloco@gmail.com'

View file

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

View file

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

View file

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

View file

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

View file

@ -1,16 +1,16 @@
require 'virtus' require 'dry-struct'
require 'instapaper/types'
module Instapaper module Instapaper
class Highlight class Highlight < Dry::Struct
include Virtus.value_object include Types
transform_keys(&:to_sym)
values do attribute :type, Types::String
attribute :type, String attribute :highlight_id, Types::Integer
attribute :highlight_id, String attribute :bookmark_id, Types::Integer
attribute :bookmark_id, String attribute :text, Types::String
attribute :text, String attribute :position, Types::Integer
attribute :position, String attribute :time, Types::Integer.optional
attribute :time, String
end
end end
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 module Instapaper
class User class User < Dry::Struct
include Virtus.value_object include Types
transform_keys(&:to_sym)
values do attribute :username, Types::String
attribute :username, String attribute :user_id, Types::Integer
attribute :user_id, Integer attribute :type, Types::String
attribute :type, String attribute? :subscription_is_active, Types::BooleanFlag.optional
attribute :subscription_is_active, Axiom::Types::Boolean
end
end end
end end