Class: FakeFlorence::Announcers::Routemaster

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_florence/announcers/routemaster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, name: nil) ⇒ Routemaster

Returns a new instance of Routemaster.



8
9
10
11
12
13
14
15
# File 'lib/fake_florence/announcers/routemaster.rb', line 8

def initialize(url, name: nil)
  @name = name
  @http = Faraday.new(url: url) do |f|
    f.use Faraday::Response::RaiseError
    f.request :json
    f.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/fake_florence/announcers/routemaster.rb', line 6

def name
  @name
end

Instance Method Details

#announce(hash) ⇒ Object



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
# File 'lib/fake_florence/announcers/routemaster.rb', line 17

def announce(hash)
  t = Time.now.to_i

  events = []
  hash.each_pair do |type, features|
    features.each do |feature|
      events.push(
        topic: 'feature',
        type: type,
        url: Config.url_for(feature),
        t: t
      )
    end
  end

  res = @http.post do |req|
    req.body = events.to_json
  end

  count = events.size
  counter = "#{count} feature#{count == 1 ? '' : 's'}"
  Config.log.info "#{counter} announced to '#{@name}'."
  true

rescue Faraday::ConnectionFailed => e
  Config.log.error e.message
  false

rescue Faraday::Error::ClientError => e
  Config.log.warn "Could not announce to #{@name}: HTTP #{e.response[:status]}"
  false
end