Class: Verb::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/verb.rb

Direct Known Subclasses

Email, SMS

Constant Summary collapse

API_URL =
ENV['VERB_API'] || 'https://verb.sh/api/v1/message'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, debug = false) ⇒ Message

Returns a new instance of Message.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/verb.rb', line 33

def initialize(params = {}, debug = false)
  @context = {}
  @files = []
  @context[:tags] = []
  @debug = debug
  @api_key = nil

  params.each do |k, v|
    if k != :api_key
      @context[k] = v
    else 
      @context[:pid] = v.split('-').last # We add the project ID to the ctx

      @api_key = v.split('-').first
    end
  end
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



30
31
32
# File 'lib/verb.rb', line 30

def context
  @context
end

#filesObject (readonly)

Returns the value of attribute files.



31
32
33
# File 'lib/verb.rb', line 31

def files
  @files
end

Instance Method Details

#attach(files) ⇒ Object

Attach files to the message (if possible)



52
53
54
55
56
57
58
59
60
# File 'lib/verb.rb', line 52

def attach(files)
  if @context[:type] == :sms
    raise 'This message type cannot have file attachments'
  end

  parsed_files(files).each do |f|
    @files << f
  end
end

#send(args = {}) ⇒ Object

Send the message to the Verb API



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/verb.rb', line 74

def send(args = {})
  payload = {
    files: []
  }

  payload.merge!(args)
  payload.merge!(@context)

  # Attach Files

  @files.each_with_index do |f, idx|
    payload[:files] << File.open(f)
  end

  log(payload)

  begin
    RestClient.post API_URL, payload, {
      'x-api-key': @api_key
    }
  rescue RestClient::ExceptionWithResponse => e
    e.response
  end
end

#tag(tags) ⇒ Object

Add tags to the internal tag array



63
64
65
66
67
68
69
70
71
# File 'lib/verb.rb', line 63

def tag(tags)
  if tags.is_a? Array
    tags.each do |t|
      @context[:tags] << t
    end
  else
    @context[:tags] << tags
  end
end