Class: Cyby::Kintone::RestApi

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cyby/kintone/rest_api.rb

Constant Summary collapse

AUTH =
'X-Cybozu-Authorization'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RestApi

Returns a new instance of RestApi.



10
11
12
13
14
15
# File 'lib/cyby/kintone/rest_api.rb', line 10

def initialize(app)
  config = YAML.load_file("#{ENV['HOME']}/.cyby.yml")
  self.class.base_uri "https://#{config['subdomain']}.cybozu.com/k/v1"
  @auth = Base64.encode64("#{config['login']}:#{config['password']}").chomp
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



4
5
6
# File 'lib/cyby/kintone/rest_api.rb', line 4

def app
  @app
end

Instance Method Details

#delete(path, body = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/cyby/kintone/rest_api.rb', line 54

def delete(path, body = {})
  body.merge!(app: @app)
  options = { headers: headers, body: body.to_json }
  resp = self.class.delete(path, options)
  if resp.code == 200
    resp
  else
    raise Cyby::Kintone::InvalidRecord.new(resp)
  end
end

#get(path, body = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/cyby/kintone/rest_api.rb', line 21

def get(path, body = {})
  body.merge!(app: @app)
  options = { headers: headers, body: body.to_json }
  resp = self.class.get(path, options)
  if resp.code == 200
    resp
  else
    raise Cyby::Kintone::InvalidRecord.new(resp)
  end
end

#headersObject



17
18
19
# File 'lib/cyby/kintone/rest_api.rb', line 17

def headers
  { AUTH => @auth, 'Content-Type' => 'application/json' }
end

#post(path, body = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/cyby/kintone/rest_api.rb', line 32

def post(path, body = {})
  body.merge!(app: @app)
  options = { headers: headers, body: body.to_json }
  resp = self.class.post(path, options)
  if resp.code == 200
    resp
  else
    raise Cyby::Kintone::InvalidRecord.new(resp)
  end
end

#put(path, body = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/cyby/kintone/rest_api.rb', line 43

def put(path, body = {})
  body.merge!(app: @app)
  options = { headers: headers, body: body.to_json }
  resp = self.class.put(path, options)
  if resp.code == 200
    resp
  else
    raise Cyby::Kintone::InvalidRecord.new(resp)
  end
end