Class: RssNotifier::Adapter::Pushbullet

Inherits:
Object
  • Object
show all
Defined in:
lib/rss_notifier/adapter/pushbullet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, access_token) ⇒ Pushbullet

Returns a new instance of Pushbullet.



11
12
13
14
# File 'lib/rss_notifier/adapter/pushbullet.rb', line 11

def initialize(name, access_token)
  @name = name
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



9
10
11
# File 'lib/rss_notifier/adapter/pushbullet.rb', line 9

def access_token
  @access_token
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/rss_notifier/adapter/pushbullet.rb', line 8

def name
  @name
end

Instance Method Details

#notify(item) ⇒ Object



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
# File 'lib/rss_notifier/adapter/pushbullet.rb', line 16

def notify(item)
  body = JSON.dump({
    'type' => 'link',
    'title' => "#{item.feed.name}",
    'body' => item.title,
    'url' => item.link
  })

  RssNotifier.logger.debug("Adapter::Pushbullet #{name}, #{body}")

  response = HTTP.timeout(:per_operation, write: 5, connect: 7, read: 5)
    .headers(
      'Access-Token' => access_token,
      'Content-Type' => 'application/json'
    )
    .post('https://api.pushbullet.com/v2/pushes', body: body)

  if response.code != 200
    RssNotifier.logger.warn("Could not notify #{self}. Code=#{response.code}")
    false
  else
    RssNotifier.logger.debug("#{self} notified")
    true
  end
end

#to_sObject



42
43
44
# File 'lib/rss_notifier/adapter/pushbullet.rb', line 42

def to_s
  "<Adapter::Pushbullet #{name}>"
end