summaryrefslogtreecommitdiff
path: root/src/main/groovy/org/xapek/yvesf/classifieds/Dumper.groovy
blob: ccadd56203dbc5d97b5b9d0a68172aa88b795f21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package org.xapek.yvesf.classifieds

import groovy.xml.MarkupBuilder
import org.xapek.yvesf.classifieds.Model.Classified
import org.xapek.yvesf.classifieds.Model.ClassifiedsList


class Dumper {
  static dump(ClassifiedsList classifiedList, PrintWriter writer) {
    new MarkupBuilder(writer).rss(version: '2.0', 'xmlns:atom': 'http://www.w3.org/2005/Atom') {
      channel {
        title('Glocals Classifieds Feed')
        description('glocal classifieds feed')
        link('https://www.xapek.org/git/yvesf/classifieds')
        classifiedList.each { Classified classified ->
          item {
            title("${classified.type} - ${classified.title} - ${classified.composedLocation}")
            guid(classified.link)
            link(classified.link)
            pubDate(classified.formattedDate)
            author("${classified.mem_id}@noemail.local (${classified.mem_name})")
            description(getDescription(classified) + classified.description)
          }
        }
      }
    }
  }

  static String getDescription(Classified classified) {
    final descriptionOs = new ByteArrayOutputStream()
    final description = new MarkupBuilder(new PrintWriter(descriptionOs))
    description.table {
      tr {
        th('Price')
        td("${classified.price} ${classified.currency}")
      }
      tr {
        th('Views')
        td(classified.views)
      }
      tr {
        th('Available')
        td(classified.available)
      }
      tr {
        th('Rooms')
        td(classified.rooms)
      }
      tr {
        th('Member')
        td {
          a(classified.mem_name, referrerpolicy: 'no-referrer', href: classified.memberProfileLink)
        }
      }
      if (classified.images) {
        tr {
          th('Images')
          td {
            classified.images.eachWithIndex { String url, Integer i ->
              a("Photo ${i + 1}", referrerpolicy: 'no-referrer', href: url)
            }
          }
        }
      }
      br()
    }
    return descriptionOs.toString()
  }
}