Module: ConfirmationCode::Service::Damatu
- Extended by:
- Damatu
- Includes:
- ConfirmationCode
- Included in:
- Damatu
- Defined in:
- lib/confirmation_code/service/damatu.rb
Constant Summary collapse
- HOST =
'http://api.dama2.com:7766'- UPLOAD_URL =
File.join(HOST, 'app/d2Url?')
- UPLOAD_LOCAL_URL =
File.join(HOST, 'app/d2File?')
- ACCOUNT_URL =
File.join(HOST, 'app/d2Balance?')
- RECOGNITION_ERROR_URL =
File.join(HOST, 'app/d2ReportError?')
Constants included from ConfirmationCode
Instance Attribute Summary collapse
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#app_key ⇒ Object
readonly
Returns the value of attribute app_key.
Attributes included from ConfirmationCode
Instance Method Summary collapse
- #account(options = {}) ⇒ Object
- #client ⇒ Object
- #damatu_options(options) ⇒ Object
- #get_pwd(user, pwd) ⇒ Object
- #md5(value) ⇒ Object
- #recognition_error(ret_id, options = {}) ⇒ Object
- #result(body) ⇒ Object
- #set_app_key(app_key) ⇒ Object
- #set_extra_options(options) ⇒ Object
- #sign(user, param = []) ⇒ Object
- #upload(image_url, options = {}) ⇒ Object
- #upload_local(image_path, options = {}) ⇒ Object
Methods included from ConfirmationCode
Instance Attribute Details
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
22 23 24 |
# File 'lib/confirmation_code/service/damatu.rb', line 22 def app_id @app_id end |
#app_key ⇒ Object (readonly)
Returns the value of attribute app_key.
22 23 24 |
# File 'lib/confirmation_code/service/damatu.rb', line 22 def app_key @app_key end |
Instance Method Details
#account(options = {}) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/confirmation_code/service/damatu.rb', line 74 def account( = {}) = () ['sign'] = sign(['user']) response = client.get(ACCOUNT_URL, ) result(JSON.parse(response.body)) end |
#client ⇒ Object
36 37 38 |
# File 'lib/confirmation_code/service/damatu.rb', line 36 def client @client ||= HTTPClient.new end |
#damatu_options(options) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/confirmation_code/service/damatu.rb', line 89 def () = {} ['appID'] = @app_id ['user'] = [:user_name] ['pwd'] = get_pwd([:user_name], [:user_pw]) ['type'] = [:type] unless [:type].nil? return end |
#get_pwd(user, pwd) ⇒ Object
44 45 46 |
# File 'lib/confirmation_code/service/damatu.rb', line 44 def get_pwd(user, pwd) return md5(@app_key + md5(md5(user) + md5(pwd))) end |
#md5(value) ⇒ Object
40 41 42 |
# File 'lib/confirmation_code/service/damatu.rb', line 40 def md5(value) return Digest::MD5.hexdigest(value) end |
#recognition_error(ret_id, options = {}) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/confirmation_code/service/damatu.rb', line 81 def recognition_error(ret_id, = {}) = () ['id'] = ret_id.to_s ['sign'] = sign(['user'], ret_id.to_s.bytes) response = client.post(RECOGNITION_ERROR_URL, ) result(JSON.parse(response.body)) end |
#result(body) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/confirmation_code/service/damatu.rb', line 98 def result(body) { "success" => body['ret'] == 0, "data" => body.except('ret', 'sign', 'cookie') } end |
#set_app_key(app_key) ⇒ Object
32 33 34 |
# File 'lib/confirmation_code/service/damatu.rb', line 32 def set_app_key(app_key) @app_key = app_key end |
#set_extra_options(options) ⇒ Object
27 28 29 30 |
# File 'lib/confirmation_code/service/damatu.rb', line 27 def () @app_key = [:app_key] @app_id = [:app_id] end |
#sign(user, param = []) ⇒ Object
48 49 50 51 |
# File 'lib/confirmation_code/service/damatu.rb', line 48 def sign(user, param = []) encode_str = (@app_key.bytes + user.bytes + param).pack('c*') return md5(encode_str)[0..7] end |
#upload(image_url, options = {}) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/confirmation_code/service/damatu.rb', line 53 def upload(image_url, = {}) = () ['url'] = CGI::escape(image_url) ['sign'] = sign(['user'], image_url.bytes) ['type'] = 200 if ['type'].nil? response = client.get(UPLOAD_URL, ) result(JSON.parse(response.body)) end |
#upload_local(image_path, options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/confirmation_code/service/damatu.rb', line 62 def upload_local(image_path, = {}) = () ['type'] = 200 if ['type'].nil? byte_data = File.read(image_path) File.open(image_path) do |file| ['file'] = file ['sign'] = sign(['user'], byte_data.bytes) response = client.post(UPLOAD_LOCAL_URL, ) result(JSON.parse(response.body)) end end |