cyby 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 44e969a7816e2e9fced715ca9e2f8cc58733892b
4
- data.tar.gz: 55a82382ec95a782a5fee67ad5a6c4d8f8006e50
3
+ metadata.gz: 53aa37324f5116067ffb2c6f162f39582d9747c4
4
+ data.tar.gz: e690a048a7c5214b2859d7e984d2223a95c0000b
5
5
  SHA512:
6
- metadata.gz: 9d028e510dc4d51b5daf0c9ffac58d8efdee20fe7deb2d8ec8c6ed786c01617383db753c3a22afe7e31b97b99fe0617369e30fb4874d9d1bb82c56bf7b035dec
7
- data.tar.gz: 6bd207b96013670275d48d5b80eb9caf31e7c9c3d98a33d431df39c29d4559752a96b56fbbb0a50f8f80c9fed01f9aff578dac66945a0e4de6f31042b806b1fd
6
+ metadata.gz: a52c55e6cdcd89396a46428f2a7886005b5a1e592fc0f201bbcab8a1cc4090773a58832ca09d2dbc971f2cbdda5784cb484174595a3d0c13fe3f24bb2f5a7ec0
7
+ data.tar.gz: 2e9463720c254d369bd1d597818c9ebf75fc9b792f33241d58272b6330fda7dbe146bdfeba9958eeeaabb7d874b82918aaf9fc39c554d5283c052a3bbe4287f5
data/README.md CHANGED
@@ -24,7 +24,7 @@ And then create '.cyby.yml' file in your home dirctory.
24
24
 
25
25
  ### Usage
26
26
 
27
- Require in ruby script.
27
+ Get records.
28
28
 
29
29
  require 'cyby'
30
30
 
@@ -50,9 +50,44 @@ Require in ruby script.
50
50
  # Select the fields.
51
51
  app.select("id", "name", "create_at").map { |record| [record.id, record.name, record.create_at] }
52
52
 
