Skip to content

Instantly share code, notes, and snippets.

@Verkalets
Created November 5, 2014 15:19
Show Gist options
  • Save Verkalets/5304bf1369ec29bb2294 to your computer and use it in GitHub Desktop.
Save Verkalets/5304bf1369ec29bb2294 to your computer and use it in GitHub Desktop.
class ModelsPersistors::Crunchbase::Person
@@neo = Neography::Rest.new
def initialize(json_data)
data = json_data["data"]
@uuid = data["uuid"]
save(data)
end
private
def save(data)
relationships = data["relationships"]
person = data["properties"]
save_person person
save_experience relationships["experience"]["items"] if relationships["experience"]
save_investments relationships["investments"]["items"] if relationships["investments"]
if relationships["founded_companies"]
save_founded_companies relationships["founded_companies"]["items"]
end
if relationships["primary_image"] and relationships["primary_image"]["items"].first
save_primary_image relationships["primary_image"]["items"].first
end
save_websites relationships["websites"]["items"] if relationships["websites"]
save_news relationships["news"]["items"] if relationships["news"]
end
def save_person(person)
@@neo.execute_query(
"
MERGE (person:Crunchbase:Person {uuid: {uuid}})
ON CREATE SET person += {person}
ON MATCH SET person += {person}
",
person: person,
uuid: @uuid
)
end
def save_experience(experience)
worked_at_set_clause = ModelsPersistors::Helpers.set_clause(node: "worked_at",
props: ["started_on", "ended_on"])
experience.each do |position|
organization_permalink = MegakiUtils::Crunchbase.path_to_permalink(
position["organization_path"])
@@neo.execute_query(
"
MERGE (person:Crunchbase:Person {uuid: {uuid}})
MERGE (company:Company:Crunchbase {permalink: {permalink}})
MERGE (person)-[worked_at:WORKED_AT {title: {title}}]->(company)
ON CREATE SET #{worked_at_set_clause}
ON MATCH SET #{worked_at_set_clause}
",
position.merge(permalink: organization_permalink, uuid: @uuid)
)
end
end
def save_investments(investments)
funder_set_clause = ModelsPersistors::Helpers.set_clause(node: "funder",
props: ["money_invested_currency_code", "money_invested_usd", "money_invested"])
persistable_investments = investments.lazy.reject do |investment|
investment["funding_round"]["path"].nil?
end
persistable_investments.each do |investment|
funding_round_permalink = MegakiUtils::Crunchbase.path_to_permalink(
investment["funding_round"]["path"])
@@neo.execute_query(
"
MATCH (person:Crunchbase:Person {uuid: {uuid}})
MERGE (funding_round:FundingRound {permalink: {funding_round_permalink}})
MERGE (funding_round)-[funder:FUNDER]->(person)
ON CREATE SET #{funder_set_clause}
ON MATCH SET #{funder_set_clause}
",
money_invested_currency_code: investment["money_invested_currency_code"],
money_invested_usd: investment["money_invested_usd"],
money_invested: investment["money_invested"],
funding_round_permalink: funding_round_permalink,
uuid: @uuid
)
end
end
def save_founded_companies(founded_companies)
founded_companies.each do |founded_company|
organization_permalink = MegakiUtils::Crunchbase.path_to_permalink(founded_company["path"])
@@neo.execute_query(
"
MERGE (person:Crunchbase:Person {uuid: {uuid}})
MERGE (company:Crunchbase:Company {permalink: {organization_permalink}})
MERGE (person)-[:FOUNDED]->(company)
",
organization_permalink: organization_permalink,
uuid: @uuid
)
end
end
def save_primary_image(primary_image)
@@neo.execute_query(
"
MATCH (person:Crunchbase:Person {uuid: {uuid}})
SET person.primary_image = {primary_image}
",
primary_image: "http://images.crunchbase.com/#{primary_image["path"]}",
uuid: @uuid
)
end
def save_websites(websites)
websites.each do |website|
@@neo.execute_query(
"
MATCH (person:Crunchbase:Person {uuid: {uuid}})
MERGE (website:Crunchbase:Website {url: {url}})
ON CREATE SET website = {website}
ON MATCH SET website = {website}
MERGE (person)-[:HAS_WEBSITE]->(website)
",
website: website,
url: website["url"],
uuid: @uuid
)
end
end
def save_news(news)
news.each do |article|
@@neo.execute_query(
"
MATCH (person:Crunchbase:Person {uuid: {uuid}})
MERGE (article:Crunchbase:News {url: {url}})
ON CREATE SET article = {article}
ON MATCH SET article = {article}
MERGE (person)-[:HAS_NEWS]->(article)
",
article: article,
url: article["url"],
uuid: @uuid
)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment