Class: MaropostApi::ProductsAndRevenue

Inherits:
Object
  • Object
show all
Defined in:
lib/maropost_api/products_and_revenue.rb

Overview

Contains methods that will create, update, read and delete the services provided in the Product and Revenue Api

Instance Method Summary collapse

Constructor Details

#initialize(account = ENV["ACCOUNT"], api_key = ENV["API_KEY"]) ⇒ ProductsAndRevenue

Creates a new instance of Reports class.

Parameters:

  • account (Integer) (defaults to: ENV["ACCOUNT"])

    is authentic user account id (Integer) provided by maropost.com

  • api_key (String) (defaults to: ENV["API_KEY"])

    is the auth token (String) that is validated on the server to authenticate the user



13
14
15
16
# File 'lib/maropost_api/products_and_revenue.rb', line 13

def initialize( = ENV["ACCOUNT"], api_key = ENV["API_KEY"])
  MaropostApi.instance_variable_set(:@api_key, api_key)
  MaropostApi.instance_variable_set(:@account, )
end

Instance Method Details

#create_order(require_unique, contact: {}, order: {}, order_items: [], add_tags: [], remove_tags: [], uid: nil, list_ids: nil, grand_total: nil, campaign_id: nil, coupon_code: nil) ⇒ Object

Creates a new order from the given parameters

Parameters:

  • require_unique (Boolean)

    validates that the order has a unique original_order_id for the given contact.

  • contact (Hash) (defaults to: {})

    Named parameter, which if exists, must contain :email as its key

  • order (Hash) (defaults to: {})

    Named parameter, which must contain :order_date, :order_status, :original_order_id, :order_items as its keys

  • @order_items (OrderItem)

    Custom Type containing key value pair to reference an order item

  • add_tags (Array) (defaults to: [])

    List of string tags to be added in the new order

  • remove_tags (Array) (defaults to: [])

    List of string tags to be removed from the new order

  • uid (String) (defaults to: nil)

    Unique Identifier

  • list_ids (String|CSV) (defaults to: nil)

    A csv list fo list ids

  • grand_total (Float) (defaults to: nil)

    An amount referencing the total of the order

  • campaing_id (Integer)

    ID of the Campaign the order belongs to

  • coupon_code (String) (defaults to: nil)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/maropost_api/products_and_revenue.rb', line 51

def create_order(
    require_unique,
    contact: {},
    order: {},
    order_items: [],
    add_tags: [],
    remove_tags: [],
    uid: nil,
    list_ids: nil,
    grand_total: nil,
    campaign_id: nil,
    coupon_code: nil
)
  # required contacts fields to check for
  [:email,].each do |contact_field|
    raise ArgumentError.new "contact[:#{contact_field}] is required!" if contact.has_key?(contact_field.to_sym) == false
  end
  # order items validation
  order_items.each do |item|
    raise TypeError.new("Each order item must be an instance of maropost_api/custom_types/OrderItem: " << item.class.to_s << " given") unless item.kind_of?(OrderItem)
    order[:order_items] ||= []
    order[:order_items].push(item.to_hash)
  end
  # required order fields to check for
  [:order_date, :order_status, :original_order_id].each do |order_field|
    raise ArgumentError.new "order[:#{order_field}] is required!" if order.has_key?(order_field) == false
  end

  params = order
  params[:order_items] = order_items
  params[:contact] = contact
  params[:uid] = uid
  params[:list_ids] = list_ids
  params[:grand_total] = grand_total
  params[:campaign_id] = campaign_id
  params[:coupon_code] = coupon_code

  full_path = full_resource_path

  MaropostApi.post_result(full_path, {"order" => params})
end

#delete_for_order_id(order_id) ⇒ Object



145
146
147
148
149
150
# File 'lib/maropost_api/products_and_revenue.rb', line 145

def delete_for_order_id(order_id)
  full_path = full_resource_path('/find')
  query_params = MaropostApi.set_query_params({'where[id]' => order_id})

  MaropostApi.delete_result(full_path, query_params)
end

#delete_for_original_order_id(original_order_id) ⇒ Object

deletes an order fo the given original order id

Parameters:

  • original_order_id (String)

    Orginal Order Id of the Order



138
139
140
141
142
143
# File 'lib/maropost_api/products_and_revenue.rb', line 138

def delete_for_original_order_id(original_order_id)
  full_path = full_resource_path("/#{original_order_id}")
  query_params = MaropostApi.set_query_params

  MaropostApi.delete_result(full_path, query_params)
