Sha256: 434157da6c72d159f28d26976e875a3123440a278cd761ab7052517afcefbad1

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

module Outreach
  class Prospect
    attr_accessor :first_name, :last_name, :company, :contact, :tags, :id

    def initialize(attrs)
      @id = attrs['id']
      @first_name = attrs['first_name']
      @last_name = attrs['last_name']
      @company = attrs['company']
      @contact = attrs['contact']
      @tags = attrs['tags']
    end

    def self.build_from_attributes_hash(attrs)
      result = {}
      result['first_name'] = nested_hash_value(attrs, ['attributes', 'personal',
                                                      'name', 'first'])
      result['last_name'] = nested_hash_value(attrs, ['attributes', 'personal',
                                                     'name', 'last'])
      result['company'] = to_ostruct(attrs['attributes'].fetch('company', {}))
      result['contact'] = to_ostruct(attrs['attributes'].fetch('contact', {}))
      result['tags'] = nested_hash_value(attrs, ['attributes', 'metadata',
                                                'tags'])
      result['id'] = attrs['id']
      new(result)
    end

    private

    def self.to_ostruct(hash)
      o = OpenStruct.new(hash)
      hash.each.with_object(o) do |(k,v), o|
        o.send(:"#{k}=", to_ostruct(v)) if v.is_a? Hash
      end
      o
    end

    def self.nested_hash_value(attrs, keys)
      keys.reduce(attrs) {|m,k| m && m[k] }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
outreach-0.2.1 lib/outreach/prospect.rb
outreach-0.2.0 lib/outreach/prospect.rb
outreach-0.1.3 lib/outreach/prospect.rb
outreach-0.1.2 lib/outreach/prospect.rb
outreach-0.1.1 lib/outreach/prospect.rb