Module: Solve360::Item::ClassMethods

Defined in:
lib/solve360/item.rb

Instance Method Summary collapse

Instance Method Details

#construct_record_from_collection(response) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/solve360/item.rb', line 208

def construct_record_from_collection(response)
  response["response"].collect do |item|  
    item = item[1]
    if item.respond_to?(:keys)
      attributes = {}
      attributes[:id] = item["id"]
    
      attributes[:fields] = map_api_fields(item)

      record = new(attributes)
    end
  end.compact
end

#construct_record_from_singular(response) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/solve360/item.rb', line 187

def construct_record_from_singular(response)
  item = response["response"]["item"]
  item.symbolize_keys!
  
  item[:fields] = map_api_fields(item[:fields])

  record = new(item)
  
  if response["response"]["relateditems"]
    related_items = response["response"]["relateditems"]["relatedto"]
  
    if related_items.kind_of?(Array)
      record.related_items.concat(related_items)
    else
      record.related_items = [related_items]
    end
  end
  
  record
end

#create(fields, options = {}) ⇒ Object

Create a record in the API

Parameters:

  • field (Hash)

    > value as configured in Item::fields



144
145
146
147
148
# File 'lib/solve360/item.rb', line 144

def create(fields, options = {})
  new_record = self.new(fields)
  new_record.save
  new_record
end

#field_mappingObject



230
231
232
# File 'lib/solve360/item.rb', line 230

def field_mapping
  @field_mapping
end

#find(id) ⇒ Object

Find records

Parameters:

  • id (Integer, Symbol)

    of the record on the CRM or :all



153
154
155
156
157
158
159
# File 'lib/solve360/item.rb', line 153

def find(id)
  if id == :all
    find_all
  else
    find_one(id)
  end
end

#find_allObject

Find all records



170
171
172
173
# File 'lib/solve360/item.rb', line 170

def find_all
  response = request(:get, "/#{resource_name}/", "<request><layout>1</layout></request>")
  construct_record_from_collection(response)
end

#find_one(id) ⇒ Object

Find a single record

Parameters:

  • id (Integer)

    of the record on the CRM



164
165
166
167
# File 'lib/solve360/item.rb', line 164

def find_one(id)
  response = request(:get, "/#{resource_name}/#{id}")
  construct_record_from_singular(response)
end

#map_api_fields(fields) ⇒ Hash

As ::map_api_fields but API -> human

Examples:

map_attributes(:firstname => "Steve", :custom12345 => "Web Developer")
=> {"First Name" => "Steve", "Description" => "Web Developer"}

Parameters:

  • API (Hash)

    mapped attributes

Returns:

  • (Hash)

    human mapped attributes



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/solve360/item.rb', line 129

def map_api_fields(fields)
  fields.stringify_keys!
  
  mapped_fields = {}

  field_mapping.each do |human, api|
    mapped_fields[human] = fields[api] if !fields[api].blank?
  end
  
  mapped_fields
end

#map_fields(&block) ⇒ Object



226
227
228
# File 'lib/solve360/item.rb', line 226

def map_fields(&block)        
  @field_mapping.merge! yield
end

#map_human_fields(fields) ⇒ Hash

Map human map_human_fields to API fields

Examples:

map_attributes("First Name" => "Steve", "Description" => "Web Developer")
=> {:firstname => "Steve", :custom12345 => "Web Developer"}

Parameters:

  • human (Hash)

    mapped fields

Returns:

  • (Hash)

    API mapped attributes



111
112
113
114
115
116
117
118
119
# File 'lib/solve360/item.rb', line 111

def map_human_fields(fields)
  mapped_fields = {}

  field_mapping.each do |human, api|
    mapped_fields[api] = fields[human] if !fields[human].blank?
  end

  mapped_fields
end

#request(verb, uri, body = "") ⇒ Object

Send an HTTP request

Parameters:

  • :get, (Symbol, String)

    :post, :put or :delete

  • url (String)

    of the resource

  • optional (String, nil)

    string to send in request body



180
181
182
183
184
185
# File 'lib/solve360/item.rb', line 180

def request(verb, uri, body = "")
  send(verb, HTTParty.normalize_base_uri(Solve360::Config.config.url) + uri,
      :headers => {"Content-Type" => "application/xml", "Accepts" => "application/json"},
      :body => body,
      :basic_auth => {:username => Solve360::Config.config.username, :password => Solve360::Config.config.token})
end

#resource_nameObject



222
223
224
# File 'lib/solve360/item.rb', line 222

def resource_name
  self.name.to_s.demodulize.underscore.pluralize
end