lib/mls.rb in mls-0.11.3 vs lib/mls.rb in mls-0.12.1
- old
+ new
@@ -23,27 +23,27 @@
API_VERSION = '0.1.0'
attr_reader :url, :user_agent
attr_writer :asset_host, :image_host, :agent_profile
- attr_accessor :api_key, :auth_key, :logger
+ attr_accessor :api_key, :auth_cookie, :logger
# Sets the API Token and Host of the MLS Server
#
# #!ruby
# MLS.url = "https://mls.42floors.com/API_KEY"
def url=(uri) # TODO: testme
@url = URI.parse(uri)
@api_key = CGI.unescape(@url.user)
@host, @port = @url.host, @url.port
end
-
+
# Sets the user agent so that MLS can distinguish between multiple users
# with the same auth
def user_agent=(user_agent)
@user_agent = user_agent
- end
+ end
def logger # TODO: testme
@logger ||= default_logger
end
@@ -56,48 +56,47 @@
# provides the asset host, if asset_host is set then it is returned,
# otherwise it queries the MLS for this configuration.
def asset_host # TODO: testme
@asset_host ||= get('/asset_host').body
end
-
+
def image_host # TODO: testme
raw_image_host % (rand(4))
end
-
+
def raw_image_host
@image_host ||= get('/image_host').body
end
def agent_profile(id)
@agent_profile = Yajl::Parser.new(:symbolize_keys => true)
.parse(MLS.get("/agents/#{id}").body)
end
def headers # TODO: testme
- h = {
+ {
'Content-Type' => 'application/json',
'User-Agent' => @user_agent,
'X-42Floors-API-Version' => API_VERSION,
'X-42Floors-API-Key' => api_key
}
- h['X-42Floors-API-Auth-Key'] = auth_key if auth_key
- h
end
-
- def add_headers(req) # TODO: testme
+
+ def prepare_request(req) # TODO: testme
headers.each { |k, v| req[k] = v }
+ req['Cookie'] = auth_cookie if auth_cookie
end
-
+
# Gets to +url+ on the MLS Server. Automatically includes any headers returned
# by the MLS#headers function.
#
# Paramaters::
#
# * +url+ - The +url+ on the server to Get to. This url will automatically
# be prefixed with <tt>"/api"</tt>. To get to <tt>"/api/accounts"</tt>
# pass <tt>"/accounts"</tt> as +url+
- # * +params+ - A Hash or Ruby Object that responds to #to_param. The result
+ # * +params+ - A Hash or Ruby Object that responds to #to_param. The result
# of this method is appended on the URL as query params
# * +valid_response_codes+ - An Array of HTTP response codes that should be
# considered accepable and not raise exceptions. For example If you don't
# want a MLS::Exception::NotFound to be raised when a GET request returns
# a 404 pass in 404, and the response body will be returned if the status
@@ -129,25 +128,25 @@
# MLS.get('/act') do |response, response_code|
# # ...
# end
def get(url, params={}, *valid_response_codes, &block)
params ||= {}
-
+
req = Net::HTTP::Get.new("/api#{url}?" + params.to_param)
- add_headers(req)
+ prepare_request(req)
response = connection.request(req)
handle_response(response, valid_response_codes)
-
+
response.body.force_encoding(Encoding::UTF_8)
if block_given?
yield(response, response.code.to_i)
else
response
end
end
-
+
# Puts to +url+ on the MLS Server. Automatically includes any headers returned
# by the MLS#headers function.
#
# Paramaters::
#
@@ -188,25 +187,25 @@
# MLS.put('/act') do |response, response_code|
# # ...
# end
def put(url, body={}, *valid_response_codes, &block)
body ||= {}
-
+
req = Net::HTTP::Put.new("/api#{url}")
req.body = Yajl::Encoder.encode(body)
- add_headers(req)
-
+ prepare_request(req)
+
response = connection.request(req)
handle_response(response, valid_response_codes)
if block_given?
yield(response, response.code.to_i)
else
response
end
- end
-
+ end
+
# Posts to +url+ on the MLS Server. Automatically includes any headers returned
# by the MLS#headers function.
#
# Paramaters::
#
@@ -247,15 +246,15 @@
# MLS.post('/act') do |response, response_code|
# # ...
# end
def post(url, body={}, *valid_response_codes, &block)
body ||= {}
-
+
req = Net::HTTP::Post.new("/api#{url}")
req.body = Yajl::Encoder.encode(body)
- add_headers(req)
-
+ prepare_request(req)
+
response = connection.request(req)
handle_response(response, valid_response_codes)
if block_given?
yield(response, response.code.to_i)
@@ -306,15 +305,15 @@
# MLS.delete('/act') do |response, response_code|
# # ...
# end
def delete(url, body={}, *valid_response_codes, &block)
body ||= {}
-
+
req = Net::HTTP::Delete.new("/api#{url}")
req.body = Yajl::Encoder.encode(body)
- add_headers(req)
-
+ prepare_request(req)
+
response = connection.request(req)
handle_response(response, valid_response_codes)
if block_given?
yield(response, response.code.to_i)
else
@@ -337,31 +336,31 @@
#
# Examples:
#
# #!ruby
# MLS.handle_response(<Net::HTTP::Response @code=200>) # => <Net::HTTP::Response @code=200>
- #
+ #
# MLS.handle_response(<Net::HTTP::Response @code=404>) # => raises MLS::Exception::NotFound
- #
+ #
# MLS.handle_response(<Net::HTTP::Response @code=500>) # => raises MLS::Exception
- #
+ #
# MLS.handle_response(<Net::HTTP::Response @code=404>, 404) # => <Net::HTTP::Response @code=404>
- #
+ #
# MLS.handle_response(<Net::HTTP::Response @code=500>, 404, 500) # => <Net::HTTP::Response @code=500>
#
# MLS.handle_response(<Net::HTTP::Response @code=405>, 300, 400..499) # => <Net::HTTP::Response @code=405>
#
# MLS.handle_response(<Net::HTTP::Response @code=405>, [300, 400..499]) # => <Net::HTTP::Response @code=405>
def handle_response(response, *valid_response_codes)
if response['X-42Floors-API-Version-Deprecated']
logger.warn("DEPRECATION WARNING: API v#{API_VERSION} is being phased out")
end
-
+
code = response.code.to_i
valid_response_codes.flatten!
valid_response_codes << (200..299)
-
+
if !valid_response_codes.detect{|i| i.is_a?(Range) ? i.include?(code) : i == code}
case code
when 400
raise MLS::Exception::BadRequest, response.body
when 401
@@ -374,20 +373,20 @@
raise MLS::Exception::ServiceUnavailable, response.body
when 300..599
raise MLS::Exception, code
end
end
-
+
response
end
-
+
# Ping the MLS. If everything is configured and operating correctly <tt>"pong"</tt>
# will be returned. Otherwise and MLS::Exception should be thrown.
#
# #!ruby
# MLS.ping # => "pong"
- #
+ #
# MLS.ping # raises MLS::Exception::ServiceUnavailable if a 503 is returned
def ping # TODO: testme
get('/ping').body
end
@@ -403,35 +402,36 @@
# Delegates all uncauge class method calls to the singleton
def self.method_missing(method, *args, &block) #:nodoc: # TODO: testme
instance.__send__(method, *args, &block)
end
-
+
def self.parse(json) # TODO: testme
Yajl::Parser.new(:symbolize_keys => true).parse(json)
end
end
require 'mls/errors'
require 'mls/resource'
require 'mls/parser'
-# Properties
-require 'mls/property'
-require 'mls/properties/fixnum'
-require 'mls/properties/boolean'
-require 'mls/properties/decimal'
-require 'mls/properties/datetime'
-require 'mls/properties/string'
-require 'mls/properties/hash'
-require 'mls/properties/array'
+# Attributes
+require 'mls/attribute'
+require 'mls/attributes/fixnum'
+require 'mls/attributes/boolean'
+require 'mls/attributes/decimal'
+require 'mls/attributes/datetime'
+require 'mls/attributes/string'
+require 'mls/attributes/hash'
+require 'mls/attributes/array'
# Models
require 'mls/model'
require 'mls/models/account'
require 'mls/models/listing'
require 'mls/models/address'
+require 'mls/models/property'
require 'mls/models/photo'
require 'mls/models/video'
require 'mls/models/pdf'
require 'mls/models/tour'
require 'mls/models/flyer'