From 77283ab3c2bed257337503d1f31cb3d29f68f417 Mon Sep 17 00:00:00 2001 From: Forest Carlisle Date: Thu, 12 Jan 2017 15:41:30 -0800 Subject: [PATCH 1/3] WIP: move to dry --- .gitignore | 1 + lib/tracker_api.rb | 4 +-- lib/tracker_api/resources/me.rb | 32 ++++++++++---------- lib/tracker_api/resources/shared/base_dry.rb | 17 +++++++++++ lib/tracker_api/resources/shared/types.rb | 13 ++++++++ tracker_api.gemspec | 4 ++- 6 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 lib/tracker_api/resources/shared/base_dry.rb create mode 100644 lib/tracker_api/resources/shared/types.rb diff --git a/.gitignore b/.gitignore index c352266..7a48c16 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ .yardoc .idea .vscode +.rubocop.yml bin .DS_Store Gemfile.lock diff --git a/lib/tracker_api.rb b/lib/tracker_api.rb index 86fe10e..d201fcd 100644 --- a/lib/tracker_api.rb +++ b/lib/tracker_api.rb @@ -9,7 +9,6 @@ else require 'core_ext/object/blank' end -require 'equalizer' require 'multi_json' require 'representable/json' @@ -59,7 +58,8 @@ module Endpoints module Resources module Shared autoload :Base, 'tracker_api/resources/shared/base' - autoload :Collection, 'tracker_api/resources/shared/collection' + autoload :Types, 'tracker_api/resources/shared/types' + autoload :BaseDry, 'tracker_api/resources/shared/base_dry' end autoload :Activity, 'tracker_api/resources/activity' autoload :Account, 'tracker_api/resources/account' diff --git a/lib/tracker_api/resources/me.rb b/lib/tracker_api/resources/me.rb index 5e3ea32..9f55f11 100644 --- a/lib/tracker_api/resources/me.rb +++ b/lib/tracker_api/resources/me.rb @@ -1,21 +1,21 @@ +require_relative './shared/types' + 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 < Shared::BaseDry + attribute :name, Shared::Types::Coercible::String + attribute :initials, Shared::Types::Coercible::String + attribute :username, Shared::Types::Coercible::String + # attribute :time_zone, Shared::Types::Coercible::TimeZone + attribute :api_token, Shared::Types::Coercible::String + attribute :has_google_identity, Shared::Types::Strict::Bool + attribute :project_ids, Shared::Types::Strict::Array.member(Shared::Types::Coercible::Int) + attribute :projects, Shared::Types::Strict::Array.member(MembershipSummary) + attribute :workspace_ids, Shared::Types::Strict::Array.member(Shared::Types::Coercible::Int) + attribute :workspaces, Shared::Types::Strict::Array.member(Workspace) + attribute :email, Shared::Types::Coercible::String + attribute :receives_in_app_notifications, Shared::Types::Strict::Bool + attribute :kind, Shared::Types::Coercible::String end end end diff --git a/lib/tracker_api/resources/shared/base_dry.rb b/lib/tracker_api/resources/shared/base_dry.rb new file mode 100644 index 0000000..216ff8b --- /dev/null +++ b/lib/tracker_api/resources/shared/base_dry.rb @@ -0,0 +1,17 @@ +require 'dry-struct' +require_relative 'types' + +module TrackerApi + + module Resources + module Shared + class BaseDry < Dry::Struct + constructor_type :schema + + include Dry::Equalizer(:id) + + attribute :id, Types::Coercible::Int + end + end + end +end diff --git a/lib/tracker_api/resources/shared/types.rb b/lib/tracker_api/resources/shared/types.rb new file mode 100644 index 0000000..2ffb567 --- /dev/null +++ b/lib/tracker_api/resources/shared/types.rb @@ -0,0 +1,13 @@ +require 'dry-types' + +module TrackerApi + module Resources + module Shared + module Types + include Dry::Types.module + + # Statuses = Types::Strict::String.enum('draft', 'published', 'archived') + end + end + end +end diff --git a/tracker_api.gemspec b/tracker_api.gemspec index d513273..552902b 100644 --- a/tracker_api.gemspec +++ b/tracker_api.gemspec @@ -28,10 +28,12 @@ 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' end From e101953cdb521b2f4b9685b9053d26b09ea6e922 Mon Sep 17 00:00:00 2001 From: Forest Carlisle Date: Wed, 18 Jan 2017 15:59:40 -0800 Subject: [PATCH 2/3] Use Oj for parsing response json * Need to symbolize keys to make Dry::Struct happy. --- lib/tracker_api.rb | 4 ++-- lib/tracker_api/client.rb | 13 ++++++++++++- lib/tracker_api/resources/me.rb | 4 ++-- .../resources/membership_summary.rb | 18 +++++++++--------- tracker_api.gemspec | 1 + 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/tracker_api.rb b/lib/tracker_api.rb index d201fcd..54d1c4a 100644 --- a/lib/tracker_api.rb +++ b/lib/tracker_api.rb @@ -11,6 +11,7 @@ end require 'multi_json' +require 'oj' require 'representable/json' # stdlib @@ -18,8 +19,7 @@ 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' diff --git a/lib/tracker_api/client.rb b/lib/tracker_api/client.rb index c092451..af63b3e 100644 --- a/lib/tracker_api/client.rb +++ b/lib/tracker_api/client.rb @@ -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 @@ -285,4 +285,15 @@ def next_page_params end end end + + require 'faraday_middleware/response_middleware' + class ParseJsonWithSymbols < FaradayMiddleware::ResponseMiddleware + 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 diff --git a/lib/tracker_api/resources/me.rb b/lib/tracker_api/resources/me.rb index 9f55f11..3751833 100644 --- a/lib/tracker_api/resources/me.rb +++ b/lib/tracker_api/resources/me.rb @@ -6,13 +6,13 @@ class Me < Shared::BaseDry attribute :name, Shared::Types::Coercible::String attribute :initials, Shared::Types::Coercible::String attribute :username, Shared::Types::Coercible::String - # attribute :time_zone, Shared::Types::Coercible::TimeZone + # attribute :time_zone, TimeZone attribute :api_token, Shared::Types::Coercible::String attribute :has_google_identity, Shared::Types::Strict::Bool attribute :project_ids, Shared::Types::Strict::Array.member(Shared::Types::Coercible::Int) attribute :projects, Shared::Types::Strict::Array.member(MembershipSummary) attribute :workspace_ids, Shared::Types::Strict::Array.member(Shared::Types::Coercible::Int) - attribute :workspaces, Shared::Types::Strict::Array.member(Workspace) + # attribute :workspaces, Shared::Types::Strict::Array.member(Workspace) attribute :email, Shared::Types::Coercible::String attribute :receives_in_app_notifications, Shared::Types::Strict::Bool attribute :kind, Shared::Types::Coercible::String diff --git a/lib/tracker_api/resources/membership_summary.rb b/lib/tracker_api/resources/membership_summary.rb index 56c4c6a..b208b36 100644 --- a/lib/tracker_api/resources/membership_summary.rb +++ b/lib/tracker_api/resources/membership_summary.rb @@ -1,14 +1,14 @@ +require_relative './shared/types' + 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 < Shared::BaseDry + attribute :kind, Shared::Types::Coercible::String + attribute :last_viewed_at, Shared::Types::DateTime + attribute :project_color, Shared::Types::Coercible::String + attribute :project_id, Shared::Types::Coercible::Int + attribute :project_name, Shared::Types::Coercible::String + attribute :role, Shared::Types::Coercible::String end end end diff --git a/tracker_api.gemspec b/tracker_api.gemspec index 552902b..ccc6d68 100644 --- a/tracker_api.gemspec +++ b/tracker_api.gemspec @@ -36,4 +36,5 @@ Gem::Specification.new do |spec| # spec.add_dependency 'equalizer' spec.add_dependency 'representable' spec.add_dependency 'multi_json' + spec.add_dependency 'oj' end From 0c5619c72c398544573c709bcfb5d76a86d4c343 Mon Sep 17 00:00:00 2001 From: Forest Carlisle Date: Wed, 18 Jan 2017 16:06:33 -0800 Subject: [PATCH 3/3] Code reorg --- lib/tracker_api.rb | 4 +-- lib/tracker_api/resources/me.rb | 28 +++++++++---------- .../resources/membership_summary.rb | 16 +++++------ lib/tracker_api/resources/resource.rb | 14 ++++++++++ lib/tracker_api/resources/shared/base_dry.rb | 17 ----------- lib/tracker_api/resources/shared/types.rb | 13 --------- lib/tracker_api/resources/types.rb | 11 ++++++++ 7 files changed, 47 insertions(+), 56 deletions(-) create mode 100644 lib/tracker_api/resources/resource.rb delete mode 100644 lib/tracker_api/resources/shared/base_dry.rb delete mode 100644 lib/tracker_api/resources/shared/types.rb create mode 100644 lib/tracker_api/resources/types.rb diff --git a/lib/tracker_api.rb b/lib/tracker_api.rb index 54d1c4a..1a1ba88 100644 --- a/lib/tracker_api.rb +++ b/lib/tracker_api.rb @@ -58,9 +58,9 @@ module Endpoints module Resources module Shared autoload :Base, 'tracker_api/resources/shared/base' - autoload :Types, 'tracker_api/resources/shared/types' - autoload :BaseDry, 'tracker_api/resources/shared/base_dry' 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' diff --git a/lib/tracker_api/resources/me.rb b/lib/tracker_api/resources/me.rb index 3751833..3f6b67d 100644 --- a/lib/tracker_api/resources/me.rb +++ b/lib/tracker_api/resources/me.rb @@ -1,21 +1,19 @@ -require_relative './shared/types' - module TrackerApi module Resources - class Me < Shared::BaseDry - attribute :name, Shared::Types::Coercible::String - attribute :initials, Shared::Types::Coercible::String - attribute :username, Shared::Types::Coercible::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, Shared::Types::Coercible::String - attribute :has_google_identity, Shared::Types::Strict::Bool - attribute :project_ids, Shared::Types::Strict::Array.member(Shared::Types::Coercible::Int) - attribute :projects, Shared::Types::Strict::Array.member(MembershipSummary) - attribute :workspace_ids, Shared::Types::Strict::Array.member(Shared::Types::Coercible::Int) - # attribute :workspaces, Shared::Types::Strict::Array.member(Workspace) - attribute :email, Shared::Types::Coercible::String - attribute :receives_in_app_notifications, Shared::Types::Strict::Bool - attribute :kind, Shared::Types::Coercible::String + 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 diff --git a/lib/tracker_api/resources/membership_summary.rb b/lib/tracker_api/resources/membership_summary.rb index b208b36..dc583fc 100644 --- a/lib/tracker_api/resources/membership_summary.rb +++ b/lib/tracker_api/resources/membership_summary.rb @@ -1,14 +1,12 @@ -require_relative './shared/types' - module TrackerApi module Resources - class MembershipSummary < Shared::BaseDry - attribute :kind, Shared::Types::Coercible::String - attribute :last_viewed_at, Shared::Types::DateTime - attribute :project_color, Shared::Types::Coercible::String - attribute :project_id, Shared::Types::Coercible::Int - attribute :project_name, Shared::Types::Coercible::String - attribute :role, Shared::Types::Coercible::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 diff --git a/lib/tracker_api/resources/resource.rb b/lib/tracker_api/resources/resource.rb new file mode 100644 index 0000000..aa7496f --- /dev/null +++ b/lib/tracker_api/resources/resource.rb @@ -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 diff --git a/lib/tracker_api/resources/shared/base_dry.rb b/lib/tracker_api/resources/shared/base_dry.rb deleted file mode 100644 index 216ff8b..0000000 --- a/lib/tracker_api/resources/shared/base_dry.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'dry-struct' -require_relative 'types' - -module TrackerApi - - module Resources - module Shared - class BaseDry < Dry::Struct - constructor_type :schema - - include Dry::Equalizer(:id) - - attribute :id, Types::Coercible::Int - end - end - end -end diff --git a/lib/tracker_api/resources/shared/types.rb b/lib/tracker_api/resources/shared/types.rb deleted file mode 100644 index 2ffb567..0000000 --- a/lib/tracker_api/resources/shared/types.rb +++ /dev/null @@ -1,13 +0,0 @@ -require 'dry-types' - -module TrackerApi - module Resources - module Shared - module Types - include Dry::Types.module - - # Statuses = Types::Strict::String.enum('draft', 'published', 'archived') - end - end - end -end diff --git a/lib/tracker_api/resources/types.rb b/lib/tracker_api/resources/types.rb new file mode 100644 index 0000000..8e27b48 --- /dev/null +++ b/lib/tracker_api/resources/types.rb @@ -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