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



64
65
66
# File 'lib/explore/profile_document.rb', line 64

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

.error(path:, message:) ⇒ Object



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

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

.from_account(account) ⇒ Object



17
18
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
# File 'lib/explore/profile_document.rb', line 17

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



68
69
70
71
72
73
# File 'lib/explore/profile_document.rb', line 68

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



75
76
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
# File 'lib/explore/profile_document.rb', line 75

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))

  {
    ok: errors.empty?,
    document: normalized.merge(
      "profile" => profile,
      "links" => links,
      "projects" => projects,
      "experience" => experience,
      "writing" => writing
    ),
    errors:
  }
end

.validate!(document) ⇒ Object

Raises:



108
109
110
111
112
113
# File 'lib/explore/profile_document.rb', line 108

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

  result[:document]
end