Module: SparkApi::Models::Subresource

Included in:
Document, FloPlan, Note, OpenHouse, Photo, RentalCalendar, TourOfHome, Video, VirtualTour
Defined in:
lib/spark_api/models/subresource.rb

Instance Method Summary collapse

Instance Method Details

#build_subclassObject



5
6
7
# File 'lib/spark_api/models/subresource.rb', line 5

def build_subclass
  Class.new(self)
end

#find_by_id(id, parent_id, arguments = {}) ⇒ Object



13
14
15
# File 'lib/spark_api/models/subresource.rb', line 13

def find_by_id(id, parent_id, arguments={})
  collect(connection.get("/listings/#{parent_id}#{self.path}/#{id}", arguments)).first
end

#find_by_listing_key(key, arguments = {}) ⇒ Object



9
10
11
# File 'lib/spark_api/models/subresource.rb', line 9

def find_by_listing_key(key, arguments={})
  collect(connection.get("/listings/#{key}#{self.path}", arguments))
end

#parse_date_start_and_end_times(attributes) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/spark_api/models/subresource.rb', line 17

def parse_date_start_and_end_times(attributes)
  # Transform the date strings
  unless attributes['Date'].nil? || attributes['Date'].empty?
    date = Date.strptime attributes['Date'], '%m/%d/%Y'
    ['StartTime','EndTime'].each do |time|
      next if attributes[time].nil? || attributes[time].empty?
      formatted_date = "#{attributes['Date']}T#{attributes[time]}"
      datetime = nil

      begin
        datetime = DateTime.strptime(formatted_date, '%m/%d/%YT%l:%M %P')
        dst_offset = 0
      rescue => ex
        ; # Do nothing; doesn't matter
      end

      unless datetime
        other_formats = ['%m/%d/%YT%H:%M%z', '%m/%d/%YT%H:%M:%S%z']
        other_formats.each_with_index do |format, i|
          begin
            datetime = DateTime.strptime(formatted_date, format)
            datetime = datetime.new_offset DateTime.now.offset
            now = Time.now
            dst_offset = now.dst? || now.zone == 'UTC' ? 0 : 1
            break
          rescue => ex
            next
          end
        end
      end

      # if we still don't have a valid time, raise an error
      unless datetime
        raise ArgumentError.new('invalid date')
      end
      
      

      attributes[time] = Time.local(datetime.year, datetime.month, datetime.day,
                                    datetime.hour + dst_offset, datetime.min, datetime.sec)
    end
    attributes['Date'] = date
  end
end