Class: NwmlsClient::Listing

Inherits:
Object
  • Object
show all
Defined in:
lib/nwmls_client/listing.rb

Constant Summary collapse

DEFAULT_SCHEMA_NAME =
'StandardXML1_3'
MLS =
'NWMLS'
DEFAULT_PTYP =
'RESI'
AMENITY_FILE =
'amenity.yml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, criteria = {}) ⇒ Listing

Returns a new instance of Listing.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nwmls_client/listing.rb', line 23

def initialize(config, criteria = {})
  raise "Configuration data is required for authentication" if config.nil?
  @user_id = config.fetch("user_id")
  @password = config.fetch("password")
  @mls = config.fetch("mls")
  @schema_name = config.fetch("schema")
  @criteria = criteria
  @criteria[:property_type] ||= :residential
  @criteria[:begin_date] ||= DateTime.now-1
  @criteria[:end_date] ||= DateTime.now
end

Instance Attribute Details

#criteriaObject

Returns the value of attribute criteria.



18
19
20
# File 'lib/nwmls_client/listing.rb', line 18

def criteria
  @criteria
end

#mlsObject

Returns the value of attribute mls.



17
18
19
# File 'lib/nwmls_client/listing.rb', line 17

def mls
  @mls
end

#passwordObject

Returns the value of attribute password.



21
22
23
# File 'lib/nwmls_client/listing.rb', line 21

def password
  @password
end

#schema_nameObject

Returns the value of attribute schema_name.



19
20
21
# File 'lib/nwmls_client/listing.rb', line 19

def schema_name
  @schema_name
end

#user_idObject

Returns the value of attribute user_id.



20
21
22
# File 'lib/nwmls_client/listing.rb', line 20

def user_id
  @user_id
end

Instance Method Details

#display(listing_data) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/nwmls_client/listing.rb', line 59

def display(listing_data)

  raise "No listing data to display" if listing_data.nil?

  amenity_file = File.join(File.dirname(__FILE__), AMENITY_FILE)
  if not File.exist?(amenity_file)
    puts "Required file not found: #{amenity_file}"
    return
  end

  begin
    amenity = YAML.load_file(amenity_file)
  rescue Exception
    puts "Failed to load file (#{AMENITY_FILE}): #{$!}"
    return
  end

  # TODO: Is there a way to load the listing data and replace both 
  # Codes and Amenities with the friendly names using regular expressions?

  # Check if there is a list of items to display
  # or only one
  if listing_data.is_a?(Array)
    listing_data.each do |item|
      item.each do |key, value|
        items = []
        code = key.to_s.upcase
        friendly_name = NwmlsClient::Codes.expand_code(code)

        if amenity[code] && value
          amenities = value.split('|')
          amenities.each do |am|
            items << "#{amenity[code][am]}"
          end
          puts "#{friendly_name}: #{items.join(", ")}"
        else
          puts "#{friendly_name}: #{value}"
        end

      end
      puts "\n"
    end
  else
    listing_data.each do |key, value|

      if value != nil
        items = []
        code = key.to_s.upcase
        friendly_name = NwmlsClient::Codes.expand_code(code)

        if amenity[code]
          amenities = value.split('|')
          amenities.each do |am|
            items << "#{amenity[code][am]}"
          end
          puts "#{friendly_name}: #{items.join(', ')}"
        else
          puts "#{friendly_name}: #{value}"
        end

      end

    end
  end
end

#retrieve_dataObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nwmls_client/listing.rb', line 35

def retrieve_data()

  operation = ( @criteria[:operation] || :retrieve_listing_data )
  data_response = ( @criteria[:data_response] || :retrieve_listing_data_response )
  data_result = ( @criteria[:data_result] || :retrieve_listing_data_result )
  data_types = ( @criteria[:data_types] || :listings )
  data_type = ( @criteria[:data_type] || @criteria[:property_type] )


  request = { 'v_strXmlQuery' => build_request()}

  @client = create_client()
  response = @client.call(operation, message: request)

  cdata = response.body[data_response][data_result] 
  nori_options = { :convert_tags_to => lambda { |tag| tag.snakecase.to_sym } }
  result = Nori.new(nori_options).parse(cdata)

  raise "No listing data found:\n#{result}" if result[data_types][data_type].nil?

  listing_data = result[data_types][data_type]

end