Class: Auchandirect::ScrAPI::DummyCart

Inherits:
BaseCart
  • Object
show all
Defined in:
lib/auchandirect/scrAPI/dummy_cart.rb

Overview

A mock for Auchandirect::ScrAPI::Cart It provides a call log as a way of testing

Constant Summary collapse

@@content =

As the real store, the remote cart outlives the sessions

Hash.new(0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCart

#url

Constructor Details

#initialize(login = nil, password = nil) ⇒ DummyCart

Returns a new instance of DummyCart.



66
67
68
69
70
71
72
73
74
75
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 66

def initialize( = nil, password = nil)
  @log = []
  @login = ""
  @password = ""
  @unavailable_items = {}

  if !.nil? || !password.nil?
    relog(, password)
  end
end

Instance Attribute Details

#logObject (readonly)

Accessors to the login and passwords used to login And to the received messages log



61
62
63
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 61

def log
  @log
end

#loginObject (readonly)

Accessors to the login and passwords used to login And to the received messages log



61
62
63
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 61

def 
  @login
end

#passwordObject (readonly)

Accessors to the login and passwords used to login And to the received messages log



61
62
63
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 61

def password
  @password
end

Class Method Details

.login_parameterObject



52
53
54
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 52

def self.
  'login'
end

.login_parameters(login, password) ⇒ Object



47
48
49
50
51
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 47

def self.(,password)
  [{'name' => 'session_data', 'value' => 'crypted_data', 'type' => 'hidden'},
   {'name' => , 'value' => , 'type' => 'text'},
   {'name' => password_parameter, 'value' => password, 'type' => 'password'}]
end

.login_urlObject



44
45
46
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 44

def self.
  url+"/login"
end

.logout_urlObject



41
42
43
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 41

def self.logout_url
  url+"/logout"
end

.password_parameterObject



55
56
57
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 55

def self.password_parameter
  'password'
end

.urlObject



29
30
31
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 29

def self.url
  "http://www.#{Storexplore::Testing::DummyStoreConstants::NAME}.com"
end

.valid_emailObject

The valid login to log into this dummy store



33
34
35
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 33

def self.valid_email
  "[email protected]"
end

.valid_passwordObject

The valid password to log into this dummy store



37
38
39
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 37

def self.valid_password
  "valid-password"
end

Instance Method Details

#add_to_cart(quantity, item) ⇒ Object



97
98
99
100
101
102
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 97

def add_to_cart(quantity, item)
  if available?(item)
    @log.push(:add_to_cart)
    @@content[item] += quantity
  end
end

#add_unavailable_item(item) ⇒ Object

Makes an item temporarily unavailable



131
132
133
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 131

def add_unavailable_item(item)
  @unavailable_items[item] = true
end

#available?(item) ⇒ Boolean

Is the given item available at this moment ?

Returns:

  • (Boolean)


136
137
138
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 136

def available?(item)
  !@unavailable_items[item]
end

#cart_valueObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 104

def cart_value
  @@content.to_a.inject(0.0) do |amount,id_and_quantity|
    item = id_and_quantity.first
    quantity = id_and_quantity.last

    unit_price = item.hash.abs.to_f/1e7

    amount + quantity * unit_price
  end
end

#containing?(item, quantity) ⇒ Boolean

Does the cart contain the specified quantity of this item ?

Returns:

  • (Boolean)


126
127
128
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 126

def containing?(item, quantity)
  @@content[item] == quantity
end

#contentObject

Collection of all the different items in the cart



116
117
118
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 116

def content
  @@content.keys
end

#empty?Boolean

Is the cart empty ?

Returns:

  • (Boolean)


121
122
123
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 121

def empty?
  @@content.empty?
end

#empty_the_cartObject



92
93
94
95
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 92

def empty_the_cart
  @log.push(:empty_the_cart)
  @@content.clear
end

#logoutObject



88
89
90
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 88

def logout
  @log.push(:logout)
end

#relog(login, password) ⇒ Object

Resets the session as if a new one was started



78
79
80
81
82
83
84
85
86
# File 'lib/auchandirect/scrAPI/dummy_cart.rb', line 78

def relog(, password)
  if  != DummyCart.valid_email
    raise InvalidAccountError.new
  end

  @log.push(:login)
  @login = 
  @password = password
end