Class: Notifications::Client::Speaker
- Inherits:
-
Object
- Object
- Notifications::Client::Speaker
- Includes:
- ErrorHandling
- Defined in:
- lib/notifications/client/speaker.rb
Constant Summary collapse
- BASE_PATH =
"/v2/notifications".freeze
- USER_AGENT =
"NOTIFY-API-RUBY-CLIENT/#{Notifications::Client::VERSION}".freeze
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#secret_token ⇒ Object
readonly
Returns the value of attribute secret_token.
-
#service_id ⇒ Object
readonly
Returns the value of attribute service_id.
Instance Method Summary collapse
- #get(id = nil, options = {}) ⇒ Object
- #get_pdf_for_letter(id) ⇒ Object
- #get_with_url(url, options = {}) ⇒ Object
-
#initialize(secret_token = nil, base_url = nil) ⇒ Speaker
constructor
A new instance of Speaker.
-
#post(kind, form_data) ⇒ Object
@option form_data [String] :phone_number phone number of the sms recipient.
- #post_precompiled_letter(reference, pdf_file, postage = nil) ⇒ Object
- #post_with_url(url, form_data) ⇒ Object
Methods included from ErrorHandling
#build_error, #error_class_for_code
Constructor Details
#initialize(secret_token = nil, base_url = nil) ⇒ Speaker
Returns a new instance of Speaker.
23 24 25 26 27 28 29 |
# File 'lib/notifications/client/speaker.rb', line 23 def initialize(secret_token = nil, base_url = nil) @service_id = secret_token[secret_token.length - 73..secret_token.length - 38] @secret_token = secret_token[secret_token.length - 36..secret_token.length] @base_url = base_url || PRODUCTION_BASE_URL validate_uuids! end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
12 13 14 |
# File 'lib/notifications/client/speaker.rb', line 12 def base_url @base_url end |
#secret_token ⇒ Object (readonly)
Returns the value of attribute secret_token.
14 15 16 |
# File 'lib/notifications/client/speaker.rb', line 14 def secret_token @secret_token end |
#service_id ⇒ Object (readonly)
Returns the value of attribute service_id.
13 14 15 |
# File 'lib/notifications/client/speaker.rb', line 13 def service_id @service_id end |
Instance Method Details
#get(id = nil, options = {}) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/notifications/client/speaker.rb', line 64 def get(id = nil, = {}) path = BASE_PATH.dup path << "/" << id if id path << "?" << URI.encode_www_form() if .any? request = Net::HTTP::Get.new(path, headers) perform_request!(request) end |
#get_pdf_for_letter(id) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/notifications/client/speaker.rb', line 121 def get_pdf_for_letter(id) path = "/v2/notifications/" << id << "/pdf" request = Net::HTTP::Get.new(path, headers) # can't use `perform_request!` because we're just returning raw binary data response = open(request) if response.is_a?(Net::HTTPClientError) || response.is_a?(Net::HTTPServerError) raise build_error(response) else response.body end end |
#get_with_url(url, options = {}) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/notifications/client/speaker.rb', line 77 def get_with_url(url, = {}) path = url path << "?" << URI.encode_www_form() if .any? request = Net::HTTP::Get.new(path, headers) perform_request!(request) end |
#post(kind, form_data) ⇒ Object
@option form_data [String] :phone_number
phone number of the sms recipient
51 52 53 54 55 56 57 58 |
# File 'lib/notifications/client/speaker.rb', line 51 def post(kind, form_data) request = Net::HTTP::Post.new( "#{BASE_PATH}/#{kind}", headers ) request.body = form_data.is_a?(Hash) ? form_data.to_json : form_data perform_request!(request) end |
#post_precompiled_letter(reference, pdf_file, postage = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/notifications/client/speaker.rb', line 105 def post_precompiled_letter(reference, pdf_file, postage = nil) content = Base64.strict_encode64(pdf_file.read) form_data = { reference: reference, content: content } if postage != nil form_data[:postage] = postage end request = Net::HTTP::Post.new( "#{BASE_PATH}/letter", headers ) request.body = form_data.to_json perform_request!(request) end |
#post_with_url(url, form_data) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/notifications/client/speaker.rb', line 92 def post_with_url(url, form_data) request = Net::HTTP::Post.new( url, headers ) request.body = form_data.is_a?(Hash) ? form_data.to_json : form_data perform_request!(request) end |