Class: NCMB::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
# File 'lib/ncmb/client.rb', line 12

def initialize(params = {})
  @domain          = NCMB::DOMAIN
  @api_version     = NCMB::API_VERSION
  @application_key = params[:application_key]
  @client_key      = params[:client_key]
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



11
12
13
# File 'lib/ncmb/client.rb', line 11

def api_version
  @api_version
end

#application_keyObject

Returns the value of attribute application_key.



11
12
13
# File 'lib/ncmb/client.rb', line 11

def application_key
  @application_key
end

#client_keyObject

Returns the value of attribute client_key.



11
12
13
# File 'lib/ncmb/client.rb', line 11

def client_key
  @client_key
end

#domainObject

Returns the value of attribute domain.



11
12
13
# File 'lib/ncmb/client.rb', line 11

def domain
  @domain
end

Instance Method Details

#data_store(name) ⇒ Object



19
20
21
# File 'lib/ncmb/client.rb', line 19

def data_store(name)
  NCMB::DataStore.new self, name
end

#encode_query(queries = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/ncmb/client.rb', line 31

def encode_query(queries = {})
  queries.each do |k, v|
    if v.is_a? Hash
      queries[k] = URI.escape(v.to_json.to_s, /[^-_.!~*'()a-zA-Z\d;\/?:@&=+$,#]/)
    else
      queries[k] = URI.escape(v.to_s, /[^-_.!~*'()a-zA-Z\d;\/?:@&=+$,#]/)
    end
  end
  queries
end

#generate_signature(method, path, now = nil, queries = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ncmb/client.rb', line 42

def generate_signature(method, path, now = nil, queries = {})
  params_base = {
    "SignatureMethod" => "HmacSHA256",
    "SignatureVersion" => "2",
    "X-NCMB-Application-Key" => @application_key
  }
  params = method == :get ? params_base.merge(encode_query(queries)) : params_base
  now ||= Time.now.utc.iso8601
  params = params.merge "X-NCMB-Timestamp" => now
  params = Hash[params.sort{|a, b| a[0].to_s <=> b[0].to_s}]
  signature_base = []
  signature_base << method.upcase
  signature_base << @domain
  signature_base << path
  signature_base << params.collect{|k,v| "#{k}=#{v}"}.join("&")
  signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), @client_key, signature_base.join("\n"))).strip()
end

#get(path, params) ⇒ Object



23
24
25
# File 'lib/ncmb/client.rb', line 23

def get(path, params)
  request :get, path, params
end

#post(path, params) ⇒ Object



27
28
29
# File 'lib/ncmb/client.rb', line 27

def post(path, params)
  request :post, path, params
end

#request(method, path, queries = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ncmb/client.rb', line 60

def request(method, path, queries = {})
  now = Time.now.utc.iso8601
  signature = generate_signature(method, path, now, queries)
  query = queries.collect{|k,v| "#{k}=#{v.is_a?(Hash) ? v.to_json.to_s : v}"}.join("&")
  http = Net::HTTP.new(@domain, 443)
  http.use_ssl=true
  headers = {
    "X-NCMB-Application-Key" => @application_key,
    "X-NCMB-Signature" => signature,
    "X-NCMB-Timestamp" => now,
    "Content-Type" => 'application/json'
  }
  if method == :get
    path = path + (query == '' ? "" : "?"+query)
    return http.get(path, headers).body
  else
    return http.post(path, queries.to_json, headers)
  end
end