nofxx-pyradise 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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Marcos Piccinini
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,35 @@
1
+ = PYradise
2
+
3
+ Fetch/search and store Paraguay's prices!
4
+
5
+
6
+ == Install
7
+
8
+ * dm-core
9
+ * do_sqlite
10
+
11
+ sudo gem install nofxx-pyradise
12
+
13
+ == Use
14
+
15
+ Fetch all:
16
+
17
+ pyradise fetch
18
+
19
+ List all:
20
+
21
+ pyradise list
22
+
23
+ Search for a new 2TB Sata drive:
24
+
25
+ pyradise list SATA2
26
+
27
+ View the price oscillation of a item:
28
+
29
+ pyradise view <ID>
30
+
31
+
32
+
33
+ == Copyright
34
+
35
+ Copyright (c) 2009 Marcos Piccinini. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "pyradise"
8
+ gem.summary = "Paraguay gem!"
9
+ gem.email = "x@nofxx.com"
10
+ gem.homepage = "http://github.com/nofxx/pyradise"
11
+ gem.authors = ["Marcos Piccinini"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.pattern = 'spec/**/*_spec.rb'
23
+ spec.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |spec|
29
+ spec.libs << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "pyradise #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
data/bin/pyradise ADDED
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+ # Pyradise
3
+ #
4
+ # Created on 2008-9-4.
5
+ # Copyright (c) 2008. All rights reserved.
6
+ #
7
+ begin
8
+ require 'rubygems'
9
+ rescue LoadError
10
+ # no rubygems to load, so we fail silently
11
+ require 'lib/pyradise'
12
+ end
13
+
14
+ require 'optparse'
15
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
17
+ require 'pyradise'
18
+
19
+
20
+ include Pyradise
21
+
22
+ OPTIONS = {
23
+ }
24
+
25
+ MANDATORY_OPTIONS = %w( )
26
+
27
+ parser = OptionParser.new do |opts|
28
+ opts.banner = <<BANNER
29
+ Pyradise - PY ftw
30
+
31
+ Usage: #{File.basename($0)} command [options]
32
+
33
+ Options are:
34
+
35
+ BANNER
36
+ opts.separator ""
37
+
38
+ opts.on("-s", "--search=SEARCH", String,
39
+ "Search for.") { |OPTIONS[:search]| }
40
+
41
+
42
+ opts.on("-v", "--version",
43
+ "Show program version") { puts "Pyradise v001"; exit }
44
+
45
+ opts.parse!(ARGV)
46
+
47
+ if MANDATORY_OPTIONS && MANDATORY_OPTIONS.find { |option| OPTIONS[option.to_sym].nil? }
48
+ puts opts; exit
49
+ end
50
+ end
51
+
52
+ if ARGV.empty?
53
+ puts parser.banner
54
+ exit
55
+ else
56
+ Pyradise.run! ARGV, OPTIONS
57
+ end
@@ -0,0 +1,38 @@
1
+ class Product
2
+ include DataMapper::Resource
3
+
4
+ property :id, Serial
5
+ property :sid, String
6
+ property :name, String, :nullable => false, :length => (2..20)
7
+ property :price, Integer
8
+ property :store, String
9
+ property :prices, Text
10
+ # has n, :orders
11
+
12
+ # def self.count
13
+ # all.length
14
+ # end
15
+ # def to_param
16
+ # id.to_s
17
+ # end
18
+ before :save do
19
+ # self[:prices] = Marshal.dump({ Time.now.to_i => price })
20
+ phist = { Time.now.to_i => price }
21
+ self[:prices] = Marshal.dump(prices ? prices.merge(phist) : phist)
22
+
23
+ end
24
+
25
+ def prices
26
+ self[:prices] ? Marshal.load(self[:prices]) : nil
27
+ end
28
+
29
+ def prices=(price)
30
+ self[:prices] = Marshal.dump(prices ? prices.merge(price) : price)
31
+ end
32
+
33
+
34
+ #no idea why..
35
+ def self.identity_field
36
+ "product"
37
+ end
38
+ end
@@ -0,0 +1,8 @@
1
+ class Stat
2
+ include DataMapper::Resource
3
+
4
+ property :id, Serial
5
+ property :key, String
6
+ property :value, String, :nullable => false, :length => (2..20)
7
+
8
+ end
data/lib/pyradise.rb ADDED
@@ -0,0 +1,126 @@
1
+ require 'rubygems'
2
+ require 'open-uri'
3
+ require 'do_sqlite3'
4
+ require 'dm-core'
5
+ #require "dm-tokyo-adapter"
6
+ require 'pyradise/product'
7
+ require 'pyradise/stat'
8
+
9
+ #TODO: def init
10
+ HOME = ENV['HOME'] + "/.pyradise/"
11
+ #DataMapper.setup(:default, :adapter => 'tokyo_cabinet', :database => 'data.tct', :path => HOME)
12
+ DataMapper.setup(:default, :adapter => 'sqlite3', :database => HOME + "py.sqlite3")
13
+ # DataMapper.auto_migrate!
14
+
15
+ module Pyradise
16
+
17
+ DOLETA = 2.2
18
+ TAX = 1.3
19
+
20
+ class << self
21
+
22
+ def run! comm, opts
23
+ send(*comm)
24
+ end
25
+
26
+ def fetch
27
+ create fetch_stores
28
+ end
29
+
30
+ def create txts
31
+ products = []
32
+ for txt in txts
33
+ print "Parsing #{txt[:store]}..."
34
+ c = Product.all.length
35
+ parse(txt[:txt], txt[:delimiter]).each do |p|
36
+ if prod = Product.first(:name => p[:name], :store => txt[:store])
37
+ prod.price = p[:price]
38
+ else
39
+ Product.create(p.merge(:store => txt[:store]))
40
+ end
41
+ end
42
+ puts "#{Product.all.length - c} products created."
43
+ end
44
+ end
45
+
46
+ def fetch_stores
47
+ stores = []
48
+ for store in YAML.load(File.new(File.dirname(__FILE__) + '/stores.yml'))[:stores]
49
+ data = {}
50
+ data[:store] = store[0]
51
+ data[:delimiter] = store[1][:delimiter]
52
+ dump = open("#{HOME}#{store[0]}-#{Time.now.to_i}.txt", "wb")
53
+ data[:txt] = open(store[1][:txt]).read
54
+ dump.write(data[:txt])
55
+ dump.close
56
+ puts "Store #{store[0]} dumped... "
57
+ stores << data
58
+ end
59
+ stores
60
+ end
61
+
62
+ def parse(txt, del)
63
+ products = []
64
+ txt.each_line do |l|
65
+ sid, name, price = l.split(del)
66
+ next unless price
67
+ products << { :sid => sid.strip, :name => name.strip, :price => price.to_i }
68
+ end
69
+ products
70
+ end
71
+
72
+ def get_or_create_home
73
+ unless File.exists? HOME
74
+ FileUtils.mkdir_p HOME
75
+ end
76
+ HOME
77
+ end
78
+
79
+ def list(*query)
80
+ t = Time.now
81
+ w = terminal_size[0]
82
+ q = {}
83
+ q.merge!(:name.like => "%#{query[0]}%") if query[0]
84
+ q.merge!(:order => [query[1]]) if query[1]
85
+ puts "Searching #{'"' + query[0] + '"' if query[0]}... Order by: #{query[1] || 'name'}"
86
+ puts "_" * w
87
+ prods = q.empty? ? Product.all : Product.all(q)
88
+ prods.each do |prod|
89
+ s = w - 35
90
+ name = prod.name.length > s ? prod.name[0..s] + ".." : prod.name
91
+ puts sprintf("%-6s | %-5s | %-#{w-38}s %-3d | R$ %d", prod.store, prod.sid, name, prod.price, prod.price * DOLETA * TAX)
92
+ end
93
+ puts "_" * w
94
+ puts "Total: #{prods.length} (#{Time.now - t}s)"
95
+ end
96
+
97
+ def view(sid)
98
+ if !sid
99
+ puts "Use: pyradise view <ID>"
100
+ elsif !prod = Product.first(:sid => sid.to_i)
101
+ puts "Product not found."
102
+ else
103
+ w = terminal_size[0] - 50
104
+ prices = prod.prices
105
+ max = prices.values.max
106
+ prices.keys.sort.each do |k|
107
+ rel = "=" * (prices[k] * 100 / max)
108
+ puts "#{Time.at(k).strftime('%M/%d')} #{rel}| #{prices[k]}"
109
+ end
110
+ end
111
+ end
112
+
113
+ def clear
114
+ Product.all.destroy
115
+ end
116
+
117
+ #from highliner
118
+ def terminal_size
119
+ `stty size`.split.map { |x| x.to_i }.reverse
120
+ end
121
+
122
+ def red(txt)
123
+ "[e[31m#{txt}e[0m]"
124
+ end
125
+ end
126
+ end
data/lib/stores.yml ADDED
@@ -0,0 +1,9 @@
1
+ :stores:
2
+ :master:
3
+ :url: http://www.master10.com
4
+ :txt: http://www.master10.com.py/importar/arquivos/lista.master.txt
5
+ :delimiter: "|"
6
+ :nave:
7
+ :url: http://www.navenet.com
8
+ :txt: http://www.navenet.com/gerartxt.aspx
9
+ :delimiter: !ruby/regexp /\s{3,}/
data/pyradise.gemspec ADDED
@@ -0,0 +1,57 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{pyradise}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Marcos Piccinini"]
9
+ s.date = %q{2009-07-27}
10
+ s.default_executable = %q{pyradise}
11
+ s.email = %q{x@nofxx.com}
12
+ s.executables = ["pyradise"]
13
+ s.extra_rdoc_files = [
14
+ "LICENSE",
15
+ "README.rdoc"
16
+ ]
17
+ s.files = [
18
+ ".document",
19
+ ".gitignore",
20
+ "LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "bin/pyradise",
25
+ "lib/pyradise.rb",
26
+ "lib/pyradise/product.rb",
27
+ "lib/pyradise/stat.rb",
28
+ "lib/stores.yml",
29
+ "pyradise.gemspec",
30
+ "script/console",
31
+ "spec/pyradise/product_spec.rb",
32
+ "spec/pyradise/stat_spec.rb",
33
+ "spec/pyradise_spec.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/nofxx/pyradise}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.5}
40
+ s.summary = %q{Paraguay gem!}
41
+ s.test_files = [
42
+ "spec/spec_helper.rb",
43
+ "spec/pyradise_spec.rb",
44
+ "spec/pyradise/stat_spec.rb",
45
+ "spec/pyradise/product_spec.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ else
54
+ end
55
+ else
56
+ end
57
+ end
data/script/console ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # Pyradise
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ ['irb', 'pyradise', 'readline', 'rubygems'].each {|e| require e }
6
+
7
+ #history_file = File.join(ENV["HOME"], '.myrb_history')
8
+ #IO.readlines(history_file).each {|e| Readline::HISTORY << e.chomp } if File.exists?(history_file)
9
+ while (input = Readline.readline('>> ', true)) != 'exit'
10
+ begin puts "=> #{eval(input).inspect}"; rescue Exception; puts "Error: #{$!}" end
11
+ end
12
+ #File.open(history_file, 'w') {|f| f.write Readline::HISTORY.to_a.join("\n") }
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "Product" do
4
+ it "should create a product" do
5
+ Product.create(:sid => "111", :name => "CPU", :price => 1000).should be_true
6
+ end
7
+
8
+ it "should list products" do
9
+ Product.all.length.should eql(1)
10
+ end
11
+
12
+ it "should clean up" do
13
+ Product.first.destroy
14
+ Product.all.should be_empty
15
+ end
16
+
17
+ it "should store all prices" do
18
+ Time.stub_chain(:now, :to_i).and_return(1)
19
+ p = Product.create(:name => "foo", :price => 1300)
20
+ p.prices.should eql({1 => 1300})
21
+ p.prices = { 2 => 1000 }
22
+ p.prices.should eql({1 => 1300, 2 => 1000})
23
+ end
24
+
25
+
26
+ end
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "Product" do
4
+ it "should create a product" do
5
+ Stat.create(:key => "update", :value => Marshal.dump(Time.now))
6
+ # Stat.first.destroy
7
+ Stat.all.length.should eql(1)
8
+ end
9
+
10
+ it "should list products" do
11
+ Stat.all.should_not be_empty
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe "Pyradise" do
4
+ it "should fetch the stores" do
5
+ pending
6
+ Time.stub_chain(:now, :to_i).and_return(55)
7
+ Pyradise.should_receive(:open).with("a","wb").and_return(mf = mock("File"))
8
+ Pyradise.should_receive(:open).with("http://www.master10.com.py/importar/arquivos/lista.master.txt").and_return(mr = mock("Remote"))
9
+ mr.should_receive(:read).and_return("11| Cool stuff | 20.00")
10
+ mf.should_receive(:write).with("11| Cool stuff | 20.00").and_return true
11
+ mf.should_receive(:close).and_return true
12
+ Product.should_receive(:create).with({:store=>:master,:sid=>"11", :price=>" 20.00", :name=>" Cool stuff "})
13
+ Pyradise.fetch.should be_true
14
+ end
15
+
16
+ it "should create a home folder" do
17
+ Pyradise.get_or_create_home.should match("pyradise")
18
+ end
19
+
20
+
21
+ end
22
+
@@ -0,0 +1,11 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'pyradise'
6
+ DataMapper.setup(:default, :adapter => 'sqlite3', :database => HOME + "pytest.sqlite3")
7
+ DataMapper.auto_migrate!
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nofxx-pyradise
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Marcos Piccinini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-27 00:00:00 -07:00
13
+ default_executable: pyradise
14
+ dependencies: []
15
+
16
+ description:
17
+ email: x@nofxx.com
18
+ executables:
19
+ - pyradise
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - bin/pyradise
33
+ - lib/pyradise.rb
34
+ - lib/pyradise/product.rb
35
+ - lib/pyradise/stat.rb
36
+ - lib/stores.yml
37
+ - pyradise.gemspec
38
+ - script/console
39
+ - spec/pyradise/product_spec.rb
40
+ - spec/pyradise/stat_spec.rb
41
+ - spec/pyradise_spec.rb
42
+ - spec/spec_helper.rb
43
+ has_rdoc: false
44
+ homepage: http://github.com/nofxx/pyradise
45
+ licenses:
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Paraguay gem!
70
+ test_files:
71
+ - spec/spec_helper.rb
72
+ - spec/pyradise_spec.rb
73
+ - spec/pyradise/stat_spec.rb
74
+ - spec/pyradise/product_spec.rb