beecart 0.1.2 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 12a7ab469f00ea88d93def182ca1c0cc046d895c
4
- data.tar.gz: 57186de3b6f3d60636efa58589dea92eef260d8a
3
+ metadata.gz: 4aaaa29438efe2a1fcae03c72606a9e7f7a3f40e
4
+ data.tar.gz: 74a8a30d656ea09eb4ae394c4c3b4cc7ee11f118
5
5
  SHA512:
6
- metadata.gz: b2640d1b3ed73fbbca91a6421c75dc6f0dd65d5a3c6d6ac7b7d158c41c51ef79f969aaa9543f589720639749d55c494eee6bb7326df15bc2eb4f5480aae7d3eb
7
- data.tar.gz: ad3ca5bcc6dc44b7b33b0f6b70aba728ca40bd5d8c7d824be1d3b2fbcaad76a88ce10a854a0356f24b65353717a18ace7acf26151ebf95b151dc296bcf9915bc
6
+ metadata.gz: fa8c26d334afae68fe475bdc584bec445231d4c31317d75a903d6c2777b9a6720fdbe2d4f8ea19800a7f2928239ea0a6b7ed0923cfa2196be88ed892c91c1bee
7
+ data.tar.gz: 88540c4713089d73156a93f56b2b5fc9d5febacc6a2de4d3e81d3356fe12fdeff481de01aeac67bd30ab4e5e614ee4dac53ced58b02f5f52c66768dab6977a93
data/lib/beecart/cart.rb CHANGED
@@ -79,11 +79,17 @@ module Beecart
79
79
  def total_price(with_tax=false)
80
80
  price = data[:items].inject(0) do |res, (key, item)|
81
81
  res += item[:price].to_i * item[:quantity].to_i
82
-
83
82
  res.to_i
84
83
  end
85
84
 
86
- return with_tax ? price + ( price * Beecart.config.tax_rate ) : price
85
+ return with_tax ? price + ( price * Beecart.config.tax_rate ).to_i : price
86
+ end
87
+
88
+ # カート内の商品の合計金額にかかる消費税を計算
89
+ #
90
+ # @return [Integer] 合計金額にかかる消費税
91
+ def tax
92
+ return ( total_price * Beecart.config.tax_rate ).to_i
87
93
  end
88
94
 
89
95
  # カート内の商品の税込み合計金額を計算する
@@ -121,20 +127,21 @@ module Beecart
121
127
 
122
128
  # 購入データを追加する
123
129
  #
124
- # @param [Symbol] key 追加するデータの識別子
125
- # @param [Symbol] validator_name Validator名
126
- # @param [Hash] data 追加するデータ
127
- # @return [Boolean]
130
+ # @param [Symbol] key 追加するデータの識別子
131
+ # @param [Symbol] validator_name Validator名
132
+ # @param [Hash] data 追加するデータ
133
+ # @return [Data] 追加されたデータ
128
134
  def append_info(key, validator_name=false, data)
129
135
 
130
136
  validator = get_validator(validator_name)
137
+ validator.run(data)
131
138
 
132
- if validator.run(data)
139
+ unless validator.error
133
140
  @data[key.to_sym] = data
134
141
  dump_data
135
- else
136
- false
137
142
  end
143
+
144
+ validator
138
145
  end
139
146
 
140
147
  # 入っている商品に変更を加える
@@ -180,41 +187,22 @@ module Beecart
180
187
  @redis.del(@key)
181
188
  end
182
189
 
183
- # 仮計上(与信)をとる
184
- #
185
- # @param [Hash] payment_info 支払情報
186
- # @return [WebPay::Charge]
187
- def authorize payment_info={}
188
- gateway.authorize total_price_with_tax, payment_info
189
- end
190
-
191
- # 本計上を取る
192
- #
193
- # @param [Hash] payment_info 支払情報
194
- # @return [WebPay::Charge]
195
- def charge payment_info={}
196
- gateway.charge total_price_with_tax, payment_info
197
- end
198
-
199
190
  private
