Class: NCMB::Client

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

Constant Summary

Constants included from NCMB

API_VERSION, DOMAIN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NCMB

initialize

Constructor Details

#initialize(params = {}) ⇒ Client

Returns a new instance of Client.



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

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.



16
17
18
# File 'lib/ncmb/client.rb', line 16

def api_version
  @api_version
end

#application_keyObject

Returns the value of attribute application_key.



16
17
18
# File 'lib/ncmb/client.rb', line 16

def application_key
  @application_key
end

#client_keyObject

Returns the value of attribute client_key.



16
17
18
# File 'lib/ncmb/client.rb', line 16

def client_key
  @client_key
end

#domainObject

Returns the value of attribute domain.



16
17
18
# File 'lib/ncmb/client.rb', line 16

def domain
  @domain
end

Instance Method Details

#array2hash(ary) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ncmb/client.rb', line 40

def array2hash(ary)
  new_v = {}
  ary.each do |hash|
    if hash.is_a? Hash
      key = hash.keys[0]
      new_v[key] = hash[key]
    else
      new_v = [hash]
    end
  end
  new_v
end

#delete(path, params) ⇒ Object



36
37
38
# File 'lib/ncmb/client.rb', line 36

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

#encode_query(queries = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ncmb/client.rb', line 53

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

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ncmb/client.rb', line 81

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 = params.sort_by{|a, b| a.to_s}.to_h
  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.new('sha256'), @client_key, signature_base.join("\n"))).strip()
end

#get(path, params) ⇒ Object



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

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

#hash2query(queries = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ncmb/client.rb', line 66

def hash2query(queries = {})
  results = {}
  queries.each do |k, v|
    v = array2hash(v) if v.is_a? Array
    if v.is_a? Hash
      results[k.to_s] = v.to_json.to_s
    else
      results[k.to_s] = v.to_s
    end
  end
  results.collect do |key, value|
    "#{key}=#{value}"
  end
end

#post(path, params) ⇒ Object



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

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

#put(path, params) ⇒ Object



32
33
34
# File 'lib/ncmb/client.rb', line 32

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

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ncmb/client.rb', line 99

def request(method, path, queries = {})
  now = Time.now.utc.iso8601
  signature = generate_signature(method, path, now, queries)
  query = hash2query(queries).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 + URI.escape((query == '' ? "" : "?"+query), /[^-_.!~*'()a-zA-Z\d;\/?@&=+$,#]/)
    return JSON.parse(http.get(path, headers).body, symbolize_names: true)
  elsif method == :post
    return JSON.parse(http.post(path, queries.to_json, headers).body, symbolize_names: true)
  elsif method == :put
    return JSON.parse(http.put(path, queries.to_json, headers).body, symbolize_names: true)
  elsif method == :delete
    response = http.delete(path, headers).body
    if response && response.length >= 2
        return JSON.parse(response, symbolize_names: true)
    else
        return response
    end
  end
end