Class: Dotloop::Loop

Inherits:
Object
  • Object
show all
Includes:
QueryParamHelpers
Defined in:
lib/dotloop/loop.rb

Constant Summary collapse

LOOP_FIELDS =
%w[name status transactionType].freeze
LOOP_DETAILS_FIELDS =
{
  "Property Address" => [
    "Country", "Street Number", "Street Name", "Unit Number", "City",
    "State/Prov", "Zip/Postal Code", "County", "MLS Number", "Parcel/Tax ID"
  ],
  "Financials" => [
    "Purchase/Sale Price", "Sale Commission Rate",
    "Sale Commission Split % - Buy Side", "Sale Commission Split % - Sell Side",
    "Sale Commission Total", "Earnest Money Amount", "Earnest Money Held By",
    "Sale Commission Split $ - Buy Side", "Sale Commission Split $ - Sell Side"
  ],
  "Contract Dates" => [
    "Contract Agreement Date", "Closing Date"
  ],
  "Offer Dates" => [
    "Inspection Date", "Offer Date", "Offer Expiration Date", "Occupancy Date"
  ],
  "Contract Info" => [
    "Transaction Number", "Class", "Type"
  ],
  "Referral" => [
    "Referral %", "Referral Source"
  ],
  "Listing Information" => [
    "Expiration Date", "Listing Date", "Original Price",
    "Current Price", "1st Mortgage Balance", "2nd Mortgage Balance",
    "Other Liens", "Description of Other Liens", "Homeowner's Association",
    "Homeowner's Association Dues", "Total Encumbrances", "Property Includes",
    "Property Excludes", "Remarks"
  ],
  "Geographic Description" => [
    "MLS Area", "Legal Description", "Map Grid", "Subdivision", "Lot",
    "Deed Page", "Deed Book", "Section", "Addition", "Block"
  ],
  "Property" => [
    "Year Built", "Bedrooms", "Square Footage", "School District",
    "Type", "Bathrooms", "Lot Size"
  ]
}.freeze

Constants included from QueryParamHelpers

QueryParamHelpers::BATCH_SIZE, QueryParamHelpers::MAX_CONTACTS, QueryParamHelpers::MAX_LOOPS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Loop

Returns a new instance of Loop.



50
51
52
# File 'lib/dotloop/loop.rb', line 50

def initialize(client:)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/dotloop/loop.rb', line 6

def client
  @client
end

Instance Method Details

#all(options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dotloop/loop.rb', line 54

def all(options = {})
  raise_if_invalid_batch_size(options)
  options[:batch_size] ||= BATCH_SIZE
  Array.new.tap do |arr|
    (1..MAX_LOOPS).each do |i|
      options[:batch_number] = i
      current_batch = batch(options)
      arr.push current_batch
      break if current_batch.size < options[:batch_size]
    end
  end.flatten
end

#batch(options = {}) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/dotloop/loop.rb', line 67

def batch(options = {})
  @client.get("/profile/#{profile_id(options)}/loop", query_params(options))[:data].map do |attrs|
    lp = Dotloop::Models::Loop.new(attrs)
    lp.client = client
    lp.profile_id = profile_id(options)
    lp
  end
end

#create(profile_id:, params: {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dotloop/loop.rb', line 93

def create(profile_id:, params: {})
  data = {
    name: params['name'],
    status: params['status'],
    transactionType: params['transactionType']
  }

  loop_data = @client.post("/profile/#{profile_id.to_i}/loop", data)[:data]
  lp = Dotloop::Models::Loop.new(loop_data)
  lp.client = client
  lp.profile_id = profile_id.to_i
  lp
end

#detail(profile_id:, loop_id:) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/dotloop/loop.rb', line 84

def detail(profile_id:, loop_id:)
  loop_detail = @client.get("/profile/#{profile_id.to_i}/loop/#{loop_id.to_i}/detail")[:data]
  loop_detail = Dotloop::LoopDetail.new(loop_detail).details
  ld = Dotloop::Models::LoopDetail.new(loop_detail)
  ld.profile_id = profile_id.to_i
  ld.loop_id = loop_id.to_i
  ld
end

#find(profile_id:, loop_id:) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/dotloop/loop.rb', line 76

def find(profile_id:, loop_id:)
  loop_data = @client.get("/profile/#{profile_id.to_i}/loop/#{loop_id.to_i}")[:data]
  lp = Dotloop::Models::Loop.new(loop_data)
  lp.client = client
  lp.profile_id = profile_id.to_i
  lp
end

#loop_it(profile_id:, params: {}) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/dotloop/loop.rb', line 107

def loop_it(profile_id:, params: {})
  loop_data = @client.post("/loop-it?profile_id=#{profile_id}", params)[:data]
  lp = Dotloop::Models::Loop.new(loop_data)
  lp.client = client
  lp.profile_id = profile_id.to_i
  lp
end

#update(profile_id:, loop_id:, params: {}) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/dotloop/loop.rb', line 115

def update(profile_id:, loop_id:, params: {})
  data = {}
  params.each do |key, value|
    LOOP_FIELDS.include?(key.to_s) || next
    data[key] = value.to_s
  end

  loop_data = @client.patch("/profile/#{profile_id.to_i}/loop/#{loop_id.to_i}", data)[:data]
  lp = Dotloop::Models::Loop.new(loop_data)
  lp.client = client
  lp.profile_id = profile_id.to_i
  lp
end

#update_details(profile_id:, loop_id:, params: {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dotloop/loop.rb', line 129

def update_details(profile_id:, loop_id:, params: {})
  data = {}
  LOOP_DETAILS_FIELDS.keys.each do |key|
    (detail_fields = params[key])
    detail_fields.is_a?(Hash) || next
    data[key] = {}
    detail_fields.each do |k, v|
      LOOP_DETAILS_FIELDS[key].include?(k.to_s) || next
      data[key][k] = v.to_s
    end
  end

  loop_detail = @client.patch("/profile/#{profile_id.to_i}/loop/#{loop_id.to_i}/detail", data)[:data]
  loop_detail = Dotloop::LoopDetail.new(loop_detail).details
  Dotloop::Models::LoopDetail.new(loop_detail)
end