Class: PQSDK::Offer

Inherits:
Object
  • Object
show all
Defined in:
lib/pqsdk/offer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Offer

Returns a new instance of Offer.



6
7
8
9
10
11
12
# File 'lib/pqsdk/offer.rb', line 6

def initialize(params = {})
  params.each do |key, val|
    send("#{key}=", val)
  end

  store_ids ||= []
end

Instance Attribute Details

#brandObject

Returns the value of attribute brand.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def brand
  @brand
end

#btn_online_offers_visibleObject

Returns the value of attribute btn_online_offers_visible.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def btn_online_offers_visible
  @btn_online_offers_visible
end

#btn_other_offers_visibleObject

Returns the value of attribute btn_other_offers_visible.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def btn_other_offers_visible
  @btn_other_offers_visible
end

Returns the value of attribute btn_partner_link_text.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def btn_partner_link_text
  @btn_partner_link_text
end

Returns the value of attribute btn_partner_link_visible.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def btn_partner_link_visible
  @btn_partner_link_visible
end

#btn_print_visibleObject

Returns the value of attribute btn_print_visible.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def btn_print_visible
  @btn_print_visible
end

#btn_stores_visibleObject

Returns the value of attribute btn_stores_visible.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def btn_stores_visible
  @btn_stores_visible
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def description
  @description
end

#discountObject

Returns the value of attribute discount.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def discount
  @discount
end

#end_dateObject

Returns the value of attribute end_date.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def end_date
  @end_date
end

#imageObject

Returns the value of attribute image.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def image
  @image
end

#nationalObject

Returns the value of attribute national.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def national
  @national
end

#original_priceObject

Returns the value of attribute original_price.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def original_price
  @original_price
end

Returns the value of attribute partner_link.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def partner_link
  @partner_link
end

#priceObject

Returns the value of attribute price.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def price
  @price
end

#start_dateObject

Returns the value of attribute start_date.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def start_date
  @start_date
end

#store_idsObject

Returns the value of attribute store_ids.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def store_ids
  @store_ids
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/pqsdk/offer.rb', line 3

def title
  @title
end

Instance Method Details

#saveObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pqsdk/offer.rb', line 14

def save
  method = :post
  endpoint = "v1/offers"

  fields = {:offer => {}}
  [ :title, :description, :price, :original_price, :discount, :start_date, :end_date, :brand, :image,
    :national, :partner_link,
    :btn_other_offers_visible, :btn_partner_link_text, :btn_partner_link_visible, :btn_print_visible, :btn_stores_visible, :btn_online_offers_visible ].each do |key|
    fields[:offer][key.to_s] = send(key) unless send(key).nil?
  end
  fields[:offer]['store_ids'] = store_ids.presence || []

  res = RestLayer.send(method, endpoint, fields, {'Authorization' => "Bearer #{Token.access_token}", 'Content-Type' => 'application/json'})

  if [200, 201].include? res[0]
    # All right!
  elsif res[0] == 400
    raise Exception.new("Bad request! Error: #{res[1]['errors']}")
  else
    raise Exception.new("Unexpected HTTP status code #{res[0]}, #{res[1]}")
  end
end

#to_hashObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pqsdk/offer.rb', line 37

def to_hash
  {
    title: title,
    description: description,
    price: price,
    original_price: original_price,
    discount: discount,
    start_date: start_date,
    end_date: end_date,
    brand: brand,
    image: image,
    store_ids: store_ids
  }
end