exchange_rate_TW 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9f4e992b329c7ee2a8427a43c16780f22e6333688e337a267af3241140adfa5c
4
+ data.tar.gz: 5bb12fa54172ba2aa3acad977fcf6e9237b27a8f528cbd41359bec6b81774351
5
+ SHA512:
6
+ metadata.gz: a1b26dbd77e288bc7f443a33bff6e8333c7e47e6dbf57d83e6c8a42683abe8cb7af6e6ca0242343b2202f76b2940c49a80eecdec31f2b216677ad9b591d4a38b
7
+ data.tar.gz: 91dd8aa96cbd6b3303360816607263541e2b0e8f5d9f3cbb4494cce315acd22d2d1df01d84d4e1ff8d72631d651fa6c3f645b337872d3ec1cf0b1c324109a51d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at Frank Tseng. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in exchange_rate.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem 'nokogiri', '~> 1.10', '>= 1.10.7'
9
+ gem "async"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 b19930813
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,44 @@
1
+ # ExchangeRate
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/exchange_rate`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'exchange_rate'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install exchange_rate
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/exchange_rate. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/exchange_rate/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the ExchangeRate project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/exchange_rate/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "exchange_rate"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/exchange_rate/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "exchange_rate_TW"
5
+ spec.version = ExchangeRate::VERSION
6
+ spec.authors = ["Frank Tseng"]
7
+ spec.email = ["b19930813@gmail.com"]
8
+
9
+ spec.summary = %q{Get exchange rate.}
10
+ spec.description = %q{Get exchange rate from ESun , Taiwan bank}
11
+ spec.homepage = "https://github.com/b19930813/Rate_Change"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ #spec.metadata["allowed_push_host"] = "'http://mygemserver.com'"
16
+
17
+ #spec.metadata["homepage_uri"] = spec.homepage
18
+ #spec.metadata["source_code_uri"] = "https://github.com/b19930813/Rate_Change"
19
+ #spec.metadata["changelog_uri"] = "https://github.com/b19930813/Rate_Change"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
@@ -0,0 +1,409 @@
1
+ #require "./exchange_rate/version"
2
+ require "nokogiri"
3
+ require 'open-uri'
4
+ require "json"
5
+ require 'async'
6
+ require 'thread'
7
+
8
+ #回傳目前最便宜的匯率
9
+ class ExchangeRate
10
+ #根據現金匯率取得最划算的銀行資訊
11
+ def self.get_JP
12
+ taiwan_bank = TaiwanBank.get_JP
13
+ esun_bank = ESun.get_JP
14
+ taiwan_bank[:cash_sell_rate] < esun_bank[:cash_sell_rate] ? taiwan_bank : esun_bank
15
+ end
16
+
17
+ def self.get_US
18
+ taiwan_bank = TaiwanBank.get_US
19
+ esun_bank = ESun.get_US
20
+ taiwan_bank[:cash_sell_rate] < esun_bank[:cash_sell_rate] ? taiwan_bank : esun_bank
21
+ end
22
+
23
+ def self.get_CN
24
+ taiwan_bank = TaiwanBank.get_CN
25
+ esun_bank = ESun.get_CN
26
+ taiwan_bank[:cash_sell_rate] < esun_bank[:cash_sell_rate] ? taiwan_bank : esun_bank
27
+ end
28
+
29
+ #不計算玉山自己的特殊匯率
30
+ def self.Exchange_TW_TO_JP(money, isCash = true)
31
+ taiwan_bank = TaiwanBank.get_JP
32
+ esun_bank = ESun.get_JP
33
+ if taiwan_bank[:cash_sell_rate] < esun_bank[:cash_sell_rate]
34
+ isCash ? money / taiwan_bank[:cash_sell_rate] : money / taiwan_bank[:cash_sell_rate]
35
+ else
36
+ isCash ? money / esun_bank[:selling_rate] : money / esun_bank[:selling_rate]
37
+ end
38
+ end
39
+
40
+ def self.Exchange_JP_TO_TW(money, isCash = true)
41
+ taiwan_bank = TaiwanBank.get_JP
42
+ esun_bank = ESun.get_JP
43
+ if taiwan_bank[:cash_buy_rate] > esun_bank[:cash_buy_rate]
44
+ isCash ? money / taiwan_bank[:cash_buy_rate] : money / taiwan_bank[:cash_buy_rate]
45
+ else
46
+ isCash ? money / esun_bank[:buying_rate] : money / esun_bank[:buying_rate]
47
+ end
48
+ end
49
+
50
+
51
+ def self.Exchange_TW_TO_US(money, isCash = true)
52
+ taiwan_bank = TaiwanBank.get_US
53
+ esun_bank = ESun.get_US
54
+ if taiwan_bank[:cash_sell_rate] < esun_bank[:cash_sell_rate]
55
+ isCash ? money / taiwan_bank[:cash_sell_rate] : money / taiwan_bank[:cash_sell_rate]
56
+ else
57
+ isCash ? money / esun_bank[:selling_rate] : money / esun_bank[:selling_rate]
58
+ end
59
+ end
60
+
61
+
62
+
63
+ def self.Exchange_US_TO_TW(money, isCash = true)
64
+ taiwan_bank = TaiwanBank.get_US
65
+ esun_bank = ESun.get_US
66
+ if taiwan_bank[:cash_buy_rate] > esun_bank[:cash_buy_rate]
67
+ isCash ? money / taiwan_bank[:cash_buy_rate] : money / taiwan_bank[:cash_buy_rate]
68
+ else
69
+ isCash ? money / esun_bank[:buying_rate] : money / esun_bank[:buying_rate]
70
+ end
71
+ end
72
+
73
+ def self.Exchange_TW_TO_CN(money, isCash = true)
74
+ taiwan_bank = TaiwanBank.get_CN
75
+ esun_bank = ESun.get_CN
76
+ if taiwan_bank[:cash_sell_rate] < esun_bank[:cash_sell_rate]
77
+ isCash ? money / taiwan_bank[:cash_sell_rate] : money / taiwan_bank[:cash_sell_rate]
78
+ else
79
+ isCash ? money / esun_bank[:selling_rate] : money / esun_bank[:selling_rate]
80
+ end
81
+ end
82
+
83
+ def self.Exchange_CN_TO_TW(money, isCash = true)
84
+ taiwan_bank = TaiwanBank.get_CN
85
+ esun_bank = ESun.get_CN
86
+ if taiwan_bank[:cash_buy_rate] > esun_bank[:cash_buy_rate]
87
+ isCash ? money / taiwan_bank[:cash_buy_rate] : money / taiwan_bank[:cash_buy_rate]
88
+ else
89
+ isCash ? money / esun_bank[:buying_rate] : money / esun_bank[:buying_rate]
90
+ end
91
+ end
92
+
93
+ end
94
+ #bank class end
95
+
96
+ class TaiwanBank < ExchangeRate
97
+ #取得日幣,cash_buy_rate,cash_sell_rate,buying_rate,selling_rate
98
+ #取得日幣
99
+ def self.get_JP
100
+ rates = refresh_bank_rate_json
101
+ bank_result = {
102
+ cash_buy_rate:rates['results']['JPY']['cash_buy_rate'].to_f,
103
+ cash_sell_rate:rates['results']['JPY']['cash_sell_rate'].to_f,
104
+ buying_rate:rates['results']['JPY']['buying_rate'].to_f,
105
+ selling_rate:rates['results']['JPY']['selling_rate'].to_f,
106
+ bank_name:"Taiwan Bank JP"
107
+ }
108
+ bank_result
109
+ end
110
+ #using Async 取得日幣
111
+ def self.async_get_JP
112
+ Async do
113
+ @jp = get_JP
114
+ end
115
+ @jp
116
+ end
117
+
118
+ #取得美金
119
+ def self.get_US
120
+ rates = refresh_bank_rate_json
121
+ bank_result = {
122
+ cash_buy_rate:rates['results']['USD']['cash_buy_rate'].to_f,
123
+ cash_sell_rate:rates['results']['USD']['cash_sell_rate'].to_f,
124
+ buying_rate:rates['results']['USD']['buying_rate'].to_f,
125
+ selling_rate:rates['results']['USD']['selling_rate'].to_f,
126
+ bank_name:"Taiwan Bank US"
127
+ }
128
+ bank_result
129
+ end
130
+
131
+ def self.async_get_US
132
+ Async do
133
+ @us = get_US
134
+ end
135
+ @us
136
+ end
137
+
138
+ #取得人民幣
139
+ def self.get_CN
140
+ rates = refresh_bank_rate_json
141
+ bank_result = {
142
+ cash_buy_rate:rates['results']['CNY']['cash_buy_rate'].to_f,
143
+ cash_sell_rate:rates['results']['CNY']['cash_sell_rate'].to_f,
144
+ buying_rate:rates['results']['CNY']['buying_rate'].to_f,
145
+ selling_rate:rates['results']['CNY']['selling_rate'].to_f,
146
+ bank_name:"Taiwan Bank CN"
147
+ }
148
+ bank_result
149
+ end
150
+
151
+ def self.async_get_CN
152
+ Async do
153
+ @cn = get_CN
154
+ end
155
+ @cn
156
+ end
157
+
158
+
159
+ #日幣換算
160
+ def self.Exchange_TW_TO_JP(money,isCash = true)
161
+ if money.is_a? Numeric
162
+ isCash ? money / get_JP[:selling_rate] : money / get_JP[:cash_sell_rate]
163
+ else
164
+ raise "請輸入數字"
165
+ end
166
+ end
167
+
168
+ def self.Exchange_JP_TO_TW(money,isCash = true)
169
+ if money.is_a? Numeric
170
+ isCash ? money * get_JP[:buying_rate] : money * get_JP[:cash_buy_rate]
171
+ else
172
+ raise "請輸入數字"
173
+ end
174
+ end
175
+
176
+ #美金換算
177
+ def self.Exchange_TW_TO_US(money, isCash = true)
178
+ if money.is_a? Numeric
179
+ isCash ? money / get_US[:selling_rate] : money / get_US[:cash_sell_rate]
180
+ else
181
+ raise "請輸入數字"
182
+ end
183
+ end
184
+
185
+ def self.Exchange_US_TO_TW(money, isCash = true)
186
+ if money.is_a? Numeric
187
+ isCash ? money * get_US[:buying_rate] : money * get_US[:cash_buy_rate]
188
+ else
189
+ raise "請輸入數字"
190
+ end
191
+ end
192
+
193
+ #人民幣換算
194
+ def self.Exchange_TW_TO_CN(money, isCash = true)
195
+ if money.is_a? Numeric
196
+ isCash ? money / get_CN[:selling_rate] : money / get_CN[:cash_sell_rate]
197
+ else
198
+ raise "請輸入數字"
199
+ end
200
+ end
201
+
202
+ def self.Exchange_CN_TO_TW(money, isCash = true)
203
+ if money.is_a? Numeric
204
+ isCash ? money * get_CN[:buying_rate] : money * get_CN[:cash_buy_rate]
205
+ else
206
+ raise "請輸入數字"
207
+ end
208
+ end
209
+
210
+ private
211
+ def self.parseNode(node)
212
+ name = node.css("div.print_show").text
213
+ symbol = name.match(/[A-Z]+/).to_s
214
+ rates = {
215
+ cash_buy_rate: node.css("td[data-table=本行現金買入]")[0].text,
216
+ cash_sell_rate: node.css("td[data-table=本行現金賣出]")[0].text,
217
+ buying_rate: node.css("td[data-table=本行即期買入]")[0].text,
218
+ selling_rate: node.css("td[data-table=本行即期賣出]")[0].text,
219
+ name: name.strip
220
+ }
221
+ data = { symbol.to_sym => rates }
222
+ end
223
+
224
+ def self.refresh_bank_rate_json
225
+ url = "https://rate.bot.com.tw/xrt?Lang=zh-TW"
226
+ html = Nokogiri::HTML(open(url))
227
+ datetime = html.css("span.time").text
228
+ tableRows = html.css("table > tbody > tr")
229
+ rates = {
230
+ update: datetime, #自定義一個變數名稱為update抓取上面所設定的datetime
231
+ results: tableRows.reduce({}) { |accumulator, node| accumulator.merge parseNode(node) }
232
+ }
233
+
234
+ rates= rates.to_json
235
+ return json = JSON.parse(rates)
236
+ end
237
+ end
238
+
239
+
240
+ #玉山銀行的匯率
241
+ class ESun < ExchangeRate
242
+ def self.get_JP
243
+ rates = refresh_bank_rate_json(3)
244
+ bank_result = {
245
+ cash_buy_rate:rates['result']['cash_buy_rate'].to_f,
246
+ cash_sell_rate:rates['result']['cash_sell_rate'].to_f,
247
+ buying_rate:rates['result']['buying_rate'].to_f,
248
+ selling_rate:rates['result']['selling_rate'].to_f,
249
+ buying_best_rate:rates['result']['buying_best_rate'].to_f,
250
+ selling_best_rate:rates['result']['selling_best_rate'].to_f,
251
+ bank_name:"ESun Bank JP"
252
+ }
253
+ bank_result
254
+ end
255
+
256
+ def self.get_US
257
+ rates = refresh_bank_rate_json(0)
258
+ bank_result = {
259
+ cash_buy_rate:rates['result']['cash_buy_rate'].to_f,
260
+ cash_sell_rate:rates['result']['cash_sell_rate'].to_f,
261
+ buying_rate:rates['result']['buying_rate'].to_f,
262
+ selling_rate:rates['result']['selling_rate'].to_f,
263
+ buying_best_rate:rates['result']['buying_best_rate'].to_f,
264
+ selling_best_rate:rates['result']['selling_best_rate'].to_f,
265
+ bank_name:"ESun Bank US"
266
+ }
267
+ bank_result
268
+ end
269
+
270
+ def self.get_CN
271
+ rates = refresh_bank_rate_json(1)
272
+ bank_result = {
273
+ cash_buy_rate:rates['result']['cash_buy_rate'].to_f,
274
+ cash_sell_rate:rates['result']['cash_sell_rate'].to_f,
275
+ buying_rate:rates['result']['buying_rate'].to_f,
276
+ selling_rate:rates['result']['selling_rate'].to_f,
277
+ buying_best_rate:rates['result']['buying_best_rate'].to_f,
278
+ selling_best_rate:rates['result']['selling_best_rate'].to_f,
279
+ bank_name:"ESun Bank CN"
280
+ }
281
+ bank_result
282
+ end
283
+
284
+ #台幣換成日幣,change_type = 0:即期 1:現金 2:優惠匯率
285
+ def self.Exchange_TW_TO_JP(money,change_type = 0)
286
+ if money.is_a? Numeric
287
+ case change_type
288
+ when 0
289
+ money / get_JP[:selling_rate]
290
+ when 1
291
+ money / get_JP[:cash_sell_rate]
292
+ else
293
+ money / get_JP[:selling_best_rate]
294
+ end
295
+
296
+ else
297
+ raise "請輸入數字"
298
+ end
299
+ end
300
+
301
+ def self.Exchange_JP_TO_TW(money,change_type = 0)
302
+ if money.is_a? Numeric
303
+ case change_type
304
+ when 0
305
+ money * get_JP[:buying_rate]
306
+ when 1
307
+ money * get_JP[:cash_buy_rate]
308
+ else
309
+ money * get_JP[:buying_best_rate]
310
+ end
311
+
312
+ else
313
+ raise "請輸入數字"
314
+ end
315
+ end
316
+
317
+ #美金換算
318
+ def self.Exchange_TW_TO_US(money, change_type = 0)
319
+ if money.is_a? Numeric
320
+ case change_type
321
+ when 0
322
+ money / get_US[:selling_rate]
323
+ when 1
324
+ money / get_US[:cash_sell_rate]
325
+ else
326
+ money / get_US[:selling_best_rate]
327
+ end
328
+
329
+ else
330
+ raise "請輸入數字"
331
+ end
332
+ end
333
+
334
+ def self.Exchange_US_TO_TW(money, change_type = 0)
335
+ if money.is_a? Numeric
336
+ case change_type
337
+ when 0
338
+ money * get_US[:buying_rate]
339
+ when 1
340
+ money * get_US[:cash_buy_rate]
341
+ else
342
+ money * get_US[:buying_best_rate]
343
+ end
344
+ else
345
+ raise "請輸入數字"
346
+ end
347
+ end
348
+
349
+ #人民幣換算
350
+ def self.Exchange_TW_TO_CN(money, change_type = 0)
351
+ if money.is_a? Numeric
352
+ case change_type
353
+ when 0
354
+ money / get_CN[:selling_rate]
355
+ when 1
356
+ money / get_CN[:cash_sell_rate]
357
+ else
358
+ money / get_CN[:selling_best_rate]
359
+ end
360
+ else
361
+ raise "請輸入數字"
362
+ end
363
+ end
364
+
365
+ def self.Exchange_CN_TO_TW(money, change_type = 0)
366
+ if money.is_a? Numeric
367
+ case change_type
368
+ when 0
369
+ money * get_CN[:buying_rate]
370
+ when 1
371
+ money * get_CN[:cash_buy_rate]
372
+ else
373
+ money * get_CN[:buying_best_rate]
374
+ end
375
+ else
376
+ raise "請輸入數字"
377
+ end
378
+ end
379
+
380
+ # code +7
381
+ private
382
+ def self.parseNode(node,country_code)
383
+
384
+ rates = {
385
+ cash_buy_rate:node.css("td[data-name=即期買入匯率]")[country_code].text,
386
+ cash_sell_rate: node.css("td[data-name=即期賣出匯率]")[country_code].text,
387
+ buying_rate: node.css("td[data-name=現金買入匯率]")[country_code].text,
388
+ selling_rate: node.css("td[data-name=現金賣出匯率]")[country_code].text,
389
+ buying_best_rate: node.css("td[data-name]")[3 + country_code*7].text,
390
+ selling_best_rate: node.css("td[data-name]")[4 + country_code*7].text,
391
+ name: name.strip
392
+ }
393
+ rates
394
+ end
395
+
396
+ def self.refresh_bank_rate_json(country_code)
397
+ url = "https://www.esunbank.com.tw/bank/personal/deposit/rate/forex/foreign-exchange-rates"
398
+ html = Nokogiri::HTML(open(url))
399
+ datetime = html.css("span[@id = LbQuoteTime]").text
400
+ tableRows = html.css("table > tr > td")
401
+ rates = {
402
+ update: datetime,
403
+ result: parseNode(tableRows,country_code)
404
+ }
405
+ rates= rates.to_json
406
+ return json = JSON.parse(rates)
407
+ end
408
+ end
409
+
@@ -0,0 +1,3 @@
1
+ module ExchangeRate
2
+ VERSION = "0.1.0"
3
+ end
data/lib/test.rb ADDED
File without changes
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exchange_rate_TW
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Frank Tseng
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Get exchange rate from ESun , Taiwan bank
14
+ email:
15
+ - b19930813@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - exchange_rate.gemspec
31
+ - lib/exchange_rate.rb
32
+ - lib/exchange_rate/version.rb
33
+ - lib/test.rb
34
+ homepage: https://github.com/b19930813/Rate_Change
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.3.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.1.2
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Get exchange rate.
57
+ test_files: []