materialistic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b5f1aa0d469f291c400ca17ce5e799cb53aec295
4
+ data.tar.gz: 9b12264d7a82e389ff63fc65581f3f847abf35fc
5
+ SHA512:
6
+ metadata.gz: 4f8abd2f0a3a61e4619bc5fa7b0998e3af738d94f4c6cdf12ff86fb06c7729e12f00ac050ec7d7c886588b4691f0d06f359b89db6b7646884b62eff38eaa4f2a
7
+ data.tar.gz: 7348bce5526651ab950bc606247bbb3cdfb33507120346d866b4eacf66ec1b57cc6cf2c42d5ac8d6948400ae08def950c4dadd5844735e42ed460a14bf019ade
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /vendor/bundle
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in materialistic.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Yasuaki Uechi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ # Materialistic
2
+
3
+ Clarify materials by MPN, SKU and vague material name.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'materialistic'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install materialistic
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ materials = Materialistic.list "LCD-10862"
25
+ p materials
26
+ ```
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it ( https://github.com/uetchy/materialistic/fork )
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'materialistic'
@@ -0,0 +1,39 @@
1
+ require "materialistic/version"
2
+ require "materialistic/providers"
3
+
4
+ module Materialistic
5
+ def self.list(query)
6
+ providers.inject([]) do |results, provider|
7
+ context = provider.new
8
+ results << {
9
+ id: underscore(context.class.to_s.gsub(/(^.+::)?/, '')),
10
+ display_name: context.display_name,
11
+ items: context.list(query)
12
+ }
13
+ end
14
+ end
15
+
16
+ def self.item(args)
17
+ provider_id, sku = args.to_a.first
18
+
19
+ provider = Object.const_get("Materialistic::Providers::#{camelcase(provider_id)}").new
20
+ provider.item(sku)
21
+ end
22
+
23
+ private
24
+
25
+ def self.providers
26
+ ObjectSpace.each_object(Providers::Base.singleton_class).select{ |klass| klass.superclass == Providers::Base }
27
+ end
28
+
29
+ def self.underscore(str)
30
+ str.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
31
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
32
+ tr("-", "_").
33
+ downcase
34
+ end
35
+
36
+ def self.camelcase(str)
37
+ str.to_s.split("_").map(&:capitalize).join
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ Dir[File.expand_path('../providers', __FILE__) << '/*.rb'].each do |file|
2
+ require file
3
+ end
4
+
5
+ module Providers
6
+
7
+ end
@@ -0,0 +1,11 @@
1
+ module Materialistic
2
+ module Providers
3
+ class Base
4
+ QUANTITY_HUGE = 'QTY_HUGE'
5
+
6
+ def id
7
+ self.class.to_s.gsub(/(^.+::)?/, '')
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+
3
+ require 'mechanize'
4
+
5
+ module Materialistic
6
+ module Providers
7
+ class SwitchScience < Base
8
+ def initialize
9
+ @agent = Mechanize.new
10
+ end
11
+
12
+ def display_name
13
+ 'Switch Science'
14
+ end
15
+
16
+ def list(query)
17
+ page = @agent.get "https://www.switch-science.com/catalog/list?keyword=#{query}"
18
+
19
+ products = page/'.products_thumb/li'
20
+ products.inject([]) do |result, product|
21
+ quantity = (product/'.detail/#quantity-message').text.strip.gsub(/^在庫:/, '')
22
+
23
+ result << {
24
+ name: (product/'.detail/p').text.strip,
25
+ description: (product/'img').attr('title').text.strip,
26
+ mpn: (product/'img').attr('alt').text.strip,
27
+ sku: (product/'.addcart/input[name=plu]').attr('value').text,
28
+ price: (product/'.detail/.price').text.strip.gsub(/\D/, '').to_i,
29
+ currency: 'JPY',
30
+ quantity: quantity == '多数' ? QUANTITY_HUGE : quantity,
31
+ url: page.uri.merge((product/'a').attr('href').text).to_s,
32
+ image: "https:" + (product/'img').attr('src').text
33
+ }
34
+ end
35
+
36
+ def item(sku)
37
+ page = @agent.get "https://www.switch-science.com/catalog/#{sku}/"
38
+
39
+ table = (page/'.table-bordered-rect/tr')
40
+ quantity = (table[7]/'td').text.strip
41
+
42
+ {
43
+ name: (table[1]/'td').text.strip,
44
+ description: (page/'#description').text.strip,
45
+ mpn: (table[2]/'td').text,
46
+ sku: (table[3]/'td').text,
47
+ postage_class: (table[4]/'td/span').text.to_i,
48
+ price: (table[5]/'td/.price').text.strip.gsub(/\D/, '').to_i,
49
+ currency: 'JPY',
50
+ quantity: quantity == '多数' ? QUANTITY_HUGE : quantity,
51
+ url: page.uri.to_s,
52
+ image: (table[0]/'a').attr('href').text
53
+ }
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module Materialistic
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'materialistic/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "materialistic"
8
+ spec.version = Materialistic::VERSION
9
+ spec.authors = ["Yasuaki Uechi"]
10
+ spec.email = ["uetchy@randompaper.co"]
11
+ spec.summary = %q{Clarify materials by MPN, SKU and vague material name.}
12
+ spec.description = %q{Clarify materials by MPN, SKU and vague material name.}
13
+ spec.homepage = "https://github.com/uetchy/materialistic"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry-byebug"
25
+ spec.add_dependency "mechanize"
26
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'pry-byebug'
3
+ require 'pp'
4
+
5
+ describe Materialistic do
6
+ it 'has a version number' do
7
+ expect(Materialistic::VERSION).not_to be nil
8
+ end
9
+
10
+ it 'can list items' do
11
+ result = Materialistic.list 'LCD-10862'
12
+ expect(result.class).to eq(Array)
13
+ end
14
+
15
+ it 'can fetch item details' do
16
+ result = Materialistic.item switch_science: '790'
17
+ expect(result.class).to eq(Hash)
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'materialistic'
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: materialistic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yasuaki Uechi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mechanize
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Clarify materials by MPN, SKU and vague material name.
84
+ email:
85
+ - uetchy@randompaper.co
86
+ executables:
87
+ - materialistic
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - bin/materialistic
99
+ - lib/materialistic.rb
100
+ - lib/materialistic/providers.rb
101
+ - lib/materialistic/providers/base.rb
102
+ - lib/materialistic/providers/switch-science.rb
103
+ - lib/materialistic/version.rb
104
+ - materialistic.gemspec
105
+ - spec/materialistic_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: https://github.com/uetchy/materialistic
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.5
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Clarify materials by MPN, SKU and vague material name.
131
+ test_files:
132
+ - spec/materialistic_spec.rb
133
+ - spec/spec_helper.rb