lib/processout/coupon.rb in processout-1.0.6 vs lib/processout/coupon.rb in processout-1.0.7

- old
+ new

@@ -87,23 +87,23 @@ # +client+:: +ProcessOut+ client instance # +data+:: data that can be used to fill the object def initialize(client, data = {}) @client = client - @id = data.fetch(:id, "") - @project = data.fetch(:project, nil) - @name = data.fetch(:name, "") - @amount_off = data.fetch(:amount_off, "") - @percent_off = data.fetch(:percent_off, 0) - @currency = data.fetch(:currency, "") - @max_redemptions = data.fetch(:max_redemptions, 0) - @expires_at = data.fetch(:expires_at, "") - @metadata = data.fetch(:metadata, Hash.new) - @iteration_count = data.fetch(:iteration_count, 0) - @redeemed_number = data.fetch(:redeemed_number, 0) - @sandbox = data.fetch(:sandbox, false) - @created_at = data.fetch(:created_at, "") + self.id = data.fetch(:id, nil) + self.project = data.fetch(:project, nil) + self.name = data.fetch(:name, nil) + self.amount_off = data.fetch(:amount_off, nil) + self.percent_off = data.fetch(:percent_off, nil) + self.currency = data.fetch(:currency, nil) + self.max_redemptions = data.fetch(:max_redemptions, nil) + self.expires_at = data.fetch(:expires_at, nil) + self.metadata = data.fetch(:metadata, nil) + self.iteration_count = data.fetch(:iteration_count, nil) + self.redeemed_number = data.fetch(:redeemed_number, nil) + self.sandbox = data.fetch(:sandbox, nil) + self.created_at = data.fetch(:created_at, nil) end # Create a new Coupon using the current client def new(data = {}) @@ -112,10 +112,13 @@ # Fills the object with data coming from the API # Params: # +data+:: +Hash+ of data coming from the API def fill_with_data(data) + if data.nil? + return self + end if data.include? "id" self.id = data["id"] end if data.include? "project" self.project = data["project"] @@ -155,14 +158,40 @@ end self end + # Prefills the object with the data passed as Parameters + # Params: + # +data+:: +Hash+ of data + def prefill(data) + if data.nil? + return self + end + self.id = data.fetch(:id, self.id) + self.project = data.fetch(:project, self.project) + self.name = data.fetch(:name, self.name) + self.amount_off = data.fetch(:amount_off, self.amount_off) + self.percent_off = data.fetch(:percent_off, self.percent_off) + self.currency = data.fetch(:currency, self.currency) + self.max_redemptions = data.fetch(:max_redemptions, self.max_redemptions) + self.expires_at = data.fetch(:expires_at, self.expires_at) + self.metadata = data.fetch(:metadata, self.metadata) + self.iteration_count = data.fetch(:iteration_count, self.iteration_count) + self.redeemed_number = data.fetch(:redeemed_number, self.redeemed_number) + self.sandbox = data.fetch(:sandbox, self.sandbox) + self.created_at = data.fetch(:created_at, self.created_at) + + self + end + # Get all the coupons. # Params: # +options+:: +Hash+ of options def all(options = {}) + self.prefill(options) + request = Request.new(@client) path = "/coupons" data = { } @@ -187,10 +216,12 @@ # Create a new coupon. # Params: # +options+:: +Hash+ of options def create(options = {}) + self.prefill(options) + request = Request.new(@client) path = "/coupons" data = { "id" => @id, "amount_off" => @amount_off, @@ -219,10 +250,12 @@ # Find a coupon by its ID. # Params: # +coupon_id+:: ID of the coupon # +options+:: +Hash+ of options def find(coupon_id, options = {}) + self.prefill(options) + request = Request.new(@client) path = "/coupons/" + CGI.escape(coupon_id) + "" data = { } @@ -244,10 +277,12 @@ # Save the updated coupon attributes. # Params: # +options+:: +Hash+ of options def save(options = {}) + self.prefill(options) + request = Request.new(@client) path = "/coupons/" + CGI.escape(@id) + "" data = { "metadata" => @metadata } @@ -268,9 +303,11 @@ # Delete the coupon. # Params: # +options+:: +Hash+ of options def delete(options = {}) + self.prefill(options) + request = Request.new(@client) path = "/coupons/" + CGI.escape(@id) + "" data = { }