Class: Notifications::Client::Speaker

Inherits:
Object
  • Object
show all
Defined in:
lib/notifications/client/speaker.rb

Constant Summary collapse

BASE_PATH =
"/notifications".freeze
USER_AGENT =
"NOTIFY-API-RUBY-CLIENT/#{Notifications::Client::VERSION}".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Speaker.

Parameters:

  • service_id (String)

    your service API identifier

  • secret (String)

    your service API secret

  • base_url (String) (defaults to: nil)

    host URL. This is the address to perform the requests



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

def initialize(service_id, secret_token, base_url = nil)
  @service_id = service_id
  @secret_token = secret_token
  @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

Instance Method Details

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

Parameters:

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

    query

See Also:

  • #perform_request!


50
51
52
53
54
55
56
# File 'lib/notifications/client/speaker.rb', line 50

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

#post(kind, form_data) ⇒ Object

 @option form_data [String] :to recipient

to notify (sms or email)

Parameters:

  • kind (String)

    ‘email’ or ‘sms’

  • form_data (Hash)

Options Hash (form_data):

  • :template (String)

    template to render in notification

  • :personalisation (Hash)

    fields to use in the template

See Also:

  • #perform_request!


37
38
39
40
41
42
43
44
# File 'lib/notifications/client/speaker.rb', line 37

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