Class: Rbdash::Models::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/rbdash/models/base_model.rb

Direct Known Subclasses

Query

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, id) ⇒ BaseModel



8
9
10
11
# File 'lib/rbdash/models/base_model.rb', line 8

def initialize(body, id)
  @body = body
  @id = id
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



6
7
8
# File 'lib/rbdash/models/base_model.rb', line 6

def body
  @body
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/rbdash/models/base_model.rb', line 6

def id
  @id
end

Class Method Details

.clientObject



28
29
30
# File 'lib/rbdash/models/base_model.rb', line 28

def client
  Rbdash::Request.new
end

.find(id) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/rbdash/models/base_model.rb', line 32

def find(id)
  response = client.get("#{endpoint}/#{id}")
  if response.code != 200
    puts response.code
    raise StandardError, 'abort!'
  end
  h = JSON.parse(response.body)
  body = h.select { |k, _| attributes.map(&:to_s).include?(k) }
  new(body, id)
end

.find_allObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rbdash/models/base_model.rb', line 43

def find_all
  all_results = []
  (1..100).each do |current_page|
    response = client.get(endpoint.to_s, params: { page: current_page })
    if response.code != 200
      puts response.code
      raise StandardError, 'abort!'
    end
    h = JSON.parse(response.body)
    results = h['results']
    all_results += results.map do |result|
      body = result.select { |k, _| attributes.map(&:to_s).include?(k) }
      id = result['id']
      new(body, id)
    end

    count = h['count']
    page = h['page']
    page_size = h['page_size']
    break if count <= page * page_size
  end
  all_results
end

.load(id) ⇒ Object



79
80
81
82
83
84
# File 'lib/rbdash/models/base_model.rb', line 79

def load(id)
  file = Utils.find_local_file(id)
  return new(nil, id) unless file
  body = JSON.parse(File.read(file))
  new(body, id)
end

.update(id) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rbdash/models/base_model.rb', line 67

def update(id)
  request_body = load(id).body
  response = client.post("#{endpoint}/#{id}", body: request_body)
  if response.code != 200
    puts response.code
    puts response.headers
    puts response.body
    raise StandardError, 'abort!'
  end
  response
end

Instance Method Details

#saveObject



17
18
19
20
21
22
23
24
25
# File 'lib/rbdash/models/base_model.rb', line 17

def save
  dirname = self.class.dirname
  Dir.mkdir(dirname) unless File.exist?(dirname)
  filename = "#{dirname}/query-#{id}.json"
  File.open(filename, 'w+') do |f|
    f.puts(to_json)
  end
  true
end

#to_jsonObject



13
14
15
# File 'lib/rbdash/models/base_model.rb', line 13

def to_json
  JSON.pretty_generate(body)
end