Module: Explore::ProfileDocument

Defined in:
lib/explore/profile_document.rb

Defined Under Namespace

Classes: ValidationError

Class Method Summary collapse

Class Method Details

.dump_yaml(document) ⇒ Object



66
67
68
# File 'lib/explore/profile_document.rb', line 66

def dump_yaml(document)
  YAML.dump(document)
end

.error(path:, message:) ⇒ Object



120
121
122
# File 'lib/explore/profile_document.rb', line 120

def error(path:, message:)
  { "path" => path, "message" => message }
end

.error_guidance(errors) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/explore/profile_document.rb', line 124

def error_guidance(errors)
  issues = Array(errors)
  first = issues.first || {}
  path = first["path"].to_s

  {
    "errors" => issues,
    "error_count" => issues.size,
    "field_path" => path.empty? ? nil : path,
    "section" => section_for_path(path),
    "message" => first["message"]
  }.compact
end

.fingerprint(document) ⇒ Object



138
139
140
141
# File 'lib/explore/profile_document.rb', line 138

def fingerprint(document)
  normalized = normalize_hash(document)
  "sha256:#{Digest::SHA256.hexdigest(JSON.generate(canonicalize(normalized)))}"
end

.from_account(account) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/explore/profile_document.rb', line 19

def ()
  {
    "version" => 1,
    "profile" => {
      "name" => .public_name.to_s,
      "headline" => .public_tagline.to_s,
      "summary" => .public_bio.to_s,
      "location" => .public_location.to_s
    },
    "links" => {
      "email" => .public_email.to_s,
      "github_url" => .public_github_url.to_s,
      "linkedin_url" => .public_linkedin_url.to_s,
      "blog_url" => .public_blog_url.presence || .blog_url.to_s,
      "twitter_url" => .public_twitter_url.to_s
    },
    "projects" => ..order(:position, :id).map do |project|
      {
        "title" => project.title.to_s,
        "slug" => project.slug.to_s,
        "summary" => project.summary.to_s,
        "outcome" => project.outcome.to_s
      }
    end,
    "experience" => ..order(:position, :id).map do |experience|
      {
        "company" => experience.company.to_s,
        "role" => experience.role.to_s,
        "location" => experience.location.to_s,
        "start_date" => experience.start_date.to_s,
        "end_date" => experience.end_date.to_s,
        "current" => experience.current,
        "summary" => experience.summary.to_s
      }
    end,
    "writing" => ..order(date: :desc, id: :desc).map do |post|
      {
        "title" => post.title.to_s,
        "slug" => post.slug.to_s,
        "date" => post.date.to_s,
        "summary" => post.summary.to_s,
        "url" => post.url.to_s
      }
    end
  }
end

.load_yaml_file(path) ⇒ Object



70
71
72
73
74
75
# File 'lib/explore/profile_document.rb', line 70

def load_yaml_file(path)
  parsed = YAML.safe_load(File.read(path), permitted_classes: [ Date, Time ], aliases: false)
  parsed.is_a?(Hash) ? parsed : {}
rescue Psych::SyntaxError => e
  raise ValidationError.new([ error(path: "$", message: "Malformed YAML: #{e.message.lines.first.to_s.strip}") ])
end

.validate(document) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/explore/profile_document.rb', line 77

def validate(document)
  normalized = normalize_hash(document)
  errors = []

  unless normalized["version"].to_i == 1
    errors << error(path: "$.version", message: "must be 1")
  end

  profile = ensure_hash(normalized["profile"])
  links = ensure_hash(normalized["links"])
  projects = ensure_array(normalized["projects"])
  experience = ensure_array(normalized["experience"])
  writing = ensure_array(normalized["writing"])

  errors.concat(validate_profile(profile))
  errors.concat(validate_links(links))
  errors.concat(validate_projects(projects))
  errors.concat(validate_experience(experience))
  errors.concat(validate_writing(writing))

  canonical_document = normalized.merge(
    "profile" => profile,
    "links" => links,
    "projects" => projects,
    "experience" => experience,
    "writing" => writing
  )

  {
    ok: errors.empty?,
    document: canonical_document,
    document_fingerprint: errors.empty? ? fingerprint(canonical_document) : nil,
    errors:
  }
end

.validate!(document) ⇒ Object

Raises:



113
114
115
116
117
118
# File 'lib/explore/profile_document.rb', line 113

def validate!(document)
  result = validate(document)
  raise ValidationError.new(result[:errors]) unless result[:ok]

  result[:document]
end