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

VERSION

Instance Attribute Summary collapse

Attributes included from ConfirmationCode

#password, #username

Instance Method Summary collapse

Methods included from ConfirmationCode

#use

Instance Attribute Details

#app_idObject (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_keyObject (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 (options = {})
   = damatu_options(options)
  ['sign'] = sign(['user'])
  response = client.get(ACCOUNT_URL, )
  result(JSON.parse(response.body))
end

#clientObject



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 damatu_options(options)
  damatu_options = {}
  damatu_options['appID'] = @app_id
  damatu_options['user'] = options[:user_name]
  damatu_options['pwd'] = get_pwd(options[:user_name], options[:user_pw])
  damatu_options['type'] = options[:type] unless options[:type].nil?
  return damatu_options
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, options = {})
  recognition_options = damatu_options(options)
  recognition_options['id'] = ret_id.to_s
  recognition_options['sign'] = sign(recognition_options['user'], ret_id.to_s.bytes)
  response = client.post(RECOGNITION_ERROR_URL, recognition_options)
  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 set_extra_options(options)
  @app_key = options[:app_key]
  @app_id = options[: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, options = {})
  upload_options = damatu_options(options)
  upload_options['url'] = CGI::escape(image_url)
  upload_options['sign'] = sign(upload_options['user'], image_url.bytes)
  upload_options['type'] = 200 if upload_options['type'].nil?
  response = client.get(UPLOAD_URL, upload_options)
  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, options = {})
  upload_options = damatu_options(options)
  upload_options['type'] = 200 if upload_options['type'].nil?
  byte_data = File.read(image_path)
  File.open(image_path) do |file|
    upload_options['file'] = file
    upload_options['sign'] = sign(upload_options['user'], byte_data.bytes)
    response = client.post(UPLOAD_LOCAL_URL, upload_options)
    result(JSON.parse(response.body))
  end
end