Class: MagnetAPIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/magnetapiclient.rb

Instance Method Summary collapse

Constructor Details

#initialize(endpointURI, calk, secretKey = nil) ⇒ MagnetAPIClient

Returns a new instance of MagnetAPIClient.



16
17
18
19
20
# File 'lib/magnetapiclient.rb', line 16

def initialize(endpointURI , calk, secretKey = nil)
  @endpointURI = endpointURI
  @calk = calk
  @secretKey  = secretKey
end

Instance Method Details

#callwebmethod(method_name, request, request_method) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/magnetapiclient.rb', line 22

def callwebmethod(method_name, request, request_method)
  _request = request
  if has_calk(_request) == false
    _request['calk'] = @calk
  end
  _signed_querystring = get_signed_querystring(method_name, _request, request_method)

  if request_method.downcase == 'post'
    uri = URI(""<<@endpointURI<<"/"<<method_name)
    params = CGI::parse(_signed_querystring)

    req = Net::HTTP::Post.new(uri)

    req.set_form_data( params)

    res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
      http.request(req)
      end
      case res
      when Net::HTTPSuccess, Net::HTTPRedirection
        return   res.body
      else
          res.value
      end
  else
       res = open(""<<@endpointURI<<"/"<<method_name<<"?"<<_signed_querystring).read
       
      return  res
  end
end

#encode_rfc3986(s) ⇒ Object



100
101
102
103
104
# File 'lib/magnetapiclient.rb', line 100

def encode_rfc3986(s)

  s = ERB::Util.url_encode(s).gsub("%7E", "~")
  return s
end

#get_signed_querystring(method_name, request, request_method) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/magnetapiclient.rb', line 53

def get_signed_querystring(method_name, request, request_method)
  _request = request
  _request["timestamp"] = get_time_stamp()
  _sortedRequest = ksort(_request)
  _canonicalized_query = ""

  if  ( _sortedRequest.length > 0)
    for k in _sortedRequest
        _canonicalized_query << encode_rfc3986(k[0])
        _canonicalized_query << "="
        _canonicalized_query << encode_rfc3986(k[1])
        _canonicalized_query << "&"
    end
    _canonicalized_query = _canonicalized_query[0..-2]
  end
  _string_to_sign = ""
  _string_to_sign<<request_method.downcase<<"\n"<<@endpointURI.downcase<<"\n"<<method_name.downcase<<"\n"<<_canonicalized_query;
  #puts _string_to_sign
  _signed_query_string = _canonicalized_query

  if !@secretKey.nil?
    _signature = Base64.encode64(OpenSSL::HMAC.digest('sha256', @secretKey, _string_to_sign)).gsub(/\n/, "")
    _signature = encode_rfc3986(_signature)
    _signed_query_string += '&signature=' + _signature
  end
  #puts _signed_query_string
  return _signed_query_string
end

#get_time_stampObject



87
88
89
# File 'lib/magnetapiclient.rb', line 87

def get_time_stamp()
  return Time.now.strftime("%Y-%m-%dT%H:%M:%SZ")
end

#has_calk(request) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/magnetapiclient.rb', line 91

def has_calk(request)
  request.each do |key, value|
    if key.downcase == 'calk'
      return true
    end
  end
  return false
end

#ksort(d) ⇒ Object



82
83
84
85
# File 'lib/magnetapiclient.rb', line 82

def ksort(d)
  d = Hash[d.sort]
  return d
end