Class: Insightly::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/insightly/base.rb

Direct Known Subclasses

ReadOnly, ReadWrite

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil) ⇒ Base

Returns a new instance of Base.



55
56
57
58
# File 'lib/insightly/base.rb', line 55

def initialize(id = nil)
  @data = {}
  load(id) if id
end

Class Attribute Details

.api_fieldsObject

Returns the value of attribute api_fields.



17
18
19
# File 'lib/insightly/base.rb', line 17

def api_fields
  @api_fields
end

.remote_id_fieldObject

Returns the value of attribute remote_id_field.



17
18
19
# File 'lib/insightly/base.rb', line 17

def remote_id_field
  @remote_id_field
end

.url_baseObject

Returns the value of attribute url_base.



17
18
19
# File 'lib/insightly/base.rb', line 17

def url_base
  @url_base
end

Class Method Details

.allObject



140
141
142
143
144
145
146
147
# File 'lib/insightly/base.rb', line 140

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/insightly/base.rb', line 37

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



100
101
102
# File 'lib/insightly/base.rb', line 100

def self.build(data)
  self.new.build(data)
end

.custom_fields(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/insightly/base.rb', line 22

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

.date_to_insightly(date = Date.today) ⇒ Object



52
53
54
# File 'lib/insightly/base.rb', line 52

def self.date_to_insightly(date = Date.today)
  date.strftime("%Y-%m-%d %H:%M:%S")
end

Instance Method Details

#==(other) ⇒ Object



104
105
106
# File 'lib/insightly/base.rb', line 104

def ==(other)
  self.remote_data == other.remote_data
end

#build(data) ⇒ Object



95
96
97
98
# File 'lib/insightly/base.rb', line 95

def build(data)
  @data = data
  self
end

#configObject



122
123
124
# File 'lib/insightly/base.rb', line 122

def config
  Insightly::Configuration.instantiate
end

#get_collection(path, content_selector = :json) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/insightly/base.rb', line 126

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



82
83
84
85
# File 'lib/insightly/base.rb', line 82

def load(id)
  @data = get_collection("#{url_base}/#{id}")
  self
end

#process(result, content_type) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/insightly/base.rb', line 112

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

#reloadObject



87
88
89
# File 'lib/insightly/base.rb', line 87

def reload
  load(remote_id)
end

#remote_dataObject



108
109
110
# File 'lib/insightly/base.rb', line 108

def remote_data
  @data
end

#remote_idObject



70
71
72
# File 'lib/insightly/base.rb', line 70

def remote_id
  self.send(remote_id_field.to_sym)
end

#remote_id=(value) ⇒ Object



74
75
76
# File 'lib/insightly/base.rb', line 74

def remote_id=(value)
  self.send("#{remote_id_field}=", value)
end

#remote_id?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/insightly/base.rb', line 78

def remote_id?
  self.remote_id.nil? || self.remote_id.to_s.empty? ? false : true
end

#remote_id_fieldObject



65
66
67
68
# File 'lib/insightly/base.rb', line 65

def remote_id_field
  return self.class.remote_id_field if self.class.remote_id_field
  self.class.to_s.downcase.gsub("insightly::", "") + "_id"
end

#to_jsonObject



91
92
93
# File 'lib/insightly/base.rb', line 91

def to_json
  @data.to_json
end

#url_baseObject



61
62
63
# File 'lib/insightly/base.rb', line 61

def url_base
  self.class.url_base
end