netprint 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in netprint.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 youpy
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.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Netprint
2
+
3
+ A library to upload file to netprint(https://www.printing.ne.jp/)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'netprint'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install netprint
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ n = Netprint::Agent.new(userid, password)
23
+ n.login
24
+
25
+ registration_code = n.upload('/path/to/file.pdf')
26
+ ```
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,92 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "tmpdir"
3
+ require "fileutils"
4
+ require "pathname"
5
+
6
+ module Netprint
7
+ class Agent
8
+ attr_reader :userid, :password
9
+
10
+ include FileUtils
11
+
12
+ def initialize(userid, password)
13
+ @userid = userid
14
+ @password = password
15
+ end
16
+
17
+ def login
18
+ page = mechanize.get(login_url)
19
+ @session_id = page.links[0].href.match(/s=(\w+)/)[1]
20
+ end
21
+
22
+ def upload(filename)
23
+ raise 'not logged in' unless login?
24
+
25
+ Dir.mktmpdir do |dir|
26
+ upload_filename = (Pathname(dir) + ([Time.now.to_f.to_s, File.basename(filename)].join('_'))).to_s
27
+ cp filename, upload_filename
28
+
29
+ page = mechanize.get(upload_url)
30
+ page = page.form_with(:name => 'uploadform') do |form|
31
+ form.file_uploads.first.file_name = upload_filename
32
+ form['papersize'] = '0'
33
+ form['color'] = '0'
34
+ form['number'] = '0'
35
+ form['secretcodesw'] = '0'
36
+ form['mailsw'] = '0'
37
+ end.submit
38
+
39
+ raise UploadError if page.search('//img[@src="/img/icn_error.jpg"]').size == 1
40
+
41
+ get_code
42
+ end
43
+ end
44
+
45
+ def login?
46
+ @session_id
47
+ end
48
+
49
+ private
50
+
51
+ def get_code
52
+ code = nil
53
+
54
+ loop do
55
+ page = mechanize.get(list_url)
56
+ _, registered_name, status = page.search('//tr[@bgcolor="#CFCFE6"][1]/td')
57
+
58
+ if status.text =~ /^[0-9A-Z]{8}+$/
59
+ code = status.text
60
+ break
61
+ elsif status.text == 'エラー'
62
+ raise RegistrationError
63
+ end
64
+
65
+ sleep 1
66
+ end
67
+
68
+ code
69
+ end
70
+
71
+ def upload_url
72
+ expand_url(:s => @session_id, :c => 0, :m => 1)
73
+ end
74
+
75
+ def list_url
76
+ expand_url(:s => @session_id, :c => 0, :m => 0)
77
+ end
78
+
79
+ def login_url
80
+ expand_url(:i => userid, :p => password)
81
+ end
82
+
83
+ def expand_url(params)
84
+ Addressable::Template.new('https://www.printing.ne.jp/cgi-bin/mn.cgi?{-join|&|i,p,s,c,m}').
85
+ expand(params)
86
+ end
87
+
88
+ def mechanize
89
+ @mechanize ||= Mechanize.new
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,6 @@
1
+ module Netprint
2
+ class Error < StandardError; end
3
+ class UploadError < Error; end
4
+ class RegistrationError < Error; end
5
+ end
6
+
@@ -0,0 +1,3 @@
1
+ module Netprint
2
+ VERSION = "0.0.1"
3
+ end
data/lib/netprint.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'addressable/template'
2
+ require 'addressable/uri'
3
+ require 'mechanize'
4
+
5
+ require 'netprint/version'
6
+ require 'netprint/agent'
7
+ require 'netprint/error'
8
+
9
+ module Netprint
10
+ # Your code goes here...
11
+ end
data/netprint.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/netprint/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["youpy"]
6
+ gem.email = ["youpy@buycheapviagraonlinenow.com"]
7
+ gem.description = %q{A library to upload file to netprint}
8
+ gem.summary = %q{A library to upload file to netprint}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "netprint"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Netprint::VERSION
17
+
18
+ gem.add_development_dependency('rspec', ['~> 2.8.0'])
19
+ gem.add_development_dependency('netprint')
20
+ gem.add_dependency('mechanize', '2.5.1')
21
+ gem.add_dependency('addressable', '2.2.6')
22
+ end
data/spec/foo.pdf ADDED
Binary file
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ include Netprint
4
+
5
+ describe Agent do
6
+ before do
7
+ unless ENV['NETPRINT_USERID'] && ENV['NETPRINT_PASSWORD']
8
+ raise 'set environment variable NETPRINT_USERID and NETPRINT_PASSWORD before running spec'
9
+ end
10
+
11
+ @agent = Agent.new(ENV['NETPRINT_USERID'], ENV['NETPRINT_PASSWORD'])
12
+ end
13
+
14
+ it 'should login' do
15
+ @agent.should_not be_login
16
+
17
+ @agent.login
18
+
19
+ @agent.should be_login
20
+ end
21
+
22
+ it 'should upload' do
23
+ filename = File.expand_path(File.dirname(__FILE__) + '/../foo.pdf')
24
+ @agent.login
25
+
26
+ code = @agent.upload(filename)
27
+ code.should match(/^[0-9A-Z]{8}$/)
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ require 'netprint'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # Require this file using `require "spec_helper"` to ensure that it is only
6
+ # loaded once.
7
+ #
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: netprint
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - youpy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.8.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.8.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: netprint
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mechanize
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 2.5.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.5.1
62
+ - !ruby/object:Gem::Dependency
63
+ name: addressable
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - '='
68
+ - !ruby/object:Gem::Version
69
+ version: 2.2.6
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '='
76
+ - !ruby/object:Gem::Version
77
+ version: 2.2.6
78
+ description: A library to upload file to netprint
79
+ email:
80
+ - youpy@buycheapviagraonlinenow.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - lib/netprint.rb
92
+ - lib/netprint/agent.rb
93
+ - lib/netprint/error.rb
94
+ - lib/netprint/version.rb
95
+ - netprint.gemspec
96
+ - spec/foo.pdf
97
+ - spec/netprint/agent_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: ''
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ segments:
112
+ - 0
113
+ hash: 3664946790013954889
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ segments:
121
+ - 0
122
+ hash: 3664946790013954889
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.24
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: A library to upload file to netprint
129
+ test_files:
130
+ - spec/foo.pdf
131
+ - spec/netprint/agent_spec.rb
132
+ - spec/spec_helper.rb