Class: ADN::Recipes::BroadcastMessageBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/adn/recipes/broadcast_message_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) {|_self| ... } ⇒ BroadcastMessageBuilder

Returns a new instance of BroadcastMessageBuilder.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
15
16
17
# File 'lib/adn/recipes/broadcast_message_builder.rb', line 9

def initialize(params = {})
  if params.respond_to? :each_pair
    params.each_pair do |k, v|
      send("#{k}=", v) if respond_to?("#{k}=")
    end
  end

  yield self if block_given?
end

Instance Attribute Details

#attachmentObject

Returns the value of attribute attachment.



6
7
8
# File 'lib/adn/recipes/broadcast_message_builder.rb', line 6

def attachment
  @attachment
end

#channel_idObject

Returns the value of attribute channel_id.



6
7
8
# File 'lib/adn/recipes/broadcast_message_builder.rb', line 6

def channel_id
  @channel_id
end

#headlineObject

Returns the value of attribute headline.



6
7
8
# File 'lib/adn/recipes/broadcast_message_builder.rb', line 6

def headline
  @headline
end

Returns the value of attribute parse_links.



6
7
8
# File 'lib/adn/recipes/broadcast_message_builder.rb', line 6

def parse_links
  @parse_links
end

Returns the value of attribute parse_markdown_links.



6
7
8
# File 'lib/adn/recipes/broadcast_message_builder.rb', line 6

def parse_markdown_links
  @parse_markdown_links
end

#photoObject

Returns the value of attribute photo.



6
7
8
# File 'lib/adn/recipes/broadcast_message_builder.rb', line 6

def photo
  @photo
end

Returns the value of attribute read_more_link.



6
7
8
# File 'lib/adn/recipes/broadcast_message_builder.rb', line 6

def read_more_link
  @read_more_link
end

#textObject

Returns the value of attribute text.



6
7
8
# File 'lib/adn/recipes/broadcast_message_builder.rb', line 6

def text
  @text
end

Instance Method Details

#annotationsObject



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/adn/recipes/broadcast_message_builder.rb', line 19

def annotations
  annotations = [
    {
      type: 'net.app.core.broadcast.message.metadata',
      value: {
        subject: self.headline
      }
    }
  ]

  if self.read_more_link
    annotations << {
      type: 'net.app.core.crosspost',
      value: {
        canonical_url: self.read_more_link
      }
    }
  end

  if self.photo
    file = ADN::File.upload_file(self.photo, {
      type: 'net.app.adnrb.upload'
    })


    annotations << {
      type: 'net.app.core.oembed',
      value: {
        "+net.app.core.file" => {
          file_id: file.id,
          file_token: file.file_token,
          format: 'oembed',
        }
      }
    }
  end

  if self.attachment
    file = ADN::File.upload_file(self.attachment, {
      type: 'net.app.adnrb.upload'
    })

    annotations << {
      type: 'net.app.core.attachments',
      value: {
        "+net.app.core.file_list" => [
          {
            file_id: file.id,
            file_token: file.file_token,
            format: 'metadata',
          }
        ]
      }
    }
  end

  annotations
end

#messageObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/adn/recipes/broadcast_message_builder.rb', line 78

def message
  message = {
    annotations: self.annotations,
    entities: {
      parse_links: !!(self.parse_links or self.parse_markdown_links),
      parse_markdown_links: !!self.parse_markdown_links
    }
  }

  if self.text
    message[:text] = self.text
  else
    message[:machine_only] = true
  end

  message
end

#sendObject



96
97
98
99
100
# File 'lib/adn/recipes/broadcast_message_builder.rb', line 96

def send
  api_message = ADN::API::Message.create(self.channel_id, self.message)

  Message.new api_message["data"]
end