Sha256: ad446824323385a58888e14a31e3ab858949acdf3cde2c0b97cb50c6a6bd28a2
Contents?: true
Size: 1.92 KB
Versions: 6
Compression:
Stored size: 1.92 KB
Contents
require 'httparty' class Caboose::BlockTypeSource < ActiveRecord::Base self.table_name = "block_type_sources" has_many :block_types, :class_name => 'Caboose::BlockType', :order => 'name' attr_accessible :id, :name, :url, :token, :priority, :active # Just get the names and descriptions of all block types from the source 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 # Get the full block type (including children) 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 end
Version data entries
6 entries across 6 versions & 1 rubygems