Class: Verb::Message
- Inherits:
-
Object
- Object
- Verb::Message
- Defined in:
- lib/verb.rb
Constant Summary collapse
- API_URL =
ENV['VERB_API'] || 'https://verb.sh/api/v1/message'
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
Instance Method Summary collapse
-
#attach(files) ⇒ Object
Attach files to the message (if possible).
-
#initialize(params = {}, debug = false) ⇒ Message
constructor
A new instance of Message.
-
#send(args = {}) ⇒ Object
Send the message to the Verb API.
-
#tag(tags) ⇒ Object
Add tags to the internal tag array.
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
#context ⇒ Object (readonly)
Returns the value of attribute context.
30 31 32 |
# File 'lib/verb.rb', line 30 def context @context end |
#files ⇒ Object (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() if .is_a? Array .each do |t| @context[:tags] << t end else @context[:tags] << end end |