Module: Solve360::Item::ClassMethods
- Defined in:
- lib/solve360/item.rb
Instance Method Summary collapse
- #construct_record_from_collection(response) ⇒ Object
- #construct_record_from_singular(response) ⇒ Object
-
#create(fields, options = {}) ⇒ Object
Create a record in the API.
- #field_mapping ⇒ Object
-
#find(id) ⇒ Object
Find records.
-
#find_all ⇒ Object
Find all records.
-
#find_one(id) ⇒ Object
Find a single record.
-
#map_api_fields(fields) ⇒ Hash
As ::map_api_fields but API -> human.
- #map_fields(&block) ⇒ Object
-
#map_human_fields(fields) ⇒ Hash
Map human map_human_fields to API fields.
-
#request(verb, uri, body = "") ⇒ Object
Send an HTTP request.
- #resource_name ⇒ Object
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"] = response["response"]["relateditems"]["relatedto"] if .kind_of?(Array) record..concat() else record. = [] end end record end |
#create(fields, options = {}) ⇒ Object
Create a record in the API
144 145 146 147 148 |
# File 'lib/solve360/item.rb', line 144 def create(fields, = {}) new_record = self.new(fields) new_record.save new_record end |
#field_mapping ⇒ Object
230 231 232 |
# File 'lib/solve360/item.rb', line 230 def field_mapping @field_mapping end |
#find(id) ⇒ Object
Find records
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_all ⇒ Object
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
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
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
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
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_name ⇒ Object
222 223 224 |
# File 'lib/solve360/item.rb', line 222 def resource_name self.name.to_s.demodulize.underscore.pluralize end |