Class: AliyunIot::Request::Json

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyun_iot/request/json.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method: "post", params: {}) ⇒ Json

Returns a new instance of Json.



21
22
23
24
25
# File 'lib/aliyun_iot/request/json.rb', line 21

def initialize(method: "post", params: {})
  @params = params
  @method = method
  @client = HttpClient.new(base_url)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



8
9
10
# File 'lib/aliyun_iot/request/json.rb', line 8

def body
  @body
end

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/aliyun_iot/request/json.rb', line 8

def client
  @client
end

#methodObject (readonly)

Returns the value of attribute method.



8
9
10
# File 'lib/aliyun_iot/request/json.rb', line 8

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/aliyun_iot/request/json.rb', line 8

def params
  @params
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/aliyun_iot/request/json.rb', line 8

def url
  @url
end

Instance Method Details

#executeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/aliyun_iot/request/json.rb', line 27

def execute
  ts = Time.now.utc.strftime('%FT%TZ')
  base_params = {
      Format: 'JSON',
      Version: '2017-04-20',
      AccessKeyId: access_key_id,
      SignatureMethod: 'HMAC-SHA1',
      Timestamp: ts,
      SignatureVersion: '1.0',
      SignatureNonce: SecureRandom.uuid,
      RegionId: region_id,
      ServiceCode: 'iot',
  }
  exec_params = encode base_params.merge!(params)
  begin
    JSON.parse client.send(method, exec_params).body
  rescue => e
    logger = Logger.new(STDOUT)
    logger.error e.message
    logger.error e.backtrace.join("\n")
    raise e
  end
end