class Ncmb::Client

Public Class Methods

new(application_key, client_key) click to toggle source
# File lib/ncmb_rb_wrapper.rb, line 12
def initialize(application_key, client_key)
  @application_key = application_key
  @client_key = client_key
  @api_version = '2013-09-01'
  @domain = 'mb.api.cloud.nifty.com'
end

Public Instance Methods

batch(array_request) click to toggle source
# File lib/ncmb_rb_wrapper.rb, line 35
def batch(array_request)
  request(:POST, '/batch', requests: array_request)
end
data(klass = nil) click to toggle source
# File lib/ncmb_rb_wrapper.rb, line 43
def data(klass = nil)
  if klass.nil?
    DataStore.new(@application_key, @client_key)
  else
    DataStore.new(@application_key, @client_key, klass)
  end
end
delete(path) click to toggle source
# File lib/ncmb_rb_wrapper.rb, line 31
def delete(path)
  request(:DELETE, path)
end
file() click to toggle source
# File lib/ncmb_rb_wrapper.rb, line 39
def file
  FileStore.new(@application_key, @client_key)
end
get(path, hash_query = {}) click to toggle source
# File lib/ncmb_rb_wrapper.rb, line 19
def get(path, hash_query = {})
  request(:GET, path, hash_query)
end
post(path, hash_query = {}) click to toggle source
# File lib/ncmb_rb_wrapper.rb, line 23
def post(path, hash_query = {})
  request(:POST, path, hash_query)
end
put(path, hash_query = {}) click to toggle source
# File lib/ncmb_rb_wrapper.rb, line 27
def put(path, hash_query = {})
  request(:PUT, path, hash_query)
end

Private Instance Methods

header(signature, current_time, content_type = 'application/json') click to toggle source
# File lib/ncmb_rb_wrapper.rb, line 105
def header(signature, current_time, content_type = 'application/json')
  {
    'X-NCMB-Application-Key' => @application_key,
    'X-NCMB-Signature' => signature,
    'X-NCMB-Timestamp' => current_time,
    'Content-Type' => content_type,
  }
end
request(method, path, hash_query = {}) { |http, path, current_time, signature| ... } click to toggle source
# File lib/ncmb_rb_wrapper.rb, line 52
def request(method, path, hash_query = {})
  http = Net::HTTP.new(@domain, 443)
  http.use_ssl = true

  path = "/#{@api_version}#{path}"
  current_time = Time.now.utc.iso8601
  signature = sign(method, hash_query, path, current_time)

  if block_given?
    yield(http, path, current_time, signature)
  else
    case method
    when :GET
      query = URI.encode_www_form(hash_query.map { |k, v| [k, v.to_json] })
      path = "#{path}?#{query}"
      res = http.get(path, header(signature, current_time))
      JSON.parse(res.body)
    when :POST
      res = http.post(path, hash_query.to_json, header(signature, current_time))
      JSON.parse(res.body)
    when :PUT
      res = http.put(path, hash_query.to_json, header(signature, current_time))
      JSON.parse(res.body)
    when :DELETE
      res = http.delete(path, header(signature, current_time))
      'successfully deleted.' if res.code == "200"
    else
      raise Exception 'undefined method.'
    end
  end
# rescue => e
#   puts e
end
sign(method, hash_query, path, current_time) click to toggle source
# File lib/ncmb_rb_wrapper.rb, line 86
def sign(method, hash_query, path, current_time)
  hash = {
    'SignatureMethod' => 'HmacSHA256',
    'SignatureVersion' => 2,
    'X-NCMB-Application-Key' => @application_key,
    'X-NCMB-Timestamp' => current_time
  }

  if method == :GET
    hash_query.each do |key, value|
      hash.store(key.to_s, URI.encode_www_form_component(value.to_json))
    end
  end
  hash = Hash[hash.sort]
  chained_param = hash.map { |k, v| "#{k}=#{v}" }.join('&')
  base_param = [method, @domain, path, chained_param].join("\n")
  signature = Base64.encode64(OpenSSL::HMAC::digest('sha256', @client_key, base_param))
end