Class: Cyby::Kintone::App

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

Constant Summary collapse

LIMIT =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, convert_to_camelized_field = false) ⇒ App

Returns a new instance of App.



8
9
10
11
# File 'lib/cyby/kintone/app.rb', line 8

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

Instance Attribute Details

#convert_to_camelized_fieldObject

Returns the value of attribute convert_to_camelized_field.



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

def convert_to_camelized_field
  @convert_to_camelized_field
end

Instance Method Details

#allObject



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

def all
  relation.all
end

#asc(field) ⇒ Object



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

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

#delete(record) ⇒ Object



54
55
56
57
58
# File 'lib/cyby/kintone/app.rb', line 54

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

#desc(field) ⇒ Object



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

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

#find(params) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/cyby/kintone/app.rb', line 13

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



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cyby/kintone/app.rb', line 24

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



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

def id
  @api.app
end

#inspectObject



92
93
94
# File 'lib/cyby/kintone/app.rb', line 92

def inspect
  { id: id }.inspect
end

#new_recordObject



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

def new_record
  Record.new(self)
end

#relationObject



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

def relation
  Relation.new(self)
end

#save(record) ⇒ Object



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

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



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

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

#where(cond, *params) ⇒ Object



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

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