200
191
 
201
- def gateway name=nil
202
- klass = name.nil? ?
203
- gateway_class_name(Beecart.config.default_gateway).constantize :
204
- gateway_class_name(name).constantize
205
-
206
- klass.new
207
- end
208
-
209
- def gateway_class_name name
210
- ["Beecart::Gateway::",name.to_s.camelize,"Gateway"].join('')
211
- end
212
-
213
192
  def get_validator validator_name
214
193
  begin
215
194
  validator = ["Beecart", "Validators", "#{ validator_name.to_s }_validator".camelize].join('::').constantize.new
195
+
196
+ p "--------------------------------------------------"
197
+ p "Get Validator"
198
+ p "Validator => #{ validator }"
199
+ p "Validator => #{ validator }"
200
+ p "--------------------------------------------------"
216
201
  rescue NameError
217
- validator = Beecart::Validators::DefaultValidator.new
202
+ p "--------------------------------------------------"
203
+ p "Get Validator: Failed"
204
+ p "--------------------------------------------------"
205
+ validator = Beecart::Validators::BaseValidator.new
218
206
  end
219
207
 
220
208
  validator
@@ -229,13 +217,9 @@ module Beecart
229
217
  {
230
218
  items: {},
231
219
  user_id: nil,
232
- shipping_address: {},
233
- billing_address: {},
234
- credit_card: {},
235
- shipping_instruction: {},
236
220
  created_at: Time.now.to_s,
237
221
  updated_at: Time.now.to_s
238
- }
222
+ }.merge(Beecart.config.default_cart_info)
239
223
  else
240
224
  symbolize(MessagePack.unpack(data))
241
225
  end
@@ -20,8 +20,15 @@ module Beecart
20
20
  host: 'localhost',
21
21
  port: 5555
22
22
  },
23
+
23
24
  tax_rate: 0.05,
24
- default_gateway: :webpay
25
+
26
+ default_cart_info: {
27
+ shipping_address: {},
28
+ billing_address: {},
29
+ credit_card: {},
30
+ shipping_instruction: {},
31
+ }
25
32
  }
26
33
 
27
34
  def self.defaults
@@ -41,7 +48,7 @@ module Beecart
41
48
  end
42
49
 
43
50
  attr_reader :redis
44
- attr_accessor :logger, :expire_time, :tax_rate, :default_gateway
51
+ attr_accessor :logger, :expire_time, :tax_rate, :default_cart_info
45
52
  end
46
53
 
47
54
  def self.config
@@ -6,6 +6,7 @@ module Beecart
6
6
  @varidators ||= []
7
7
  end
8
8
 
9
+ # Beecartの@validatorsに自身をPushする
9
10
  module Validator
10
11
  def self.included(base)
11
12
  Beecart.validators << base
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Beecart
4
+ module Validators
5
+ class BaseValidator
6
+
7
+ include Beecart::Validator
8
+
9
+ attr_reader :error, :messages
10
+
11
+ def initialize
12
+ @valid = true
13
+ @target_data = nil
14
+ @errors = {}
15
+ end
16
+
17
+ # Run the validation for given data
18
+ #
19
+ # @param [Hash] data Data that should be validated
20
+ # @return [Boolean] Result of Validation[:w
21
+ def run args
22
+ @target_data = args
23
+ @valid
24
+ end
25
+
26
+ protected
27
+
28
+ def record_error error_message, keys=nil
29
+ @errors[:messages] << error_message
30
+ @errors[:keys] << key
31
+ @valid = false
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module Beecart
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/beecart.rb CHANGED
@@ -7,7 +7,7 @@ require "beecart/cart"
7
7
  require "beecart/error"
8
8
 
9
9
  require "beecart/validator"
