Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Move to dry-rb and remove Virtus #98

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.yardoc
.idea
.vscode
.rubocop.yml
bin
.DS_Store
Gemfile.lock
Expand Down
8 changes: 4 additions & 4 deletions lib/tracker_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
else
require 'core_ext/object/blank'
end
require 'equalizer'

require 'multi_json'
require 'oj'
require 'representable/json'

# stdlib
require 'addressable/uri'
require 'forwardable'
require 'logger'

MultiJson.load_options = {:mode => :compat}
MultiJson.dump_options = {:mode => :compat}
Oj.default_options = {:mode => :compat }

module TrackerApi
autoload :Error, 'tracker_api/error'
Expand Down Expand Up @@ -59,8 +58,9 @@ module Endpoints
module Resources
module Shared
autoload :Base, 'tracker_api/resources/shared/base'
autoload :Collection, 'tracker_api/resources/shared/collection'
end
autoload :Types, 'tracker_api/resources/types'
autoload :Resource, 'tracker_api/resources/resource'
autoload :Activity, 'tracker_api/resources/activity'
autoload :Account, 'tracker_api/resources/account'
autoload :Change, 'tracker_api/resources/change'
Expand Down
13 changes: 12 additions & 1 deletion lib/tracker_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def initialize(options={}, &block)
@connection = Faraday.new({ url: @url }.merge(connection_options)) do |builder|
# response
builder.use Faraday::Response::RaiseError
builder.response :json
builder.use ParseJsonWithSymbols

# request
builder.request :multipart
Expand Down Expand Up @@ -285,4 +285,15 @@ def next_page_params
end
end
end

require 'faraday_middleware/response_middleware'
class ParseJsonWithSymbols < FaradayMiddleware::ResponseMiddleware
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dry::Struct expects keys to be symbols when creating the Dry::Struct based class from a hash (e.g. parsed JSON).

dependency do
require 'Oj' unless defined?(Oj)
end

define_parser do |body|
Oj.load(body, symbol_keys: true) unless body.strip.empty?
end
end
end
30 changes: 14 additions & 16 deletions lib/tracker_api/resources/me.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
module TrackerApi
module Resources
class Me
include Shared::Base

attribute :name, String
attribute :initials, String
attribute :username, String
attribute :time_zone, TimeZone
attribute :api_token, String
attribute :has_google_identity, Boolean
attribute :project_ids, [Integer]
attribute :projects, [MembershipSummary]
attribute :workspace_ids, [Integer]
attribute :workspaces, [Workspace]
attribute :email, String
attribute :receives_in_app_notifications, Boolean
attribute :kind, String
class Me < Resource
attribute :name, Types::Coercible::String
attribute :initials, Types::Coercible::String
attribute :username, Types::Coercible::String
# attribute :time_zone, TimeZone
attribute :api_token, Types::Coercible::String
attribute :has_google_identity, Types::Strict::Bool
attribute :project_ids, Types::Strict::Array.member(Types::Coercible::Int)
attribute :projects, Types::Strict::Array.member(MembershipSummary)
attribute :workspace_ids, Types::Strict::Array.member(Types::Coercible::Int)
# attribute :workspaces, Types::Strict::Array.member(Workspace)
attribute :email, Types::Coercible::String
attribute :receives_in_app_notifications, Types::Strict::Bool
attribute :kind, Types::Coercible::String
end
end
end
16 changes: 7 additions & 9 deletions lib/tracker_api/resources/membership_summary.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module TrackerApi
module Resources
class MembershipSummary
include Shared::Base

attribute :kind, String
attribute :last_viewed_at, DateTime
attribute :project_color, String
attribute :project_id, Integer
attribute :project_name, String
attribute :role, String
class MembershipSummary < Resource
attribute :kind, Types::Coercible::String
attribute :last_viewed_at, Types::DateTime
attribute :project_color, Types::Coercible::String
attribute :project_id, Types::Coercible::Int
attribute :project_name, Types::Coercible::String
attribute :role, Types::Coercible::String
end
end
end
14 changes: 14 additions & 0 deletions lib/tracker_api/resources/resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'dry-struct'

module TrackerApi
module Resources
# Base class for all resources
class Resource < Dry::Struct
constructor_type :schema

include Dry::Equalizer(:id)

attribute :id, Types::Coercible::Int
end
end
end
11 changes: 11 additions & 0 deletions lib/tracker_api/resources/types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'dry-types'

module TrackerApi
module Resources
module Types
include Dry::Types.module

# Statuses = Types::Strict::String.enum('draft', 'published', 'archived')
end
end
end
5 changes: 4 additions & 1 deletion tracker_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ Gem::Specification.new do |spec|

spec.add_dependency 'addressable'
spec.add_dependency 'virtus'
spec.add_dependency 'dry-struct'
spec.add_dependency 'dry-equalizer'
spec.add_dependency 'faraday', '~> 0.9.0'
spec.add_dependency 'faraday_middleware'
spec.add_dependency 'excon'
spec.add_dependency 'equalizer'
# spec.add_dependency 'equalizer'
spec.add_dependency 'representable'
spec.add_dependency 'multi_json'
spec.add_dependency 'oj'
end