Module: AkidoPubsub

Defined in:
lib/akido_pubsub.rb,
lib/akido_pubsub/version.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
"3.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



5
6
7
# File 'lib/akido_pubsub.rb', line 5

def config
  @config
end

Class Method Details

.configure {|self.config| ... } ⇒ Object

Yields:



7
8
9
10
# File 'lib/akido_pubsub.rb', line 7

def configure
  self.config ||= Configuration.new
  yield(self.config)
end

Instance Method Details

#create_subscription(channel, endpoint) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/akido_pubsub.rb', line 62

def create_subscription  channel, endpoint
  AkidoPubsub.config.verify_config(:self_url)

  sub_endpoint = "#{AkidoPubsub.config.self_url}/#{endpoint}"
  existing_subscription = get_subscriptions(channel, sub_endpoint)

  return existing_subscription if existing_subscription

  url = "#{AkidoPubsub.config.server_url}/pubsub/subscription"
  body = {
    :channel => channel,
    :endpoints => [
      sub_endpoint
    ],
    :notify => true
  }.to_json

  response = RestClient.post url, body, { :accept => :json }

  extract_id_from_url(response.body, 'subscription') if response.code == 200
end

#extract_id_from_url(url, resource) ⇒ Object



84
85
86
# File 'lib/akido_pubsub.rb', line 84

def extract_id_from_url  url, resource
  return (match = url.match(/resource\/(\d+)/)) && match.last
end

#get_channel(tags) ⇒ Object



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

def get_channel tags
  # fetch list of pubsub channels and find channel for audit events
  path= "pubsub/channel?#{tags.map{ |tag| "tags=#{tag}" }.join('&')}"

  channels = get_from_server(path: path, error_text: 'Error getting channel')

  matching_channel = channels.find do |channel|
    (tags - channel["tags"]).empty?
  end

  matching_channel && matching_channel['uid']
end

#get_from_server(path:, error_text: "There was an error") ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/akido_pubsub.rb', line 88

def get_from_server path:, error_text: "There was an error"
  AkidoPubsub.config.verify_config(:server_url)

  url = "#{AkidoPubsub.config.server_url}/#{path}"

  begin
    response = RestClient.get url, { :accept => :json }
  rescue RestClient::ExceptionWithResponse => e
    AkidoPubsub.config.logger.error("#{error_text}: #{e.response}")
    return
  end

  return JSON.parse(response.body)
end

#get_subscriptions(channel, endpoint) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/akido_pubsub.rb', line 47

def get_subscriptions channel, endpoint
  subscriptions = get_from_server(path: 'pubsub/subscription', error_text: 'Error getting subscriptions')

  matching_subscription = subscriptions.find do |subscription|
    subscription['channel'] == channel && subscription['endpoints'].include?(endpoint)
  end

  if matching_subscription
    AkidoPubsub.config.logger.info("Found existing subscription to channel #{channel}")
    return matching_subscription["uid"] 
  else
    return nil
  end
end