Class: Insightly::Base
- Inherits:
-
Object
- Object
- Insightly::Base
- Defined in:
- lib/insightly/base.rb
Class Attribute Summary collapse
-
.api_fields ⇒ Object
Returns the value of attribute api_fields.
-
.url_base ⇒ Object
Returns the value of attribute url_base.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #build(data) ⇒ Object
- #config ⇒ Object
- #get_collection(path, content_selector = :json) ⇒ Object
-
#initialize(id = nil) ⇒ Base
constructor
A new instance of Base.
- #load(id) ⇒ Object
- #process(result, content_type) ⇒ Object
- #reload ⇒ Object
- #remote_data ⇒ Object
- #remote_id ⇒ Object
- #to_json ⇒ Object
- #url_base ⇒ Object
Constructor Details
#initialize(id = nil) ⇒ Base
Returns a new instance of Base.
49 50 51 52 |
# File 'lib/insightly/base.rb', line 49 def initialize(id = nil) @data = {} load(id) if id end |
Class Attribute Details
.api_fields ⇒ Object
Returns the value of attribute api_fields.
14 15 16 |
# File 'lib/insightly/base.rb', line 14 def api_fields @api_fields end |
.url_base ⇒ Object
Returns the value of attribute url_base.
14 15 16 |
# File 'lib/insightly/base.rb', line 14 def url_base @url_base end |
Class Method Details
.all ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/insightly/base.rb', line 118 def self.all item = self.new list = [] item.get_collection(item.url_base).each do |d| list << self.new.build(d) end list end |
.api_field(*args) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/insightly/base.rb', line 34 def self.api_field(*args) args.each do |field| self.api_fields = [] if !self.api_fields self.api_fields << field method_name = field.downcase.to_sym send :define_method, method_name do @data[field] end method_name = "#{field.downcase}=".to_sym send :define_method, method_name do |value| @data[field] = value end end end |
.build(data) ⇒ Object
78 79 80 |
# File 'lib/insightly/base.rb', line 78 def self.build(data) self.new.build(data) end |
.custom_fields(*args) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/insightly/base.rb', line 19 def self.custom_fields(*args) args.each_with_index do |method, index| next if method.nil? or method == "" method_name = method.to_s.downcase.to_sym send :define_method, method_name do @data["#{self.class.const_get(:CUSTOM_FIELD_PREFIX)}_#{index+1}"] end method_name = "#{method.to_s.downcase}=".to_sym send :define_method, method_name do |value| @data["#{self.class.const_get(:CUSTOM_FIELD_PREFIX)}_#{index+1}"] = value end end end |
Instance Method Details
#==(other) ⇒ Object
82 83 84 |
# File 'lib/insightly/base.rb', line 82 def ==(other) self.remote_data == other.remote_data end |
#build(data) ⇒ Object
73 74 75 76 |
# File 'lib/insightly/base.rb', line 73 def build(data) @data = data self end |
#config ⇒ Object
100 101 102 |
# File 'lib/insightly/base.rb', line 100 def config Insightly::Configuration.instantiate end |
#get_collection(path, content_selector = :json) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/insightly/base.rb', line 104 def get_collection(path, content_selector = :json) if content_selector == :xml_raw content_type = :xml else content_type = content_selector end response = RestClient::Request.new(:method => :get, :url => "#{config.endpoint}/#{path.to_s}", :user => config.api_key, :password => "", :headers => {:accept => content_type, :content_type => content_type}).execute process(response, content_selector) end |
#load(id) ⇒ Object
62 63 64 65 |
# File 'lib/insightly/base.rb', line 62 def load(id) @data = get_collection("#{url_base}/#{id}") self end |
#process(result, content_type) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/insightly/base.rb', line 90 def process(result, content_type) if content_type == :json JSON.parse(result.to_str) elsif content_type == :xml Hash.from_xml(result.to_str) else result end end |
#reload ⇒ Object
67 68 69 |
# File 'lib/insightly/base.rb', line 67 def reload load(remote_id) end |
#remote_data ⇒ Object
86 87 88 |
# File 'lib/insightly/base.rb', line 86 def remote_data @data end |
#remote_id ⇒ Object
58 59 60 |
# File 'lib/insightly/base.rb', line 58 def remote_id raise ScriptError, "This should be overridden in the subclass" end |
#to_json ⇒ Object
70 71 72 |
# File 'lib/insightly/base.rb', line 70 def to_json @data.to_json end |
#url_base ⇒ Object
55 56 57 |
# File 'lib/insightly/base.rb', line 55 def url_base self.class.url_base end |