Class: PlugAir

Inherits:
Object
  • Object
show all
Defined in:
lib/plugair_sdk.rb,
lib/plugair_sdk/version.rb

Overview

PlugAir Class

Constant Summary collapse

HOST_NAME =

Constants

'https://api.plugairsdk.com'
BASE_RESOURCE_PATH =
'/auth/plug/v1'
PATH_CHALLENGE =
'/challenge'
PATH_VERIFY =
'/verify'
API_KEY_PARAMETER =
'apikey='
API_KEY_LENGTH =
40
SHARED_SECRET_LENGTH =
64
X_PLUGAIR_TOKEN_HEADER =
'pa'
AUTH_RESULT_OK =
'OK'
AUTH_RESULT_NG =
'NG'
AUTH_RESULT_OTHER_APP_PLUG =
'OTHER_APPS_PLUG'
VERSION =
'1.0.2'

Instance Method Summary collapse

Constructor Details

#initialize(api_key = ENV['PLUGAIR_API_KEY'], shared_secret = ENV['PLUGAIR_SHARED_SECRET']) ⇒ PlugAir

Returns a new instance of PlugAir.



49
50
51
52
53
54
# File 'lib/plugair_sdk.rb', line 49

def initialize(api_key=ENV['PLUGAIR_API_KEY'], shared_secret=ENV['PLUGAIR_SHARED_SECRET'])
  validate_api_key_and_shared_secret(api_key, shared_secret)

  @api_key = api_key
  @shared_secret = shared_secret
end

Instance Method Details

#generate_challengeObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/plugair_sdk.rb', line 57

def generate_challenge
  validate_api_key_and_shared_secret(@api_key, @shared_secret)

  resource_path = BASE_RESOURCE_PATH + PATH_CHALLENGE
  query_params  = API_KEY_PARAMETER + @api_key
  request_body  = JSON.generate({})

  uri = URI.parse(HOST_NAME + resource_path + '?' + query_params)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  req = Net::HTTP::Get.new(uri.request_uri)
  https.open_timeout = 10
  https.read_timeout = 20
  req['Content-Type'] = 'application/json'
  req['X-PlugAir-Token'] = generate_x_plugair_token(resource_path, query_params, request_body)
  res = https.request(req)

  case res
  when Net::HTTPSuccess
    json = JSON.parse(res.body)
    return json['challenge']
  else
    json = JSON.parse(res.body)
    if json['status'] and json['reason'] and json['message'] then
      raise PlugAirAuthError.new(json['status'], json['reason'], json['message'])
    else
      raise PlugAirUnknownError.new
    end
  end
end

#verify(credential_key, auth_key, auth_data) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/plugair_sdk.rb', line 89

def verify(credential_key, auth_key, auth_data)
  validate_api_key_and_shared_secret(@api_key, @shared_secret)

  resource_path = BASE_RESOURCE_PATH + PATH_VERIFY
  query_params  = API_KEY_PARAMETER + @api_key
  request_body  = JSON.generate({
    :credentialKey => credential_key,
    :authKey => auth_key,
    :authData => auth_data
  })

  uri = URI.parse(HOST_NAME + resource_path + '?' + query_params)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  req = Net::HTTP::Post.new(uri.request_uri)
  req.body = request_body
  https.open_timeout = 10
  https.read_timeout = 20
  req['Content-Type'] = 'application/json'
  req['X-PlugAir-Token'] = generate_x_plugair_token(resource_path, query_params, request_body)
  res = https.request(req)

  case res
  when Net::HTTPSuccess
    json = JSON.parse(res.body)
    return json
  else
    json = JSON.parse(res.body)
    if json['status'] and json['reason'] and json['message'] then
      raise PlugAirAuthError.new(json['status'], json['reason'], json['message'])
    else
      raise PlugAirUnknownError.new
    end
  end
end