10
- require "beecart/validators/default_validator"
10
+ require "beecart/validators/base_validator"
11
11
 
12
12
  require "beecart/gateway/base_gateway"
13
13
  require "beecart/gateway/webpay_gateway"
@@ -58,7 +58,8 @@ class CartsController < ApplicationController
58
58
  }
59
59
  })
60
60
 
61
- render :text => 'OK'
61
+ # render :text => 'OK'
62
+ redirect_to :index
62
63
  end
63
64
 
64
65
  def edit_item
@@ -66,7 +67,17 @@ class CartsController < ApplicationController
66
67
  quantity: params[:quantity]
67
68
  })
68
69
 
69
- render :text => 'OK'
70
+ redirect_to :index
71
+ end
72
+
73
+ def append_info
74
+ result = current_cart.append_info(params[:key], params[:key], params[:append_data])
75
+
76
+ unless result.error
77
+ redirect_to root_path
78
+ else
79
+ render text: result.messages.join("<br />")
80
+ end
70
81
  end
71
82
 
72
83
  private
@@ -17,48 +17,63 @@
17
17
  <h2>Your Current Cart Status</h2>
18
18
 
19
19
  <%# <%= @current_cart %>
20
-
21
- <dt>Cart Key:</dt>
22
- <dd><%= @current_cart.key %></dd>
23
-
24
- <dt>Total Price:</dt>
25
- <dd><%= @current_cart.total_price %></dd>
26
-
27
- <dt>Items:</dt>
28
- <dd class="item_list">
29
- <%= @current_cart.items.each do |item| %>
30
- <dt class="item">Price</dt>
31
- <dd class="item"><%= item.price %></dd>
32
- <br />
33
- <dt class="item">Quantity</dt>
34
- <dd class="item"><%= item.quantity %></dd>
35
-
36
- <%= form_tag remove_item_path, method: 'delete' do %>
37
- <%= hidden_field_tag 'items[key]', item.item_key %>
38
- <%= button_tag "Remove This Item", class: "button" %>
20
+ <dl>
21
+ <dt>Cart Key:</dt>
22
+ <dd><%= @current_cart.key %></dd>
23
+
24
+ <dt>Total Price with TAX:</dt>
25
+ <dd><%= @current_cart.total_price(true) %></dd>
26
+
27
+ <dt>Total Price:</dt>
28
+ <dd><%= @current_cart.total_price %></dd>
29
+
30
+ <dt>Tax:</dt>
31
+ <dd><%= @current_cart.tax %></dd>
32
+
33
+ <dt>Items:</dt>
34
+ <dd class="item_list">
35
+ <% @current_cart.items.each do |item| %>
36
+ <dt class="item">Key</dt>
37
+ <dd class="item"><%= item.item_key%></dd>
38
+ <br />
39
+ <dt class="item">Price</dt>
40
+ <dd class="item"><%= item.price %></dd>
41
+ <br />
42
+ <dt class="item">Quantity</dt>
43
+ <dd class="item"><%= item.quantity %></dd>
44
+
45
+ <%= form_tag remove_item_path, method: 'delete' do %>
46
+ <%= hidden_field_tag 'items[key]', item.item_key %>
47
+ <%= button_tag "Remove This Item", class: "button" %>
48
+ <% end %>
49
+ <hr />
39
50
  <% end %>
40
- <hr />
41
- <% end %>
42
- </dd>
51
+ </dd>
43
52
 
44
- <dt>Created at:</dt>
45
- <dd><%= @current_cart.data[:created_at] %></dd>
53
+ <dt>Created at:</dt>
54
+ <dd><%= @current_cart.data[:created_at] %></dd>
46
55
 
47
- <dt>Updated at:</dt>
48
- <dd><%= @current_cart.data[:updated_at] %></dd>
56
+ <dt>Updated at:</dt>
57
+ <dd><%= @current_cart.data[:updated_at] %></dd>
58
+ </dl>
49
59
 
