Class: Leal::Rewards::Client
- Inherits:
-
Object
- Object
- Leal::Rewards::Client
- Defined in:
- lib/leal/rewards/client.rb
Instance Method Summary collapse
-
#create(request_options: {}, **params) ⇒ Leal::Rewards::Types::CreateRewardsResponse
Creates a new reward for a loyalty card.
-
#delete(request_options: {}, **params) ⇒ untyped
Permanently deletes a reward.
-
#get(request_options: {}, **params) ⇒ Leal::Rewards::Types::GetRewardsResponse
Returns a single reward by ID.
- #initialize(client:) ⇒ void constructor
-
#list(request_options: {}, **params) ⇒ Array[Leal::Rewards::Types::ListRewardsResponseItem]
Returns all rewards for the store.
-
#update(request_options: {}, **params) ⇒ Leal::Rewards::Types::UpdateRewardsResponse
Updates an existing reward.
Constructor Details
#initialize(client:) ⇒ void
9 10 11 |
# File 'lib/leal/rewards/client.rb', line 9 def initialize(client:) @client = client end |
Instance Method Details
#create(request_options: {}, **params) ⇒ Leal::Rewards::Types::CreateRewardsResponse
Creates a new reward for a loyalty card. The card must belong to the same store.
The card_id is required on create but cannot be changed afterwards.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/leal/rewards/client.rb', line 78 def create(request_options: {}, **params) params = Leal::Internal::Types::Utils.normalize_keys(params) request_data = Leal::Rewards::Types::CreateRewardsRequest.new(params).to_h non_body_param_names = %w[account_id] body = request_data.except(*non_body_param_names) request = Leal::Internal::JSON::Request.new( base_url: [:base_url], method: "POST", path: "api/v1/accounts/#{URI.encode_uri_component(params[:account_id].to_s)}/rewards", body: body, request_options: ) begin response = @client.send(request) rescue Net::HTTPRequestTimeout raise Leal::Errors::TimeoutError end code = response.code.to_i if code.between?(200, 299) Leal::Rewards::Types::CreateRewardsResponse.load(response.body) else error_class = Leal::Errors::ResponseError.subclass_for_code(code) raise error_class.new(response.body, code: code) end end |
#delete(request_options: {}, **params) ⇒ untyped
Permanently deletes a reward. This cannot be undone.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/leal/rewards/client.rb', line 165 def delete(request_options: {}, **params) params = Leal::Internal::Types::Utils.normalize_keys(params) request = Leal::Internal::JSON::Request.new( base_url: [:base_url], method: "DELETE", path: "api/v1/accounts/#{URI.encode_uri_component(params[:account_id].to_s)}/rewards/#{URI.encode_uri_component(params[:id].to_s)}", request_options: ) begin response = @client.send(request) rescue Net::HTTPRequestTimeout raise Leal::Errors::TimeoutError end code = response.code.to_i return if code.between?(200, 299) error_class = Leal::Errors::ResponseError.subclass_for_code(code) raise error_class.new(response.body, code: code) end |
#get(request_options: {}, **params) ⇒ Leal::Rewards::Types::GetRewardsResponse
Returns a single reward by ID.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/leal/rewards/client.rb', line 124 def get(request_options: {}, **params) params = Leal::Internal::Types::Utils.normalize_keys(params) request = Leal::Internal::JSON::Request.new( base_url: [:base_url], method: "GET", path: "api/v1/accounts/#{URI.encode_uri_component(params[:account_id].to_s)}/rewards/#{URI.encode_uri_component(params[:id].to_s)}", request_options: ) begin response = @client.send(request) rescue Net::HTTPRequestTimeout raise Leal::Errors::TimeoutError end code = response.code.to_i if code.between?(200, 299) Leal::Rewards::Types::GetRewardsResponse.load(response.body) else error_class = Leal::Errors::ResponseError.subclass_for_code(code) raise error_class.new(response.body, code: code) end end |
#list(request_options: {}, **params) ⇒ Array[Leal::Rewards::Types::ListRewardsResponseItem]
Returns all rewards for the store. Optionally filter by card or active status.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/leal/rewards/client.rb', line 30 def list(request_options: {}, **params) params = Leal::Internal::Types::Utils.normalize_keys(params) query_params = {} query_params["card_id"] = params[:card_id] if params.key?(:card_id) query_params["active"] = params[:active] if params.key?(:active) request = Leal::Internal::JSON::Request.new( base_url: [:base_url], method: "GET", path: "api/v1/accounts/#{URI.encode_uri_component(params[:account_id].to_s)}/rewards", query: query_params, request_options: ) begin response = @client.send(request) rescue Net::HTTPRequestTimeout raise Leal::Errors::TimeoutError end code = response.code.to_i return if code.between?(200, 299) error_class = Leal::Errors::ResponseError.subclass_for_code(code) raise error_class.new(response.body, code: code) end |
#update(request_options: {}, **params) ⇒ Leal::Rewards::Types::UpdateRewardsResponse
Updates an existing reward. The card_id cannot be changed after creation.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/leal/rewards/client.rb', line 205 def update(request_options: {}, **params) params = Leal::Internal::Types::Utils.normalize_keys(params) request_data = Leal::Rewards::Types::UpdateRewardsRequest.new(params).to_h non_body_param_names = %w[account_id id] body = request_data.except(*non_body_param_names) request = Leal::Internal::JSON::Request.new( base_url: [:base_url], method: "PATCH", path: "api/v1/accounts/#{URI.encode_uri_component(params[:account_id].to_s)}/rewards/#{URI.encode_uri_component(params[:id].to_s)}", body: body, request_options: ) begin response = @client.send(request) rescue Net::HTTPRequestTimeout raise Leal::Errors::TimeoutError end code = response.code.to_i if code.between?(200, 299) Leal::Rewards::Types::UpdateRewardsResponse.load(response.body) else error_class = Leal::Errors::ResponseError.subclass_for_code(code) raise error_class.new(response.body, code: code) end end |