53
- # Use method chain
54
- app.select("id", "name").where("name like ?, "Bob").asc("id").each { |record| puts record.name }
53
+ # Using method chain
54
+ app.select("id", "name").where("name like ?, "Bob").asc("id").each
55
+
56
+ # Building complex query
57
+ relation = app.relation
58
+ if num > 10
59
+ relation.and("id < ?", num)
60
+ end
61
+ if time < Time.new(2015, 1, 1)
62
+ relation.and("create_at < ?", time)
63
+ end
64
+ if str.length >= 8
65
+ relation.and("name like ?" str)
66
+ end
67
+ unless sort.nil?
68
+ relation.asc(sort)
69
+ end
70
+ records = relation.all
71
+
72
+ Create a record.
73
+
74
+ record = app.new_record
75
+ record.name = "hiroponz"
76
+ record.save
77
+
78
+ Update a record.
79
+
80
+ record = app.where("id = 1").first
81
+ record.age = 30
82
+ record.save
83
+
84
+ Delete a record.
85
+
86
+ record = app.where("id = 1").first
87
+ record.delete
88
+
89
+ See the [official reference](https://cybozudev.zendesk.com/hc/ja/categories/200147600-kintone-API) to know kintone API details.
55
90
 
56
91
  ### Author
57
92
 
58
- Hiroyuki Sato <hiroyuki_sato@spiber.jp>
93
+ Hiroyuki Sato <hiroyuki_sato@spiber.jp>
@@ -1,7 +1,6 @@
1
1
  module Cyby
2
2
  module Kintone
3
3
  class App
4
- attr_reader :last_response
5
4
  LIMIT = 100
6
5
 
7
6
  def initialize(id)
@@ -26,13 +25,32 @@ module Cyby
26
25
  queries << "offset #{LIMIT * page}"
27
26
  end
28
27
  params_per_page[:query] = queries.join(" ")
29
- @last_response = @api.get('/records.json', params_per_page)
30
- unless @last_response.code == 200
31
- fail @last_response["message"]
28
+ response = @api.get('/records.json', params_per_page)
29
+ response['records'].map do |record|
30
+ Record.new(self, record)
32
31
  end
33
- @last_response['records'].map do |record|
34
- Record.new(record)
32
+ end
33
+
34
+ def save(record)
35
+ json = record.to_json_for_save
36
+ if json[:id]
37
+ resp = @api.put("/record.json", json)
38
+ else
39
+ resp = @api.post("/record.json", json)
40
+ record["$id"] = resp["id"]
35
41
  end
42
+ record["$revision"] = resp["revision"]
43
+ true
44
+ end
45
+
46
+ def delete(record)
47
+ json = { ids: [record["$id"]] }
48
+ @api.delete("/records.json", json)
49
+ true
50
+ end
51
+
52
+ def new_record
53
+ Record.new(self)
36
54
  end
37
55
 
38
56
  def relation
@@ -1,7 +1,8 @@
1
1
  module Cyby
2
2
  module Kintone
3
3
  class Record
4
- def initialize(raw)
4
+ def initialize(app, raw = {})
5
+ @app = app
5
6
  @raw = raw
6
7
  end
7
8
 
@@ -9,12 +10,28 @@ module Cyby
9
10
  if @raw.key?(key)
10
11
  convert(@raw[key])
11
12
  else
12
- fail "'#{key}' dose'nt exist"
13
+ fail "'#{key}' doesn't exist"
13
14
  end
14
15
  end
15
16
 
17
+ def []=(key, value)
18
+ @raw[key] ||= {}
19
+ case key
20
+ when "$id"
21
+ @raw[key]["type"] = "__ID__"
22
+ when "$revision"
23
+ @raw[key]["type"] = "__REVISION__"
24
+ end
25
+ @raw[key]["value"] = value
26
+ end
27
+
16
28
  def method_missing(method_name, *args)
17
- self.[](method_name.to_s.camelize(:lower))
29
+ key = method_name.to_s
30
+ if key[-1] == "="
31
+ self.[]=(key[0..-2], args[0])
32
+ else
33
+ self.[](key)
34
+ end
18
35
  end
19
36
 
20
37
  def inspect
@@ -25,6 +42,30 @@ module Cyby
25
42
  hash.inspect
26
43
  end
27
44
 
45
+ def save
46
+ @app.save(self)
47
+ end
48
+
49
+ def delete
50
+ @app.delete(self)
51
+ end
52
+
53
+ def to_json_for_save
54
+ record = @raw.select do |key, value|
55
+ case value["type"]
56
+ when "CREATOR", "CREATED_TIME", "MODIFIER", "UPDATED_TIME", "STATUS", "STATUS_ASSIGNEE", "RECORD_NUMBER", "__REVISION__", "__ID__"
57
+ false
58
+ else
59
+ true
60
+ end
61
+ end
62
+ if @raw["$id"]
63
+ { id: @raw["$id"]["value"], record: record }
64
+ else
65
+ { record: record }
66
+ end
67
+ end
68
+
28
69
  private
29
70
  def convert(args)
30
71
  type = args['type']
@@ -21,25 +21,45 @@ module Cyby
21
21
  def get(path, body = {})
22
22
  body.merge!(app: @app)
23
23
  options = { headers: headers, body: body.to_json }
24
- self.class.get(path, options)
24
+ resp = self.class.get(path, options)
25
+ if resp.code == 200
26
+ resp
27
+ else
28
+ fail resp["message"]
29
+ end
25
30
  end
26
31
 
27
32
  def post(path, body = {})
28
33
  body.merge!(app: @app)
29
34
  options = { headers: headers, body: body.to_json }
30
- self.class.post(path, options)
35
+ resp = self.class.post(path, options)
36
+ if resp.code == 200
37
+ resp
38
+ else
39
+ fail resp["message"]
40
+ end
31
41
  end
32
42
 
33
43
  def put(path, body = {})
34
44
  body.merge!(app: @app)
35
45
  options = { headers: headers, body: body.to_json }
36
- self.class.put(path, options)
46
+ resp = self.class.put(path, options)
47
+ if resp.code == 200
48
+ resp
49
+ else
50
+ fail resp["message"]
51
+ end
37
52
  end
38
53
 
39
54
  def delete(path, body = {})
40
55
  body.merge!(app: @app)
41
56
  options = { headers: headers, body: body.to_json }
42
- self.class.delete(path, options)
57
+ resp = self.class.delete(path, options)
58
+ if resp.code == 200
59
+ resp
60
+ else
61
+ fail resp["message"]
62
+ end
43
63
  end
44
64
  end
45
65
  end
@@ -1,3 +1,3 @@
1
1
  module Cyby
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroyuki Sato
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-15 00:00:00.000000000 Z
11
+ date: 2015-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler