Module: Explore::ProfileDocument
- Defined in:
- lib/explore/profile_document.rb
Defined Under Namespace
Classes: ValidationError
Class Method Summary collapse
- .dump_yaml(document) ⇒ Object
- .error(path:, message:) ⇒ Object
- .error_guidance(errors) ⇒ Object
- .fingerprint(document) ⇒ Object
- .from_account(account) ⇒ Object
- .load_yaml_file(path) ⇒ Object
- .validate(document) ⇒ Object
- .validate!(document) ⇒ Object
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" => } 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 from_account(account) { "version" => 1, "profile" => { "name" => account.public_name.to_s, "headline" => account.public_tagline.to_s, "summary" => account.public_bio.to_s, "location" => account.public_location.to_s }, "links" => { "email" => account.public_email.to_s, "github_url" => account.public_github_url.to_s, "linkedin_url" => account.public_linkedin_url.to_s, "blog_url" => account.public_blog_url.presence || account.blog_url.to_s, "twitter_url" => account.public_twitter_url.to_s }, "projects" => account.account_case_studies.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" => account.account_experiences.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" => account.account_posts.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..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
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 |