Class: Wegift::Order

Inherits:
Response show all
Defined in:
lib/wegift/models/order.rb

Constant Summary collapse

PATH =
'/order-digital-card'
DELIVERY_METHODS =
{:direct => 'direct', :email => 'email'}
DELIVERY_FORMATS =
{:code => 'raw', :url => 'url-instant'}

Constants inherited from Response

Response::STATUS

Instance Attribute Summary collapse

Attributes inherited from Response

#error_code, #error_details, #error_string, #status

Instance Method Summary collapse

Methods inherited from Response

#is_successful?

Constructor Details

#initialize(params = {}) ⇒ Order

Returns a new instance of Order.



15
16
17
18
19
20
# File 'lib/wegift/models/order.rb', line 15

def initialize(params = {})
  super(params)
  # default/fallback: 'direct'/'raw'
  @delivery_method = params[:delivery_method] || DELIVERY_METHODS[:direct]
  @delivery_format = params[:delivery_format] || DELIVERY_FORMATS[:code]
end

Instance Attribute Details

#amountObject

request/payload



9
10
11
# File 'lib/wegift/models/order.rb', line 9

def amount
  @amount
end

#codeObject

response/success



13
14
15
# File 'lib/wegift/models/order.rb', line 13

def code
  @code
end

#currency_codeObject

request/payload



9
10
11
# File 'lib/wegift/models/order.rb', line 9

def currency_code
  @currency_code
end

#cvc2Object

response/success



13
14
15
# File 'lib/wegift/models/order.rb', line 13

def cvc2
  @cvc2
end

#delivery_emailObject

request/payload



9
10
11
# File 'lib/wegift/models/order.rb', line 9

def delivery_email
  @delivery_email
end

#delivery_formatObject

request/payload



9
10
11
# File 'lib/wegift/models/order.rb', line 9

def delivery_format
  @delivery_format
end

#delivery_methodObject

request/payload



9
10
11
# File 'lib/wegift/models/order.rb', line 9

def delivery_method
  @delivery_method
end

#delivery_urlObject

response/success



13
14
15
# File 'lib/wegift/models/order.rb', line 13

def delivery_url
  @delivery_url
end

#expiry_dateObject

response/success



13
14
15
# File 'lib/wegift/models/order.rb', line 13

def expiry_date
  @expiry_date
end

#external_refObject

request/payload



9
10
11
# File 'lib/wegift/models/order.rb', line 9

def external_ref
  @external_ref
end

#notification_emailObject

request/payload



9
10
11
# File 'lib/wegift/models/order.rb', line 9

def notification_email
  @notification_email
end

#order_idObject

response/success



13
14
15
# File 'lib/wegift/models/order.rb', line 13

def order_id
  @order_id
end

#pinObject

response/success



13
14
15
# File 'lib/wegift/models/order.rb', line 13

def pin
  @pin
end

#product_codeObject

request/payload



9
10
11
# File 'lib/wegift/models/order.rb', line 9

def product_code
  @product_code
end

Instance Method Details

#parse(data) ⇒ Object



42
43
44
45
46
47
48
49
50
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
# File 'lib/wegift/models/order.rb', line 42

def parse(data)
  super(data)

  # nested status/error object
  # assuming root "status" is always "SUCCESS" if "e_code" is set
  #{
  #  "e_code": {
  #    "error_code": "EC001",
  #    "error_string": "Error retrieving E-Code from data processor",
  #    "status": "ERROR"
  #   },
  #  "error_code": null,
  #  "error_details": null,
  #  "error_string": null,
  #  "order_id": 18,
  #  "status": "SUCCESS"
  #}

  # override root "status" if set
  if data['e_code'] && data['e_code']['status'].eql?(STATUS[:error])
    @status = data['e_code']['status']
    @error_code = data['e_code']['error_code'] unless data['e_code']['error_code'].blank?
    @error_string = data['e_code']['error_string'] unless data['e_code']['error_string'].blank?
    @error_details = data['e_code']['error_details'] unless data['e_code']['error_details'].blank?
  end

  # set valid data
  if data['e_code'] && data['e_code']['status'].eql?(STATUS[:success])
    @code = data['e_code']['code']
    @expiry_date = data['e_code']['expiry_date']
    @pin = data['e_code']['pin']
    @cvc2 = data['e_code']['cvc2']
    @delivery_url = data['e_code']['delivery_url']
  end

  @order_id = data['order_id']

  self
end

#payloadObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wegift/models/order.rb', line 22

def payload
  {
      :product_code => @product_code,
      :currency_code => @currency_code,
      :amount => @amount,
      :delivery_method => @delivery_method,
      :delivery_format => @delivery_format,
      :notification_email => @notification_email,
      :delivery_email => @delivery_email,
      :external_ref => @external_ref,
  }
end

#post(ctx) ⇒ Object

Order Digital Card POST /api/b2b-sync/v1/order-digital-card



37
38
39
40
# File 'lib/wegift/models/order.rb', line 37

def post(ctx)
  response = ctx.request(:post, PATH, self.payload)
  self.parse(JSON.parse(response.body))
end