memobird 0.1.0
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 +7 -0
- data/.gitignore +11 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +35 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/memobird.rb +94 -0
- data/lib/memobird/version.rb +3 -0
- data/memobird.gemspec +35 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cf7558f75f1f4840fcdb4918c45d04aa838e2d166dd4234ca1df0c7baa5e1e65
|
4
|
+
data.tar.gz: 9afb35990d97e39eb4c59eb3427f7029311459e9c350fe8997fe4f0d2e0d5204
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4fb3857f1c9db869de6b5e1787d5ec0e28436e4f50888dcdb0eb5b56292f57812c58e08ab214e3c1eed1e22dbd2d47d64eee2ef5638cf1697c547adbbd31ad01
|
7
|
+
data.tar.gz: 9f66ba50ec86865c38e289f27f0d08100f7293ef549498703c8bcef91613e06f01f5da35e972f546154f8222430fef6bd4155b14097a8cb371c986ad6276640c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 bjzquan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Memobird
|
2
|
+
|
3
|
+
封装咕咕机[官方API](http://open.memobird.cn/upload/webapi.pdf)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'memobird'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install memobird
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'memobird'
|
25
|
+
|
26
|
+
Memobird.api_key = '申请的AK'
|
27
|
+
Memobird.memobird_id = '咕咕机设备编号'
|
28
|
+
Memobird.user_identifying = 'App账户的咕咕号'
|
29
|
+
|
30
|
+
Memobird.print_content(text: Time.now.inspect)
|
31
|
+
```
|
32
|
+
|
33
|
+
## License
|
34
|
+
|
35
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "memobird"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/memobird.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'memobird/version'
|
2
|
+
require 'erb'
|
3
|
+
require 'json'
|
4
|
+
require 'addressable/uri'
|
5
|
+
require 'http'
|
6
|
+
|
7
|
+
# Memobird API wrapper
|
8
|
+
module Memobird
|
9
|
+
|
10
|
+
HOST = 'http://open.memobird.cn/home'.freeze
|
11
|
+
API_RETRIEVE_USER_ID = "#{HOST}/setuserbind".freeze
|
12
|
+
API_PRINT_CONTENT = "#{HOST}/printpaper".freeze
|
13
|
+
|
14
|
+
class << self
|
15
|
+
attr_accessor :api_key, :user_id, :memobird_id, :user_identifying
|
16
|
+
|
17
|
+
def print_content(content = {})
|
18
|
+
raise 'Please set up first' if not_ready?
|
19
|
+
load_user_id if user_id.nil?
|
20
|
+
puts content
|
21
|
+
response = http_get(build_url(API_PRINT_CONTENT,
|
22
|
+
userid: user_id,
|
23
|
+
printcontent: build_print_content(content)))
|
24
|
+
puts response['showapi_res_error'].to_s + ' ' + response['printcontentid'].to_s
|
25
|
+
rescue StandardError => e
|
26
|
+
puts 'Print content error. ' + e.message
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def not_ready?
|
32
|
+
api_key.nil? || memobird_id.nil? || user_identifying.nil?
|
33
|
+
end
|
34
|
+
|
35
|
+
def http_get(url)
|
36
|
+
puts "Call #{url}"
|
37
|
+
JSON.parse(HTTP.get(url).to_s)
|
38
|
+
end
|
39
|
+
|
40
|
+
def load_user_id
|
41
|
+
File.open('user.id.txt', 'r').each do |line|
|
42
|
+
self.user_id = line.delete("\n")
|
43
|
+
puts 'User id ' + user_id
|
44
|
+
end
|
45
|
+
rescue StandardError => e
|
46
|
+
puts 'Load user id error. ' + e.message
|
47
|
+
puts 'Retrieve user id from API'
|
48
|
+
retrieve_user_id
|
49
|
+
save_user_id
|
50
|
+
end
|
51
|
+
|
52
|
+
def retrieve_user_id
|
53
|
+
response = http_get(build_url(API_RETRIEVE_USER_ID,
|
54
|
+
useridentifying: user_identifying))
|
55
|
+
self.user_id = response['showapi_userid'] if call_ok?(response)
|
56
|
+
rescue StandardError => e
|
57
|
+
puts 'Retrieve user id error. ' + e.message
|
58
|
+
self.user_id = nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def save_user_id
|
62
|
+
File.open('user.id.txt', 'w') do |f|
|
63
|
+
f.puts user_id
|
64
|
+
end
|
65
|
+
rescue StandardError => e
|
66
|
+
puts 'Save user id error. ' + e.message
|
67
|
+
end
|
68
|
+
|
69
|
+
# response of json format
|
70
|
+
def call_ok?(response = {})
|
71
|
+
response['showapi_res_code'] == 1 && response['showapi_res_error'] == 'ok'
|
72
|
+
rescue StandardError
|
73
|
+
false
|
74
|
+
end
|
75
|
+
|
76
|
+
def build_url(url_base, params = {})
|
77
|
+
params[:ak] = api_key
|
78
|
+
params[:timestamp] = ERB::Util.url_encode(Time.now.strftime('%F %T'))
|
79
|
+
params[:memobirdid] = memobird_id
|
80
|
+
url = Addressable::URI.new
|
81
|
+
url.query_values = params
|
82
|
+
"#{url_base}?#{url.query}"
|
83
|
+
end
|
84
|
+
|
85
|
+
def build_print_content(content = {})
|
86
|
+
build_text(content[:text]) || build_text(">> NO CONTENT <<\n")
|
87
|
+
end
|
88
|
+
|
89
|
+
def build_text(text)
|
90
|
+
return nil if text.nil?
|
91
|
+
'T:' + Base64.encode64(text.encode('GBK')).delete("\n")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
data/memobird.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'memobird/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'memobird'
|
7
|
+
spec.version = Memobird::VERSION
|
8
|
+
spec.authors = ['aaron67']
|
9
|
+
spec.email = ['aaron67@aaron67.cc']
|
10
|
+
|
11
|
+
spec.summary = %q{封装咕咕机官方API}
|
12
|
+
spec.description = %q{封装咕咕机官方提供的打印接口}
|
13
|
+
spec.homepage = 'https://github.com/gitzhou/memobird'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
20
|
+
else
|
21
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
f.match(%r{^(test|spec|features)/})
|
26
|
+
end
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ['lib']
|
30
|
+
|
31
|
+
spec.add_development_dependency 'addressable', '~>2.5.2'
|
32
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
33
|
+
spec.add_development_dependency 'http', '~>2.1.0'
|
34
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: memobird
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- aaron67
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: addressable
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.5.2
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.5.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: http
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.1.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.1.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: 封装咕咕机官方提供的打印接口
|
70
|
+
email:
|
71
|
+
- aaron67@aaron67.cc
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- bin/console
|
82
|
+
- bin/setup
|
83
|
+
- lib/memobird.rb
|
84
|
+
- lib/memobird/version.rb
|
85
|
+
- memobird.gemspec
|
86
|
+
homepage: https://github.com/gitzhou/memobird
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata:
|
90
|
+
allowed_push_host: https://rubygems.org
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.7.3
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: 封装咕咕机官方API
|
111
|
+
test_files: []
|