Sha256: 149a7a745f7f6a600c548af777d260413cbc0f9120006fe08b60c53375aca6be

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

module Centaman
  class Service::Member < Centaman::Service
    include Centaman::JsonWrapper

    attr_reader :member_code, :email

    def after_init(args)
      @member_code = args.fetch(:member_code, nil).try(:to_i)
      @email = args.fetch(:email, nil)
      required_args
    end

    def endpoint
      member_code ? member_endpoint(:member_code) : member_endpoint(:email)
    end

    def object_class
      Centaman::Object::Member
    end

    def build_object(resp)
      return build_objects(resp) if resp.respond_to?(:map)

      return not_found(resp) unless resp.respond_to?(:merge)
      @build_object ||= object_class.new(resp.merge(additional_hash_to_serialize_after_response))
    end

    def self.find(id)
      obj = new(member_code: id)
      obj.objects.detect { |obj| obj.id == id }
    end

    private

    def member_endpoint(attr_key)
      attr_val = self.send(attr_key)
      endpoint_url(attr_key, attr_val)
    end

    def endpoint_url(key, val)
      endpoint_options = {
        member_code: "/member_services/Member/#{val}",
        email: "/member_services/Member?email=#{val}"
      }
      endpoint_options[key]
    end

    def args_missing
      arg_missing = [member_code, email].compact.empty?
    end

    def required_args
      raise "missing required arguments. #{self.class} must be instantiated with a :member_code or :email" if args_missing
    end

    def not_found(resp)
      { error: resp.parsed_response || 'Member not found' }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
centaman-2.0.0 lib/centaman/service/member.rb
centaman-1.0.0 lib/centaman/service/member.rb
centaman-0.2.3 lib/centaman/service/member.rb
centaman-0.2.2 lib/centaman/service/member.rb