Class: TxOcr::Base
- Inherits:
-
Object
- Object
- TxOcr::Base
- Defined in:
- lib/tx_ocr/base.rb
Constant Summary collapse
- HOST =
'ocr.tencentcloudapi.com'
Instance Method Summary collapse
- #encode_parameters ⇒ Object
- #fixed_paramaters ⇒ Object
- #full_request_url ⇒ Object
- #indent_result ⇒ Object
- #json ⇒ Object
- #orginal_parameters ⇒ Object
- #raw_result ⇒ Object
- #region ⇒ Object
- #request_object ⇒ Object
- #request_params ⇒ Object
- #secret_id ⇒ Object
- #secret_key ⇒ Object
- #signature ⇒ Object
- #string_for_sign ⇒ Object
- #to_params(original_hash) ⇒ Object
- #url ⇒ Object
Instance Method Details
#encode_parameters ⇒ Object
43 44 45 46 47 48 |
# File 'lib/tx_ocr/base.rb', line 43 def encode_parameters new_hash = fixed_paramaters new_hash['Action'] = @ocr_type new_hash['ImageUrl'] = encoded_image_url to_params(new_hash) end |
#fixed_paramaters ⇒ Object
50 51 52 |
# File 'lib/tx_ocr/base.rb', line 50 def fixed_paramaters { 'Nonce' => '2373', 'Region' => region, 'SecretId' => secret_id, 'Timestamp' => Time.now.to_i.to_s, 'Version' => '2018-11-19' } end |
#full_request_url ⇒ Object
74 75 76 |
# File 'lib/tx_ocr/base.rb', line 74 def full_request_url url + request_params end |
#indent_result ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/tx_ocr/base.rb', line 95 def indent_result if json['Response']['Error'] json else new_content = raw_result.join('') new_content = new_content.gsub('。', "。\n\n") new_content end end |
#json ⇒ Object
82 83 84 |
# File 'lib/tx_ocr/base.rb', line 82 def json JSON.parse(request_object.body) end |
#orginal_parameters ⇒ Object
36 37 38 39 40 41 |
# File 'lib/tx_ocr/base.rb', line 36 def orginal_parameters new_hash = fixed_paramaters new_hash['Action'] = @ocr_type new_hash['ImageUrl'] = remote_image_url to_params(new_hash) end |
#raw_result ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/tx_ocr/base.rb', line 86 def raw_result if json['Response']['Error'] json else original_data = json['Response']['TextDetections'] original_data.map { |h| h['DetectedText'] } end end |
#request_object ⇒ Object
78 79 80 |
# File 'lib/tx_ocr/base.rb', line 78 def request_object HTTParty.get(url + request_params) end |
#request_params ⇒ Object
70 71 72 |
# File 'lib/tx_ocr/base.rb', line 70 def request_params "/?#{encode_parameters}&Signature=#{signature}" end |
#secret_id ⇒ Object
28 29 30 |
# File 'lib/tx_ocr/base.rb', line 28 def secret_id TxOcr.config[:secret_id] end |
#secret_key ⇒ Object
32 33 34 |
# File 'lib/tx_ocr/base.rb', line 32 def secret_key TxOcr.config[:secret_key] end |
#signature ⇒ Object
63 64 65 66 67 68 |
# File 'lib/tx_ocr/base.rb', line 63 def signature digest = OpenSSL::Digest.new('sha1') hmac = OpenSSL::HMAC.digest(digest, secret_key, string_for_sign) encrypt = Base64.encode64(hmac).delete("\n") encrypt.gsub('+', '%2B') # 处理 + 号 end |
#string_for_sign ⇒ Object
59 60 61 |
# File 'lib/tx_ocr/base.rb', line 59 def string_for_sign "GET#{HOST}/?" + orginal_parameters end |
#to_params(original_hash) ⇒ Object
54 55 56 57 |
# File 'lib/tx_ocr/base.rb', line 54 def to_params(original_hash) sorted_by_key = Hash[original_hash.sort] sorted_by_key.map { |x| "#{x[0]}=#{x[1]}" }.join('&') end |
#url ⇒ Object
24 25 26 |
# File 'lib/tx_ocr/base.rb', line 24 def url "https://#{HOST}" end |