end

#delete_products_for_order_id(order_id, product_ids) ⇒ Object

deletes products for the specified order id

Parameters:

  • order_id (Integer)

    ID of the order

  • product_ids (Array)

    List of product IDs to be deleted

Raises:

  • (TypeError)


168
169
170
171
172
173
174
# File 'lib/maropost_api/products_and_revenue.rb', line 168

def delete_products_for_order_id(order_id, product_ids)
  raise TypeError.new('product_ids should be an Array') if ! product_ids.kind_of? Array
  full_path = full_resource_path("/find")
  query_params = MaropostApi.set_query_params({"product_ids" => product_ids.join(','), 'where[id]' => order_id})

  MaropostApi.delete_result(full_path, query_params)
end

#delete_products_for_original_order_id(original_order_id, product_ids) ⇒ Object

deletes products for the given original order id

Parameters:

  • original_order_id (String)

    Orginal Order Id of the Order

  • product_ids (Array)

    List of product ids to delete



156
157
158
159
160
161
162
# File 'lib/maropost_api/products_and_revenue.rb', line 156

def delete_products_for_original_order_id(original_order_id, product_ids)
  raise TypeError.new('product_ids should be an Array') until product_ids.is_a? Array
  full_path = full_resource_path("/#{original_order_id}")
  query_params = MaropostApi.set_query_params({"product_ids" => product_ids.join(',')})

  MaropostApi.delete_result(full_path, query_params)
end

#get_order(id) ⇒ Object

Gets the order for the specified id

Parameters:

  • id (Integer)

    unique identifier of the order



21
22
23
24
25
26
# File 'lib/maropost_api/products_and_revenue.rb', line 21

def get_order(id)
  full_path = full_resource_path("/find")
  query_params = MaropostApi.set_query_params({"where[id]" => id})

  MaropostApi.get_result(full_path, query_params)
end

#get_order_for_original_order_id(original_order_id) ⇒ Object

gets the order for the specified original order id

Parameters:

  • original_order_id (String)

    A unique identifier that’s not changed since the creation date



31
32
33
34
35
36
# File 'lib/maropost_api/products_and_revenue.rb', line 31

def get_order_for_original_order_id(original_order_id)
  full_path = full_resource_path("/#{original_order_id}")
  query_params = MaropostApi.set_query_params

  MaropostApi.get_result(full_path, query_params)
end

#update_order_for_order_id(order_id, order: {}) ⇒ Object

updates an order fo the given original order id

Parameters:

  • original_order_id (String)

    Id of the Order

  • order (Hash) (defaults to: {})

    Key value pairs that must contain at least :order_date, :order_status and :order_items as its keys

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/maropost_api/products_and_revenue.rb', line 118

def update_order_for_order_id(order_id, order: {})
  raise ArgumentError.new('order_id is required') if order_id.nil?

  [:order_date, :order_status, :order_items].each do |f|
    raise ArgumentError.new("order[:#{f}] is required") unless order.has_key?(f)
  end
  order[:order_items].each do |oi|
    raise TypeError.new('each order item should be a type of ' << OrderItem.class.to_s) unless oi.kind_of? OrderItem
  end
  order[:order_items].map! {|i| i.to_hash }

  full_path = full_resource_path("/find")
  query_params = MaropostApi.set_query_params({"where[id]" => order_id})

  MaropostApi.put_result(full_path, {order: order}, query_params)
end

#update_order_for_original_order_id(original_order_id, order: {}) ⇒ Object

updates an order fo the given original order id

Parameters:

  • original_order_id (String)

    Orginal Order Id of the Order

  • order (Hash) (defaults to: {})

    Key value pairs that must contain at least :order_date, :order_status and :order_items as its keys

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/maropost_api/products_and_revenue.rb', line 97

def update_order_for_original_order_id(original_order_id, order: {})
  raise ArgumentError.new('original_order_id is required') if original_order_id.nil?

  [:order_date, :order_status, :order_items].each do |f|
    raise ArgumentError.new("order[:#{f}] is required") unless order.has_key?(f)
  end
  order[:order_items].each do |oi|
    raise TypeError.new('each order item should be a type of ' << OrderItem.class.to_s) unless oi.kind_of? OrderItem
  end
  order[:order_items].map! {|i| i.to_hash }

  full_path = full_resource_path("/#{original_order_id}")
  form_body = {order: order}

  MaropostApi.put_result(full_path, form_body)
end