Class: Saklient::Cloud::Client
- Inherits:
-
Object
- Object
- Saklient::Cloud::Client
- Defined in:
- lib/saklient/cloud/client.rb
Instance Method Summary collapse
- #clone_instance ⇒ Object
-
#initialize(token, secret) ⇒ Client
constructor
A new instance of Client.
- #println(msg) ⇒ Object
- #request(method, path, params = {}) ⇒ Object
- #set_access_key(token, secret) ⇒ Object
- #set_api_root(url) ⇒ Object
- #set_api_root_suffix(suffix) ⇒ Object
- #set_timeout(sec) ⇒ Object
Constructor Details
#initialize(token, secret) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 18 19 20 21 |
# File 'lib/saklient/cloud/client.rb', line 13 def initialize(token, secret) @config = { api_root: 'https://secure.sakura.ad.jp/cloud/', api_root_suffix: nil, timeout_sec: 180 } set_access_key token, secret self end |
Instance Method Details
#clone_instance ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/saklient/cloud/client.rb', line 27 def clone_instance instance = self.class.new(@config[:token], @config[:secret]) instance.set_api_root @config[:api_root] instance.set_api_root_suffix @config[:api_root_suffix] instance end |
#println(msg) ⇒ Object
23 24 25 |
# File 'lib/saklient/cloud/client.rb', line 23 def println(msg) $stderr.puts msg end |
#request(method, path, params = {}) ⇒ Object
52 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 81 82 83 84 85 86 87 88 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 |
# File 'lib/saklient/cloud/client.rb', line 52 def request(method, path, params={}) json = params.to_json unless path =~ /^http/ url_root = @config[:api_root] if @config[:api_root_suffix] url_root += @config[:api_root_suffix] url_root = url_root.sub %r|/?$|, '/' end url = url_root + 'api/cloud/1.1' + path end url += '?' + URI.escape(json) if method == 'GET' and !params.nil? #println '// APIリクエスト中: #{method} #{path}' parsed_url = URI.parse url if method == 'GET' req = Net::HTTP::Get.new parsed_url else req = Net::HTTP::Post.new parsed_url req.body = json end req.basic_auth @config[:token], @config[:secret] extra_headers = { 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => 'saklient.ruby ver-0.0.7 rev-bab628f6f4c2a9d37b38c634b4a514cb66467939', 'X-Requested-With' => 'XMLHttpRequest', 'X-Sakura-HTTP-Method' => method, 'X-Sakura-Error-Level' => 'warning', 'X-Sakura-Request-Format' => 'json', 'X-Sakura-Response-Format' => 'json', 'X-Sakura-No-Authenticate-Header' => '1' } #println parsed_url extra_headers.each do |key, value| req[key] = value end http = Net::HTTP.new parsed_url.host, parsed_url.scheme == 'https' ? 443 : 80 # http.set_debug_output($stderr) # for debugging if parsed_url.scheme == 'https' http.use_ssl = true # http.verify_mode = OpenSSL::SSL::VERIFY_NONE end res = http.start {|conn| conn.read_timeout = @config[:timeout_sec] conn.request req } ret = JSON.parse(res.body, {:symbolize_names => true}) status = res.code.to_i unless 200 <= status && status < 300 then raise Saklient::Errors::ExceptionFactory.create(status, ret[:error_code], ret[:error_msg]) end ret end |
#set_access_key(token, secret) ⇒ Object
43 44 45 46 |
# File 'lib/saklient/cloud/client.rb', line 43 def set_access_key(token, secret) @config[:token] = token @config[:secret] = secret end |
#set_api_root(url) ⇒ Object
35 36 37 |
# File 'lib/saklient/cloud/client.rb', line 35 def set_api_root(url) @config[:api_root] = url end |
#set_api_root_suffix(suffix) ⇒ Object
39 40 41 |
# File 'lib/saklient/cloud/client.rb', line 39 def set_api_root_suffix(suffix) @config[:api_root_suffix] = suffix end |
#set_timeout(sec) ⇒ Object
48 49 50 |
# File 'lib/saklient/cloud/client.rb', line 48 def set_timeout(sec) @config[:timeout_sec] = sec end |