Skip to content

Commit

Permalink
Merge pull request #143 from piscespieces/add-location-header-to-temp…
Browse files Browse the repository at this point in the history
…lates

Add location header to templates
  • Loading branch information
ignacio-chiazzo authored Jun 8, 2024
2 parents a93a3cf + aa00277 commit 1e959e6
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 7 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,20 @@ WhatsApp message templates are specific message formats that businesses use to s
currency = WhatsappSdk::Resource::Currency.new(code: "USD", amount: 1000, fallback_value: "1000")
date_time = WhatsappSdk::Resource::DateTime.new(fallback_value: "2020-01-01T00:00:00Z")
image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL")
location = WhatsappSdk::Resource::Location.new(
latitude: 25.779510, longitude: -80.338631, name: "miami store", address: "820 nw 87th ave, miami, fl"
)

parameter_image = WhatsappSdk::Resource::ParameterObject.new(type: WhatsappSdk::Resource::ParameterObject::Type::Image, image: image)
# You can also use a plain string as type e.g.
# parameter_image = WhatsappSdk::Resource::ParameterObject.new(type: "image", image: image)
parameter_text = WhatsappSdk::Resource::ParameterObject.new(type: WhatsappSdk::Resource::ParameterObject::Type::Text, text: "TEXT_STRING")
parameter_currency = WhatsappSdk::Resource::ParameterObject.new(type: WhatsappSdk::Resource::ParameterObject::Type::Currency, currency: currency)
parameter_date_time = WhatsappSdk::Resource::ParameterObject.new(type: WhatsappSdk::Resource::ParameterObject::Type::DateTime, date_time: date_time)
parameter_location = WhatsappSdk::Resource::ParameterObject.new(
type: WhatsappSdk::Resource::ParameterObject::Type::Location,
location: location
)

header_component = WhatsappSdk::Resource::Component.new(
type: WhatsappSdk::Resource::Component::Type::Header,
Expand Down Expand Up @@ -406,6 +413,11 @@ button_component2 = WhatsappSdk::Resource::Component.new(
WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload, payload: "PAYLOAD")
]
)

