mobistore 0.0.1 → 0.0.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: 800098ef8ca9da7599cb41f85d337e9309c11cc8
4
- data.tar.gz: db58211788e7d43b8a13db6eaa6ab63c0457185e
3
+ metadata.gz: 7910c1b62b6055f9b0b95e18ba9ff2ee7a3cff8b
4
+ data.tar.gz: 2aba8f49c9fb69ab64faa98d08505483337e693f
5
5
  SHA512:
6
- metadata.gz: a69400ed19d8384358a2d1f46e97372fff1df092c524c0c519fe2081c72f755fc9ee9a21967d4202d0125c5b2acbe6a47882e1439158e5fd9ec467458459258a
7
- data.tar.gz: a6ae8be734193b8d0a10c3344549c2a3c7287a22185439c917d0955191231478f5f173f569e0001a06d40b8a212ee60aac5248ca3d4323664d107088eb375509
6
+ metadata.gz: deaf8b2ed0d4fcdffd6e2daf846b7abfc0928a35ae136320db82e541a5dc8c95d6a83e5ea7a80c0636b9559157d8072dcf84805f7bf28e52e63a05527774254e
7
+ data.tar.gz: 248a364d4265da3b9223bbd289b1903a9cad80092ee2bf861e53097f4ed978363dbccea7b3e6afa0e64f0de3f909b00a97f1d7c58a223896d0b4a9dfd0442dc3
data/lib/BuyerAPI.rb ADDED
@@ -0,0 +1,49 @@
1
+ require_relative 'UserModule.rb'
2
+
3
+ class BuyerAPI
4
+
5
+ extend UserModule
6
+
7
+ @@name = "Raj Negi"
8
+ @@money = 5000
9
+ @@cc_details = "icic"
10
+
11
+ def self.buy
12
+ puts "\tEnter the details of product you want to buy: "
13
+ product_id = get_number("ID")
14
+ product = ProductAPI.search_id(product_id)
15
+
16
+ if !product
17
+ puts "Product doesn't exist"
18
+ elsif @@money < product[:price]
19
+ puts "Insufficient funds to make purchase!"
20
+ elsif product[:quantity].to_i < 1
21
+ puts "Product out of stock!"
22
+ else
23
+ ProductAPI.update(product[:id], :quantity, product[:quantity] - 1)
24
+ @@money -= product[:price]
25
+ transcation = {
26
+ product_name: product[:name],
27
+ buyer_name: @@name,
28
+ buyer_cc_details: @@cc_details,
29
+ timestamp: Time.now.to_s,
30
+ amount_paid: product[:price]
31
+ }
32
+ TranscationAPI.record(transcation)
33
+ puts "\n\tPurchase successful, current amount left: #{@@money}"
34
+ end
35
+ end
36
+
37
+ def self.set_name(n)
38
+ @@name = n
39
+ end
40
+
41
+ def self.set_money(m)
42
+ @@money = m
43
+ end
44
+
45
+ def self.set_cc_details(cc)
46
+ @@cc_details = cc
47
+ end
48
+
49
+ end
@@ -0,0 +1,9 @@
1
+ {"name":"LG 5","color":"grey","price":300,"quantity":4,"id":0}
2
+ {"name":"Samsung Galaxy Note","color":"galaxy white","price":324,"quantity":11,"id":1}
3
+ {"name":"Apple iPhone 7","color":"space black","price":600,"quantity":2,"id":2}
4
+ {"name":"OnePlus 3","color":"wooden","price":601,"quantity":0,"id":3}
5
+ {"del":true}
6
+ {"name":"Oppo Smartphone","color":"matte","price":250,"quantity":2,"id":5}
7
+ {"del":true}
8
+ {"del":true}
9
+ {"name":"new phone","color":"glossy","price":0,"quantity":1,"id":6}
@@ -0,0 +1,7 @@
1
+ {"id":0,"product_name":"p","buyer_name":"b","buyer_cc_details":"icic","timestamp":"TIME","amount_paid":2100}
2
+ {"product_name":"OnePlus 3","buyer_name":"raj","buyer_cc_details":"icic","timestamp":"2016-09-16 10:55:43 +0530","amount_paid":200,"id":1}
3
+ {"product_name":"Oppo Smartphone","buyer_name":"raj","buyer_cc_details":"icic","timestamp":"2016-09-16 11:09:10 +0530","amount_paid":250,"id":2}
4
+ {"product_name":"Apple iPhone 7","buyer_name":"raj","buyer_cc_details":"icic","timestamp":"2016-09-16 15:42:24 +0530","amount_paid":600,"id":3}
5
+ {"product_name":"Samsung Galaxy Note","buyer_name":"raj","buyer_cc_details":"icic","timestamp":"2016-09-16 15:44:41 +0530","amount_paid":324,"id":4}
6
+ {"product_name":"LG 5","buyer_name":"raj","buyer_cc_details":"icic","timestamp":"2016-09-16 15:46:42 +0530","amount_paid":300,"id":5}
7
+ {"product_name":"Oppo Smartphone","buyer_name":"Raj Negi","buyer_cc_details":"icic","timestamp":"2016-09-16 16:11:04 +0530","amount_paid":250,"id":6}
@@ -0,0 +1,35 @@
1
+ require 'json'
2
+
3
+ module FileStorageModule
4
+
5
+ def read_file(fp)
6
+ list = []
7
+ File.open(fp, 'r') do |f|
8
+ f.each do |line|
9
+ content = JSON.parse(line, symbolize_names: true)
10
+ list.push( content ) unless content[:del]
11
+ end
12
+ end
13
+ return list
14
+ end
15
+
16
+ def add_line(fp, data)
17
+ new_line = data.to_json
18
+ File.open(fp, 'a') do |file|
19
+ file.puts new_line
20
+ end
21
+ end
22
+
23
+ def update_line(fp, old_data, new_data)
24
+ old_line = old_data.to_json
25
+ new_line = new_data.to_json
26
+ content = File.read(fp)
27
+ content.sub!(old_line, new_line)
28
+ File.open(fp, "w") {|file| file.puts content }
29
+ end
30
+
31
+ def remove_line(fp, data)
32
+ update_line(fp, data, {del: true})
33
+ end
34
+
35
+ end
data/lib/ProductAPI.rb ADDED
@@ -0,0 +1,70 @@
1
+ require_relative 'FileStorageModule'
2
+ require 'byebug'
3
+ class ProductAPI
4
+
5
+ extend FileStorageModule
6
+
7
+ @@file_path = File.join(File.dirname(caller[0]), "/FileStorage/products.log")
8
+
9
+ def self.seed
10
+ byebug
11
+ @@list = read_file(@@file_path)
12
+ end
13
+
14
+ def self.get_list
15
+ @@list ||= Array.new
16
+ end
17
+
18
+ def self.add(product)
19
+ @@list ||= Array.new
20
+ product[:id] = max_id_value + 1
21
+ puts "ProductAPI#add: Creating a product with these details: #{product}"
22
+ @@list.push(product)
23
+ add_line(@@file_path, product)
24
+ end
25
+
26
+ def self.search(key, value)
27
+ if(key==:id || key==:quantity || key==:price)
28
+ value = value.to_i
29
+ end
30
+ @@list.each do |product|
31
+ return product if product[key] == value
32
+ end
33
+ return nil
34
+ end
35
+
36
+ def self.search_id(id)
37
+ search(:id, id)
38
+ end
39
+
40
+ def self.update(id, key, value)
41
+ unless(key==:name || key==:color || key==:price || key==:quantity)
42
+ puts "Invalid command. Cannot update #{key} field."
43
+ return
44
+ end
45
+ if(key==:quantity || key==:price)
46
+ value = value.to_i
47
+ end
48
+ old_product = search_id(id).clone
49
+ @@list = @@list.map do |product|
50
+ if product[:id] == id
51
+ product[key] = value
52
+ puts "ProductAPI#update: Updating product id#{id} with these details: #{key}: #{value}"
53
+ update_line(@@file_path, old_product, product)
54
+ end
55
+ product
56
+ end
57
+ end
58
+
59
+ def self.delete(id)
60
+ puts "ProductAPI#delete: Deleting product id#{id}"
61
+ @@list -= [search_id(id)]
62
+ remove_line(@@file_path, search_id(id))
63
+ end
64
+
65
+ private
66
+ def self.max_id_value
67
+ @@list.inject(0){|max, p| max = max > p[:id]? max:p[:id] }
68
+ end
69
+
70
+ end
data/lib/SellerAPI.rb ADDED
@@ -0,0 +1,38 @@
1
+ require_relative 'UserModule.rb'
2
+
3
+ class SellerAPI
4
+
5
+ attr_accessor :id, :name, :money, :valid_hash
6
+
7
+ extend UserModule
8
+
9
+ def self.add_product
10
+ puts "Enter product details:"
11
+
12
+ name = get_string("Name")
13
+ color = get_string("Color")
14
+ price = get_number("Price")
15
+ quantity = get_number("Quantity")
16
+ product = {name: name, color: color, price: price, quantity: quantity}
17
+ ProductAPI.add(product)
18
+ end
19
+
20
+ def self.update_product
21
+ list_products
22
+ puts "\n\tEnter the product needed to be updated: "
23
+
24
+ id = get_number("ID of product")
25
+ key = get_string("Update field ")
26
+ value = get_string("New value for '#{key}'")
27
+ ProductAPI.update(id, key.to_sym, value)
28
+ end
29
+
30
+ def self.delete_product
31
+ list_products
32
+ print "\n\tEnter product id to be deleted: "
33
+ id = gets.chomp.to_i
34
+ ProductAPI.delete(id)
35
+ end
36
+
37
+
38
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'FileStorageModule.rb'
2
+
3
+ class TranscationAPI
4
+
5
+ extend FileStorageModule
6
+
7
+ @@file_path = File.join(File.dirname(caller[0]), "/FileStorage/transcations.log")
8
+
9
+ def self.seed
10
+ @@list = read_file(@@file_path)
11
+ end
12
+
13
+ def self.record(transcation)
14
+ @@list ||= Array.new
15
+ transcation[:id] = @@list.size
16
+ @@list.push(transcation)
17
+ puts "TranscationAPI#record: Recording a transcation with these details: #{transcation}"
18
+ add_line(@@file_path, transcation)
19
+ end
20
+
21
+ def self.get_list
22
+ @@list
23
+ end
24
+
25
+ end
data/lib/UserModule.rb ADDED
@@ -0,0 +1,39 @@
1
+ require_relative "ProductAPI.rb"
2
+
3
+ module UserModule
4
+ def list_products
5
+ system 'clear'
6
+ puts ProductAPI.get_list
7
+ end
8
+
9
+ def search_product
10
+ list_products
11
+ print "\n"
12
+ key = get_string("product by? 'name' | 'id' | 'color' ")
13
+ value = get_string("search value for '#{key}' ")
14
+ puts ProductAPI.search(key.to_sym, value)
15
+ end
16
+
17
+ def get_number(label)
18
+ print "\tEnter #{label}: "
19
+ value = gets.chomp
20
+ if !!(value =~ /[0-9]/)
21
+ return value.to_i
22
+ else
23
+ puts "\tInvalid #{label} value. Try again."
24
+ return self.get_number(label)
25
+ end
26
+ end
27
+
28
+ def get_string(label)
29
+ print "\tEnter #{label}: "
30
+ value = gets.chomp
31
+ unless value.strip.empty?
32
+ return value
33
+ else
34
+ puts "\tInvalid #{label} value. Try again."
35
+ return self.get_string(label)
36
+ end
37
+ end
38
+
39
+ end
data/lib/mobistore.rb CHANGED
@@ -1,7 +1,7 @@
1
- require Dir.pwd + '/lib/ProductAPI.rb'
2
- require Dir.pwd + '/lib/TranscationAPI.rb'
3
- require Dir.pwd + '/lib/BuyerAPI.rb'
4
- require Dir.pwd + '/lib/SellerAPI.rb'
1
+ require_relative 'ProductAPI.rb'
2
+ require_relative 'TranscationAPI.rb'
3
+ require_relative 'BuyerAPI.rb'
4
+ require_relative 'SellerAPI.rb'
5
5
 
6
6
  class MobiStore
7
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobistore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raj Negi
@@ -17,6 +17,14 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - lib/BuyerAPI.rb
21
+ - lib/FileStorage/products.log
22
+ - lib/FileStorage/transcations.log
23
+ - lib/FileStorageModule.rb
24
+ - lib/ProductAPI.rb
25
+ - lib/SellerAPI.rb
26
+ - lib/TranscationAPI.rb
27
+ - lib/UserModule.rb
20
28
  - lib/mobistore.rb
21
29
  homepage: http://github.com/rajn-webonise
22
30
  licenses: