Class: SearchKit::Clients::Events

Inherits:
Object
  • Object
show all
Defined in:
lib/search_kit/clients/events.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEvents

Returns a new instance of Events.



9
10
11
12
13
# File 'lib/search_kit/clients/events.rb', line 9

def initialize
  uri = [ SearchKit.config.app_uri, "events" ].join("/")
  @connection = Faraday.new(uri)
  @token      = SearchKit.config.app_token
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/search_kit/clients/events.rb', line 7

def connection
  @connection
end

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/search_kit/clients/events.rb', line 7

def token
  @token
end

Instance Method Details

#complete(id) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/search_kit/clients/events.rb', line 15

def complete(id)
  response = connection.delete(id, token: token)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::Unauthorized  if response.status == 401
  fail Errors::EventNotFound if response.status == 404

  SearchKit::Models::Event.new body.fetch(:data, {})
end

#indexObject



25
26
27
28
29
30
31
32
# File 'lib/search_kit/clients/events.rb', line 25

def index
  response = connection.get(token: token)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::Unauthorized if response.status == 401

  SearchKit::Models::Events.new body.fetch(:data, [])
end

#pending(channel) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/search_kit/clients/events.rb', line 34

def pending(channel)
  params   = { "filter[channel]" => channel, token: token }
  response = connection.get('', params)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::BadRequest    if response.status == 400
  fail Errors::Unauthorized  if response.status == 401
  fail Errors::Unprocessable if response.status == 422

  SearchKit::Models::Events.new body.fetch(:data, [])
end

#publish(channel, payload) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/search_kit/clients/events.rb', line 46

def publish(channel, payload)
  params = {
    token: token,
    data: {
      type: 'events',
      attributes: { channel: channel, payload: payload }
    }
  }

  response = connection.post("", params)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::BadRequest    if response.status == 400
  fail Errors::Unauthorized  if response.status == 401
  fail Errors::Unprocessable if response.status == 422

  SearchKit::Models::Event.new body.fetch(:data, {})
end

#show(id) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/search_kit/clients/events.rb', line 65

def show(id)
  response = connection.get(id, token: token)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::Unauthorized  if response.status == 401
  fail Errors::EventNotFound if response.status == 404

  SearchKit::Models::Event.new body.fetch(:data, {})
end