lib/outreach/prospect.rb in outreach-0.1.0 vs lib/outreach/prospect.rb in outreach-0.1.1

- old
+ new

@@ -1,25 +1,43 @@ module Outreach class Prospect attr_accessor :first_name, :last_name, :company, :contact, :tags, :id def initialize(attrs) - @first_name = attrs['attributes']['personal']['name']['first'] - @last_name = attrs['attributes']['personal']['name']['last'] - @company = to_ostruct(attrs['attributes']['company']) - @contact = to_ostruct(attrs['attributes']['contact']) - @tags = attrs['attributes']['metadata']['tags'] @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 to_ostruct(hash) + 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