Class: Caboose::BlockTypeSource

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/block_type_source.rb

Instance Method Summary collapse

Instance Method Details

#refresh(name, force = false) ⇒ Object

Get the full block type (including children)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/caboose/block_type_source.rb', line 43

def refresh(name, force = false)
  bt = Caboose::BlockType.where(:name => name).first
  bt = Caboose::BlockType.create(:name => name) if bt.nil?
  return if bt.downloaded && !force    
  if force
    bt.children.each { |bt2| bt2.destroy }
  end

  # Try to contact the source URL
  resp = nil
  begin                             
    resp = HTTParty.get("#{self.url}/caboose/block-types/#{bt.name}?token=#{self.token}")
  rescue HTTParty::Error => e
    Caboose.log(e.message)
    return false
  end
              
  # Try to parse the response
  h = nil
  begin
    h = JSON.parse(resp.body)
  rescue
    Caboose.log("Response body isn't valid JSON.")
    return false
  end     
  #Caboose.log(h)
  # Update the block type
  bt.parse_api_hash(h)
  
  return true
end

#refresh_summariesObject

Just get the names and descriptions of all block types from the source



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/caboose/block_type_source.rb', line 15

def refresh_summaries
  resp = nil
  begin
    resp = HTTParty.get("#{self.url}/caboose/block-types?token=#{self.token}")
  rescue HTTParty::Error => e
    Caboose.log(e.message)
    return false
  end
  
  summaries = nil
  begin
    summaries = JSON.parse(resp.body)
  rescue
    Caboose.log("Response body isn't valid JSON.")
    return false
  end
  
  summaries.each do |h|      
    s = Caboose::BlockTypeSummary.where(:block_type_source_id => self.id, :name => h['name']).first
    s = Caboose::BlockTypeSummary.create(:block_type_source_id => self.id) if s.nil?
    s.parse_api_hash(h)
    s.save
  end
  
  return true
end