Method: BeerDb::Model::BeerSerializer#as_json

Defined in:
lib/beerdb/serializers/beer.rb

#as_jsonObject



13
14
15
16
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
# File 'lib/beerdb/serializers/beer.rb', line 13

def as_json
  ## note: as_json returns record as a hash
  ##   note: NOT yet converted with to_json or JSON.pretty_generate etc.

  brewery = {}
  if beer.brewery.present?
    brewery = { key: beer.brewery.key,
                title: beer.brewery.title }
  end

  tags = []
  if beer.tags.present?
    beer.tags.each { |tag| tags << tag.key }
  end

  country = {
    key:   beer.country.key,
    title: beer.country.title
  }

  data = { key:      beer.key,
           title:    beer.title,
           synonyms: beer.synonyms,
           abv:      beer.abv,
           srm:      beer.srm,
           og:       beer.og,
           tags:     tags,
           brewery: brewery,
           country: country }

  data
end