Class: Toot::SubscriptionsService

Inherits:
Object
  • Object
show all
Defined in:
lib/toot/subscriptions_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, response) ⇒ SubscriptionsService

Returns a new instance of SubscriptionsService.



20
21
22
23
# File 'lib/toot/subscriptions_service.rb', line 20

def initialize(request, response)
  @request = request
  @response = response
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



19
20
21
# File 'lib/toot/subscriptions_service.rb', line 19

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



19
20
21
# File 'lib/toot/subscriptions_service.rb', line 19

def response
  @response
end

Class Method Details

.call(env) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/toot/subscriptions_service.rb', line 4

def self.call(env)
  request = Rack::Request.new(env)
  response = Rack::Response.new
  me = new(request, response)

  if me.respond_to?(request.request_method.downcase)
    me.public_send(request.request_method.downcase)
  else
    # Method not allowed
    response.status = 405
  end

  response.finish
end

Instance Method Details

#deleteObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/toot/subscriptions_service.rb', line 54

def delete
  channel = request.params['channel']
  callback_url = request.params['callback_url']
  if channel.blank? || callback_url.blank?
    response.status = 400
    return
  end

  result = Toot.redis do |r|
    r.srem channel, callback_url
  end

  response.status = result ? 204 : 404
end

#getObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/toot/subscriptions_service.rb', line 25

def get
  accept = request.env['HTTP_ACCEPT']
  
  if accept.include?('application/json')
    response.header['Content-Type'] = 'application/json'
    response.write({
      channels: channels,
      subscriptions: subscriptions,
    }.to_json)
    return
  end

  response.status = 406
end

#postObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/toot/subscriptions_service.rb', line 40

def post
  json = parse_body_json(request)

  if !json["channel"] || !json["callback_url"]
    response.status = 422
    return
  end

  added = Toot.redis do |r|
    r.sadd json["channel"], json["callback_url"]
  end
  response.status = added ? 201 : 204
end