Class: Cyby::Kintone::App

Inherits:
Object
  • Object
show all
Defined in:
lib/cyby/kintone/app.rb

Constant Summary collapse

LIMIT =
100

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ App

Returns a new instance of App.



6
7
8
# File 'lib/cyby/kintone/app.rb', line 6

def initialize(id)
  @api = RestApi.new(id)
end

Instance Method Details

#allObject



65
66
67
# File 'lib/cyby/kintone/app.rb', line 65

def all
  relation.all
end

#asc(field) ⇒ Object



73
74
75
# File 'lib/cyby/kintone/app.rb', line 73

def asc(field)
  relation.asc(field)
end

#delete(record) ⇒ Object



51
52
53
54
55
# File 'lib/cyby/kintone/app.rb', line 51

def delete(record)
  json = { ids: [record["$id"]] }
  @api.delete("/records.json", json)
  true
end

#desc(field) ⇒ Object



77
78
79
# File 'lib/cyby/kintone/app.rb', line 77

def desc(field)
  relation.desc(field)
end

#find(params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/cyby/kintone/app.rb', line 10

def find(params)
  result = []
  page = 0
  begin
    records = find_per_page(params, page)
    result.concat(records)
    page += 1
  end while records.count == LIMIT
  result
end

#find_per_page(params, page) ⇒ Object



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

def find_per_page(params, page)
  params_per_page = params.dup
  queries = [params[:query]]
  if page > 0
    queries << "offset #{LIMIT * page}"
  end
  params_per_page[:query] = queries.join(" ")
  response = @api.get('/records.json', params_per_page)
  response['records'].map do |record|
    Record.new(self, record)
  end
end

#idObject



85
86
87
# File 'lib/cyby/kintone/app.rb', line 85

def id
  @api.app
end

#inspectObject



89
90
91
# File 'lib/cyby/kintone/app.rb', line 89

def inspect
  { id: id }.inspect
end

#new_recordObject



57
58
59
# File 'lib/cyby/kintone/app.rb', line 57

def new_record
  Record.new(self)
end

#relationObject



61
62
63
# File 'lib/cyby/kintone/app.rb', line 61

def relation
  Relation.new(self)
end

#save(record) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cyby/kintone/app.rb', line 34

def save(record)
  if record.changed?
    json = record.to_json_for_save
    if json[:id]
      resp = @api.put("/record.json", json)
    else
      resp = @api.post("/record.json", json)
      record["$id"] = resp["id"]
    end
    record["$revision"] = resp["revision"]
    record.unchanged
    true
  else
    false
  end
end

#select(*fields) ⇒ Object



81
82
83
# File 'lib/cyby/kintone/app.rb', line 81

def select(*fields)
  relation.select(*fields)
end

#where(cond, *params) ⇒ Object



69
70
71
# File 'lib/cyby/kintone/app.rb', line 69

def where(cond, *params)
  relation.where(cond, *params)
end