summaryrefslogtreecommitdiff
path: root/fake-service/lib/pixelated_service/server.rb
blob: d4b9e3544d127427271b2c2e4763d85d793db842 (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
require 'sinatra/base'
require 'sinatra/json'
require 'sinatra-index'
require 'json'
require 'net/http'

module PixelatedService
  class Server < Sinatra::Base
    set :root, File.join(File.dirname(__FILE__), '../../')
    set :public_folder, File.join(File.dirname(__FILE__), '../../../web-ui/app/')

    def json_body; JSON.parse request.body.read.to_s; end
    register Sinatra::Index
    use_static_index 'index.html'

    if ENV['RACK_ENV'] == 'staging'
      get    '/'            do File.read(File.join(settings.root, 'public', 'index.html')) end
    end

    get    '/mails'       do json mails(params["q"], (params["p"] || 0).to_i, (params["w"] || -1).to_i) end
    delete '/mails'       do json delete_mails(params["q"], (params["p"] || 0).to_i, (params["w"] || -1).to_i, params["idents"]) end
    post   '/mails/read'  do json readmails(params["idents"], true) end
    post   '/mails/unread'  do json readmails(params["idents"], false) end
    get    '/mail/:ident'    do |i| json mail(i)    end
    delete '/mail/:ident' do |i| json delete_mail(i) end
    post   '/mail/:ident/star'      do |i| json starmail(i, true)    end
    post   '/mail/:ident/unstar'    do |i| json starmail(i, false)    end
    post   '/mail/:ident/replied'      do |i| json repliedmail(i, true)    end
    post   '/mail/:ident/unreplied'    do |i| json repliedmail(i, false)    end
    post   '/mail/:ident/read'      do |i| json readmail(i, true)    end
    post   '/mail/:ident/unread'    do |i| json readmail(i, false)    end
    get    '/mail/:ident/tags'     do |i| json tags(i)    end
    post   '/mail/:ident/tags'    do |i| json settags(i, json_body)    end

    get    '/draft_reply_for/:ident' do |i| json draft_reply_for(i) end

    get    '/contacts'       do json contacts(params["q"], (params["p"] || 0).to_i, (params["w"] || -1).to_i) end
    get    '/contact/:ident'    do |i| json contact(i) end

    get    '/stats'       do json stats end

    get    '/personas'    do     json personas   end
    get    '/persona/:ident' do |i| json persona(i) end

    get    '/tags' do json all_tags(params["q"]) end

    post '/mails' do
      ident = send_mail json_body
      json({ ident: ident })
    end

    put '/mails' do
      ident = update_mail json_body
      json({ ident: ident })
    end

    post '/tags' do
      tag = create_tag json_body
      json({ tag: tag })
    end

    post '/control/create_mail'        do json control_create_mail  end
    post '/control/delete_mails'       do json control_delete_mails end
    post '/control/mailset/:name/load' do |name| json control_mailset_load(name) end

    include PixelatedService::Fake
  end
end