location_component = WhatsappSdk::Resource::Component.new(
type: WhatsappSdk::Resource::Component::Type::Header,
parameters: [parameter_location]
)
@messages_api.send_template(sender_id: 12_345, recipient_number: 12345678, name: "hello_world", language: "en_US", components_json: [component_1])
```

Expand Down
12 changes: 10 additions & 2 deletions example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,16 @@ def print_data_or_error(response, identifier)
name: "hello_world", language: "en_US", components: [])
puts response_with_object

# Send a template with components.Remember to create the template first.
# Send a template with components (Remember to create the template first).
header_component = WhatsappSdk::Resource::Component.new(
type: WhatsappSdk::Resource::Component::Type::Header
)

image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL", caption: "caption")
document = WhatsappSdk::Resource::Media.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
video = WhatsappSdk::Resource::Media.new(type: "video", id: "123")
location = WhatsappSdk::Resource::Location.new(
latitude: 25.779510, longitude: -80.338631, name: "miami store", address: "820 nw 87th ave, miami, fl"
)

parameter_image = WhatsappSdk::Resource::ParameterObject.new(
type: "image",
Expand All @@ -300,10 +302,16 @@ def print_data_or_error(response, identifier)
text: "I am a text"
)

parameter_location = WhatsappSdk::Resource::ParameterObject.new(
type: "location",
location: location
)

header_component.add_parameter(parameter_text)
header_component.add_parameter(parameter_image)
header_component.add_parameter(parameter_video)
header_component.add_parameter(parameter_document)
header_component.add_parameter(parameter_location)
header_component.to_json

body_component = WhatsappSdk::Resource::Component.new(
Expand Down
49 changes: 49 additions & 0 deletions lib/whatsapp_sdk/resource/location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module WhatsappSdk
module Resource
class Location
extend T::Sig

# Returns the latitude of the location.
#
# @returns latitude [Float].
sig { returns(T.nilable(Float)) }
attr_accessor :latitude

# Returns the longitude of the location.
#
# @returns longitude [Float].
sig { returns(T.nilable(Float)) }
attr_accessor :longitude

# Returns the name of the location.
#
# @returns name [String].
sig { returns(T.nilable(String)) }
attr_accessor :name

# Returns the address of the location.
#
# @returns address [String].
sig { returns(T.nilable(String)) }
attr_accessor :address

sig { params(latitude: Float, longitude: Float, name: String, address: String).void }
def initialize(latitude:, longitude:, name:, address:)
@latitude = latitude
@longitude = longitude
@name = name
@address = address
end

sig { returns(T::Hash[T.untyped, T.untyped]) }
def to_json
{
latitude: latitude,
longitude: longitude,
name: name,
address: address
}
end
end
end
end
18 changes: 15 additions & 3 deletions lib/whatsapp_sdk/resource/parameter_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Type < T::Enum
Image = new("image")
Document = new("document")
Video = new("video")
Location = new("location")
end
end

Expand Down Expand Up @@ -79,20 +80,28 @@ class Type < T::Enum
sig { returns(T.nilable(Media)) }
attr_accessor :video

# Returns location if the parameter object type is location
#
# @returns location [Location]
sig { returns(T.nilable(Location)) }
attr_accessor :location

sig do
params(
type: T.any(Type, String), text: T.nilable(String), currency: T.nilable(Currency),
date_time: T.nilable(DateTime), image: T.nilable(Media), document: T.nilable(Media), video: T.nilable(Media)
date_time: T.nilable(DateTime), image: T.nilable(Media), document: T.nilable(Media), video: T.nilable(Media),
location: T.nilable(Location)
).void
end
def initialize(type:, text: nil, currency: nil, date_time: nil, image: nil, document: nil, video: nil)
def initialize(type:, text: nil, currency: nil, date_time: nil, image: nil, document: nil, video: nil, location: nil)
@type = T.let(deserialize_type(type), Type)
@text = text
@currency = currency
@date_time = date_time
@image = image
@document = document
@video = video
@location = location
validate
end

Expand All @@ -112,6 +121,8 @@ def to_json
T.must(document).to_json
when "video"
T.must(video).to_json
when "location"
T.must(location).to_json
else
raise "Invalid type: #{type}"
end
Expand Down Expand Up @@ -149,7 +160,8 @@ def validate_attributes
[Type::DateTime, date_time],
[Type::Image, image],
[Type::Document, document],
[Type::Video, video]
[Type::Video, video],
[Type::Location, location]
].each do |type_b, value|
next unless type == type_b

Expand Down
26 changes: 25 additions & 1 deletion test/whatsapp/api/messages_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ def test_send_template_with_success_response_by_passing_components
currency = Resource::Currency.new(code: "USD", amount: 1000, fallback_value: "1000")
date_time = Resource::DateTime.new(fallback_value: "2020-01-01T00:00:00Z")
image = Resource::Media.new(type: Resource::Media::Type::Image, link: "http(s)://URL")
location = Resource::Location.new(latitude: 25.779510, longitude: -80.338631, name: "miami store", address: "820 nw 87th ave, miami, fl")

parameter_image = Resource::ParameterObject.new(
type: Resource::ParameterObject::Type::Image, image: image
Expand All @@ -592,6 +593,10 @@ def test_send_template_with_success_response_by_passing_components
parameter_date_time = Resource::ParameterObject.new(
type: Resource::ParameterObject::Type::DateTime, date_time: date_time
)
parameter_location = Resource::ParameterObject.new(
type: Resource::ParameterObject::Type::Location,
location: location
)

header_component = Resource::Component.new(
type: Resource::Component::Type::Header,
Expand Down Expand Up @@ -623,6 +628,11 @@ def test_send_template_with_success_response_by_passing_components
]
)

location_component = Resource::Component.new(
type: Resource::Component::Type::Header,
parameters: [parameter_location]
)

@messages_api.expects(:send_request).with(
endpoint: "123123/messages",
params: {
Expand Down Expand Up @@ -689,6 +699,20 @@ def test_send_template_with_success_response_by_passing_components
payload: "PAYLOAD"
}
]
},
{
type: "header",
parameters: [
{
type: "location",
location: {
latitude: 25.779510,
longitude: -80.338631,
name: "miami store",
address: "820 nw 87th ave, miami, fl"
}
}
]
}
]
}
Expand All @@ -698,7 +722,7 @@ def test_send_template_with_success_response_by_passing_components

message_response = @messages_api.send_template(
sender_id: 123_123, recipient_number: 12_345_678, name: "hello_world", language: "en_US",
components: [header_component, body_component, button_component1, button_component2]
components: [header_component, body_component, button_component1, button_component2, location_component]
)

assert_mock_response(valid_contacts, valid_messages, message_response)
Expand Down
17 changes: 17 additions & 0 deletions test/whatsapp/resource/location_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# typed: true
# frozen_string_literal: true
require "test_helper"
require "resource/location"

module WhatsappSdk
module Resource
module Resource
class LocationTest < Minitest::Test
def test_to_json
location = Location.new(latitude: 25.779510, longitude: -80.338631, name: "Miami Store", address: "820 NW 87th Ave, Miami, FL")
assert_equal({ latitude: 25.779510, longitude: -80.338631, name: "Miami Store", address: "820 NW 87th Ave, Miami, FL" }, location.to_json)
end
end
end
end
end
21 changes: 20 additions & 1 deletion test/whatsapp/resource/parameter_object_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'resource/media'
require 'resource/date_time'
require 'resource/currency'
require 'resource/location'
require 'error'
require 'resource/errors'

Expand All @@ -21,11 +22,13 @@ def setup
@video_media = Media.new(type: Media::Type::Video, id: "123")
@currency = Currency.new(code: "USD", amount: 1000, fallback_value: "USD")
@date_time = DateTime.new(fallback_value: "2020-01-01T00:00:00Z")
@location = Location.new(latitude: 25.779510, longitude: -80.338631, name: "Miami Store", address: "820 NW 87th Ave, Miami, FL")
end

[
ParameterObject::Type::Text, ParameterObject::Type::Currency, ParameterObject::Type::DateTime,
ParameterObject::Type::Image, ParameterObject::Type::Document, ParameterObject::Type::Video
ParameterObject::Type::Image, ParameterObject::Type::Document, ParameterObject::Type::Video,
ParameterObject::Type::Location
].each do |type|
define_method(
"test_raise_an_error_when_type_is_#{type.serialize}_but_the_attribute_#{type.serialize}_is_not_passed"
Expand Down Expand Up @@ -54,6 +57,7 @@ def test_creates_a_valid_parameter_object_with_for_type
ParameterObject.new(type: ParameterObject::Type::Image, image: @image_media)
ParameterObject.new(type: ParameterObject::Type::Video, video: @video_media)
ParameterObject.new(type: ParameterObject::Type::Document, document: @document_media)
ParameterObject.new(type: ParameterObject::Type::Location, location: @location)
end

def test_to_json
Expand Down Expand Up @@ -98,6 +102,21 @@ def test_to_json
{ type: "date_time", date_time: { fallback_value: @date_time.fallback_value } },
parameter_date_time.to_json
)

parameter_location = ParameterObject.new(type: ParameterObject::Type::Location, location: @location)

assert_equal(
{
type: "location",
location: {
latitude: @location.latitude,
longitude: @location.longitude,
name: @location.name,
address: @location.address
}
},
parameter_location.to_json
)
end
end
end
Expand Down

0 comments on commit 1e959e6

Please sign in to comment.