lib/mls.rb in mls-0.13.0 vs lib/mls.rb in mls-0.14.0

- old
+ new

@@ -4,10 +4,11 @@ require 'net/https' require 'singleton' require 'yajl' require 'bigdecimal' require 'bigdecimal/util' +require 'active_support' require 'active_support/core_ext' require 'date' require 'time' class Decimal #:nodoc: @@ -23,11 +24,11 @@ API_VERSION = '0.1.0' attr_reader :url, :user_agent attr_writer :asset_host, :image_host, :agent_profile - attr_accessor :api_key, :auth_cookie, :logger + attr_accessor :api_key, :cookie_jar, :logger, :branch, :server, :forwarded_for # Sets the API Token and Host of the MLS Server # # #!ruby # MLS.url = "https://mls.42floors.com/API_KEY" @@ -75,30 +76,33 @@ @agent_profile = Yajl::Parser.new(:symbolize_keys => true) .parse(MLS.get("/agents/#{id}").body) end def headers # TODO: testme - { + headers = { 'Content-Type' => 'application/json', 'User-Agent' => @user_agent, 'X-42Floors-API-Version' => API_VERSION, 'X-42Floors-API-Key' => api_key } + headers['X-42Floors-Branch'] = branch if branch + headers['X-42Floors-Server'] = server if server + headers['X-Forwarded-For'] = forwarded_for if forwarded_for + headers end def prepare_request(req) # TODO: testme headers.each { |k, v| req[k] = v } - req['Cookie'] = auth_cookie if auth_cookie + req['Cookie'] = cookie_jar[:api_session] if cookie_jar[:api_session] 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> + # * +url+ - The +url+ on the server to Get to. To get to <tt>"/accounts"</tt> # pass <tt>"/accounts"</tt> as +url+ # * +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 @@ -133,11 +137,11 @@ # # ... # end def get(url, params={}, *valid_response_codes, &block) params ||= {} - req = Net::HTTP::Get.new("/api#{url}?" + params.to_param) + req = Net::HTTP::Get.new(url + '?' + params.to_param) prepare_request(req) response = connection.request(req) handle_response(response, valid_response_codes) @@ -152,12 +156,11 @@ # Puts to +url+ on the MLS Server. Automatically includes any headers returned # by the MLS#headers function. # # Paramaters:: # - # * +url+ - The +url+ on the server to Put to. This url will automatically - # be prefixed with <tt>"/api"</tt>. To put to <tt>"/api/accounts"</tt> + # * +url+ - The +url+ on the server to Put to. To put to <tt>"/accounts"</tt> # pass <tt>"/accounts"</tt> as +url+ # * +body+ - A Ruby object which is converted into JSON and added in the request # Body. # * +valid_response_codes+ - An Array of HTTP response codes that should be # considered accepable and not raise exceptions. For example If you don't @@ -192,11 +195,11 @@ # # ... # end def put(url, body={}, *valid_response_codes, &block) body ||= {} - req = Net::HTTP::Put.new("/api#{url}") + req = Net::HTTP::Put.new(url) req.body = Yajl::Encoder.encode(body) prepare_request(req) response = connection.request(req) handle_response(response, valid_response_codes) @@ -211,12 +214,11 @@ # Posts to +url+ on the MLS Server. Automatically includes any headers returned # by the MLS#headers function. # # Paramaters:: # - # * +url+ - The +url+ on the server to Post to. This url will automatically - # be prefixed with <tt>"/api"</tt>. To post to <tt>"/api/accounts"</tt> + # * +url+ - The +url+ on the server to Post to. To post to <tt>"/accounts"</tt> # pass <tt>"/accounts"</tt> as +url+ # * +body+ - A Ruby object which is converted into JSON and added in the request # Body. # * +valid_response_codes+ - An Array of HTTP response codes that should be # considered accepable and not raise exceptions. For example If you don't @@ -251,11 +253,11 @@ # # ... # end def post(url, body={}, *valid_response_codes, &block) body ||= {} - req = Net::HTTP::Post.new("/api#{url}") + req = Net::HTTP::Post.new(url) req.body = Yajl::Encoder.encode(body) prepare_request(req) response = connection.request(req) handle_response(response, valid_response_codes) @@ -270,12 +272,11 @@ # Deletes to +url+ on the MLS Server. Automatically includes any headers returned # by the MLS#headers function. # # Paramaters:: # - # * +url+ - The +url+ on the server to Post to. This url will automatically - # be prefixed with <tt>"/api"</tt>. To delete to <tt>"/api/accounts"</tt> + # * +url+ - The +url+ on the server to Post to. To delete to <tt>"/accounts"</tt> # pass <tt>"/accounts"</tt> as +url+ # * +body+ - A Ruby object which is converted into JSON and added in the request # Body. # * +valid_response_codes+ - An Array of HTTP response codes that should be # considered accepable and not raise exceptions. For example If you don't @@ -310,11 +311,11 @@ # # ... # end def delete(url, body={}, *valid_response_codes, &block) body ||= {} - req = Net::HTTP::Delete.new("/api#{url}") + req = Net::HTTP::Delete.new(url) req.body = Yajl::Encoder.encode(body) prepare_request(req) response = connection.request(req) handle_response(response, valid_response_codes) @@ -382,9 +383,12 @@ when 300..599 raise MLS::Exception, code end end + cookie_jar[:api_session] = response['Set-Cookie'] if response['Set-Cookie'] + cookie_jar[:api_session_id] = response['X-42Floors-API-Session-Id'] if response['X-42Floors-API-Session-Id'] + 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.