Class: Message

Inherits:
Object
  • Object
show all
Defined in:
lib/writeit-rails/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#answersObject

Returns the value of attribute answers.



13
14
15
# File 'lib/writeit-rails/message.rb', line 13

def answers
  @answers
end

#author_emailObject

Returns the value of attribute author_email.



12
13
14
# File 'lib/writeit-rails/message.rb', line 12

def author_email
  @author_email
end

#author_nameObject

Returns the value of attribute author_name.



11
12
13
# File 'lib/writeit-rails/message.rb', line 11

def author_name
  @author_name
end

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/writeit-rails/message.rb', line 6

def content
  @content
end

#recipientsObject

Returns the value of attribute recipients.



8
9
10
# File 'lib/writeit-rails/message.rb', line 8

def recipients
  @recipients
end

#remote_idObject

Returns the value of attribute remote_id.



10
11
12
# File 'lib/writeit-rails/message.rb', line 10

def remote_id
  @remote_id
end

#remote_uriObject

Returns the value of attribute remote_uri.



9
10
11
# File 'lib/writeit-rails/message.rb', line 9

def remote_uri
  @remote_uri
end

#subjectObject

Returns the value of attribute subject.



5
6
7
# File 'lib/writeit-rails/message.rb', line 5

def subject
  @subject
end

#writeitinstanceObject

Returns the value of attribute writeitinstance.



7
8
9
# File 'lib/writeit-rails/message.rb', line 7

def writeitinstance
  @writeitinstance
end

Instance Method Details

#push_to_apiObject



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
# File 'lib/writeit-rails/message.rb', line 31

def push_to_api
    validate
    
    data = {
        :author_name => self.author_name,
        :author_email => self.author_email,
        :subject => self.subject,
        :content => self.content,
        :writeitinstance => self.writeitinstance.url,
        :persons => self.recipients
    }
    authorization = 'ApiKey %{username}:%{api_key}' % {
        :username => self.writeitinstance.username,
        :api_key => self.writeitinstance.api_key,
    }
    url = self.writeitinstance.base_url + '/api/v1/message/'

    request = RestClient::Request.new(
        :method => :post,
        :url => url,
        :headers => {
            :accept => :json,
            :authorization => authorization,
            },
        :payload => data.to_json
        )
    request.processed_headers["Content-Type"] = 'application/json'
    response = request.execute
    data = JSON.parse(response.body)
    self.remote_id = data['id']
    self.remote_uri = data['resource_uri']


end

#validateObject

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/writeit-rails/message.rb', line 15

def validate
    
    wrong_recipients = self.recipients.nil?
    if not wrong_recipients
        wrong_recipients = not(self.recipients.any?)
    end
    raise ArgumentError, 'No recipients, please add some first' unless not wrong_recipients

    validates_author_name = self.author_name.nil?
    raise ArgumentError, 'No author, set it first' unless not validates_author_name

    validates_instance = self.writeitinstance.nil?
    raise ArgumentError, 'No instance, please set one first' unless not validates_instance

end