Class: Notifications::Client::Speaker

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(secret_token = nil, base_url = nil) ⇒ Speaker

Returns a new instance of Speaker.

Parameters:

  • secret (String)

    your service API secret

  • base_url (String) (defaults to: nil)

    host URL. This is the address to perform the requests. If left nil the production url is used.



20
21
22
23
24
# File 'lib/notifications/client/speaker.rb', line 20

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
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



9
10
11
# File 'lib/notifications/client/speaker.rb', line 9

def base_url
  @base_url
end

#secret_tokenObject (readonly)

Returns the value of attribute secret_token.



11
12
13
# File 'lib/notifications/client/speaker.rb', line 11

def secret_token
  @secret_token
end

#service_idObject (readonly)

Returns the value of attribute service_id.



10
11
12
# File 'lib/notifications/client/speaker.rb', line 10

def service_id
  @service_id
end

Instance Method Details

#get(id = nil, options = {}) ⇒ Object

Parameters:

  • id (String) (defaults to: nil)
  • options (Hash) (defaults to: {})

    query

See Also:

  • #perform_request!


59
60
61
62
63
64
65
# File 'lib/notifications/client/speaker.rb', line 59

def get(id = nil, options = {})
  path = BASE_PATH.dup
  path << "/" << id if id
  path << "?" << URI.encode_www_form(options) if options.any?
  request = Net::HTTP::Get.new(path, headers)
  perform_request!(request)
end

#get_with_url(url, options = {}) ⇒ Object

Parameters:

  • url

    path of endpoint

  • id (String)
  • options (Hash) (defaults to: {})

    query

See Also:

  • #perform_request!


72
73
74
75
76
77
# File 'lib/notifications/client/speaker.rb', line 72

def get_with_url(url, options = {})
  path = url
  path << "?" << URI.encode_www_form(options) if options.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

Parameters:

  • kind (String)

    ‘email’, ‘sms’ or ‘letter’

  • form_data (Hash)

Options Hash (form_data):

  • :email_address (String)

    email address of the email recipent

  • :template (String)

    template to render in notification

  • :personalisation (Hash)

    fields to use in the template

  • :reference (String)

    A reference specified by the service for the notification. Get all notifications can be filtered by this reference. This reference can be unique or used used to refer to a batch of notifications. Can be an empty string or nil, when you do not require a reference for the notifications.

  • :email_reply_to_id (String)

    id of the email address that replies to email notifications will be sent to

  • :sms_sender_id (String)

    id of the sender to be used for an sms notification

See Also:

  • #perform_request!


46
47
48
49
50
51
52
53
# File 'lib/notifications/client/speaker.rb', line 46

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_with_url(url, form_data) ⇒ Object

Parameters:

  • url (String)

    path of the endpoint

  • form_data (Hash)

Options Hash (form_data):

  • :template_id (String)

    id of the template to render

  • :personalisation (Hash)

    fields to use in the template

See Also:

  • #perform_request!


87
88
89
90
91
92
93
94
# File 'lib/notifications/client/speaker.rb', line 87

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