Module: ApiStruct::Extensions::DryMonads

Included in:
ApiStruct::Entity
Defined in:
lib/api_struct/extensions/dry_monads.rb

Instance Method Summary collapse

Instance Method Details

#from_failure(error) ⇒ Object



15
16
17
18
19
# File 'lib/api_struct/extensions/dry_monads.rb', line 15

def from_failure(error)
  ApiStruct::Errors::Entity.new(
    { status: error.status, body: error.body, error: true }, false
  )
end

#from_monad(monad) ⇒ Object



4
5
6
7
# File 'lib/api_struct/extensions/dry_monads.rb', line 4

def from_monad(monad)
  monad
    .fmap { |v| from_success(v) }.or_fmap { |e| from_failure(e) }.value!
end

#from_success(value) ⇒ Object



9
10
11
12
13
# File 'lib/api_struct/extensions/dry_monads.rb', line 9

def from_success(value)
  return Dry::Monads::Success(nil) if value.nil?

  value.is_a?(Array) ? collection(value) : new(value)
end