50
60
  <hr />
51
61
 
52
- <h2>Buy Cart</h2>
53
-
54
- <%= form_tag checkout_cart_path, method: 'post' do %>
55
- <%= button_tag "Checkout Cart", class: "button" %>
62
+ <h2>Appended Data</h2>
63
+ <% @current_cart.data.each do |key, value| %>
64
+ <% unless [ :user_id, :credit_card, :created_at, :updated_at, :items, :shipping_address, :billing_address, :shipping_instruction ].include? key.to_sym %>
65
+ <dt><%= key %></dt>
66
+ <dd><%= value %></dd>
67
+ <% end %>
56
68
  <% end %>
57
69
 
70
+ <hr />
71
+
58
72
  <h2>Edit Cart</h2>
59
73
 
60
74
  <% @current_cart.items.each do |item| %>
61
75
  <%= form_tag edit_item_path(item_key: item.item_key), method: 'put' do %>
76
+ <%= item.item_key %>
62
77
  <%= text_field_tag :quantity, item.quantity %>
63
78
  <%= button_tag "Edit Cart", class: "button" %>
64
79
  <% end %>
@@ -69,3 +84,11 @@
69
84
  <%= form_tag destroy_cart_path, method: 'delete' do %>
70
85
  <%= button_tag "Destroy Cart", class: "button" %>
71
86
  <% end %>
87
+
88
+ <h2>Append data to your cart</h2>
89
+ <%= form_tag append_info_cart_path, method: 'post' do %>
90
+ <%= text_field_tag 'key', nil, placeholder: "KEY" %>
91
+ <%= text_field_tag 'append_data[value_1]', nil, placeholder: "Value 1" %>
92
+ <%= text_field_tag 'append_data[value_2]', nil, placeholder: "Value 2" %>
93
+ <%= button_tag "Append Data", class: "button" %>
94
+ <% end %>
@@ -1,15 +1,13 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  Beecart.configure do |config|
4
- config.expire_time = 30
4
+ config.expire_time = 3000
5
5
  config.redis = {
6
6
  host: 'localhost',
7
7
  port: 5555
8
8
  }
9
9
 
10
10
  config.tax_rate = 0.05
11
-
12
- config.default_gateway = :webpay
13
11
  end
14
12
 
15
13
  module Beecart
@@ -4,6 +4,7 @@ Rails.application.routes.draw do
4
4
  post '/cart/checkout', as: 'checkout_cart', to: 'carts#charge'
5
5
  post '/cart/examine', as: 'examine_cart', to: 'carts#charge'
6
6
  post '/cart/add_item', as: 'add_item', to: 'carts#add_item'
7
+ post '/cart/append_data', as: 'append_info_cart', to: 'carts#append_info'
7
8
  delete '/cart', as: 'destroy_cart', to: 'carts#destroy'
8
9
  delete '/cart/remove_item', as: 'remove_item', to: 'carts#remove_item'
9
10
  put '/cart/:item_key/edit', as: 'edit_item', to: 'carts#edit_item'
@@ -0,0 +1,30 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Beecart
4
+ module Validators
5
+ class HogeValidator < Beecart::Validators::BaseValidator
6
+ def run args
7
+ puts '---------- hoge_validator start ----------'
8
+
9
+ puts args
10
+
11
+ args.each do |k, v|
12
+ puts "k => #{ k }"
13
+ puts "v => #{ v }"
14
+ end
15
+
16
+ if args["value_1"] != "hoge"
17
+ record_error "#{ args["value_1"] } is not suitable for value_1"
18
+ end
19
+
20
+ if args["value_2"] == "hoge"
21
+ record_error "#{ args["value_2"] } is not suitable for value_2"
22
+ end
23
+
24
+ puts '---------- hoge_validator end ----------'
25
+
26
+ super
27
+ end
28
+ end
29
+ end
30
+ end