lib/fanforce/api/response.rb in fanforce-api-0.31.0 vs lib/fanforce/api/response.rb in fanforce-api-1.0.0

- old
+ new

@@ -1,115 +1,114 @@ class Fanforce::API::Response attr_reader :curl_command, :requested_url, :requested_params def self.process(response, request, requested_url, requested_params) return nil if response.code == 404 and request.method == :get - raise Fanforce::API::ServerError.new(response, request, requested_url, requested_params) if response.code > 201 + raise Fanforce::API::Error::Server.new(response, request, requested_url, requested_params) if response.code > 201 - begin response_hash = Fanforce::Utils.decode_json(response) - rescue Exception => e; raise Fanforce::API::DecodingError.new(e, response, request, requested_url, requested_params) end + begin response_hash = MultiJson.load(response, symbolize_keys: true) + rescue Exception => e; raise Fanforce::API::Error::Decoding.new(e, response, request, requested_url, requested_params) end if !response_hash.is_a?(Hash) - raise Fanforce::API::DecodingError.new('Server did not send a valid hash.', response, request, requested_url, requested_params) + raise Fanforce::API::Error::Decoding.new('Server did not send a valid hash.', response, request, requested_url, requested_params) elsif response_hash[:results] - Fanforce::API::Results.new(response_hash, response, request, requested_url, requested_params) + MultipleResults.new(response_hash, response, request, requested_url, requested_params) else - Fanforce::API::Result.new(response_hash, response, request, requested_url, requested_params) + SingleResult.new(response_hash, response, request, requested_url, requested_params) end end -end + class MultipleResults < Array + attr_reader :requested_url, :requested_params -class Fanforce::API::Result < Hash - attr_reader :requested_url, :requested_params + def initialize(response_hash, response, request, requested_url, requested_params) + @response = response + @request = request + @requested_url = requested_url + @requested_params = requested_params + @response_hash = response_hash + self.replace(response_hash[:results]) + end - def initialize(response_hash, response, request, requested_url, requested_params) - @response = response - @request = request - @requested_url = requested_url - @requested_params = requested_params - @response_hash = response_hash - self.replace(response_hash) - end + def data + self + end + alias results data - def data - self - end - alias result data + def curl_command + @curl_command ||= Fanforce.curl_command(@request.method, @requested_url, @requested_params) + end - def curl_command - @curl_command ||= Fanforce::Utils.curl_command(@request.method, @requested_url, @requested_params) - end + def body + @response.to_s + end - def body; - @response.to_s - end + def current_results + @current_results ||= @response_hash[:current_results] + end - def current_results; 1 end - def total_results; 1 end - def current_page; 1 end - def total_pages; 1 end - def prev_page; nil end - def next_page; nil end - def prev_page_url; nil end - def next_page_url; nil end - def code; @response.code end -end + def total_results; + @total_results ||= @response_hash[:total_results] + end -class Fanforce::API::Results < Array - attr_reader :requested_url, :requested_params + def current_page; + @current_page ||= @response_hash[:current_page] + end - def initialize(response_hash, response, request, requested_url, requested_params) - @response = response - @request = request - @requested_url = requested_url - @requested_params = requested_params - @response_hash = response_hash - self.replace(response_hash[:results]) - end + def total_pages; + @total_pages ||= @response_hash[:total_pages] + end - def data - self - end - alias results data + def prev_page + @prev_page ||= (@request.method == :get and current_page > 1) ? current_page - 1 : nil + end - def curl_command - @curl_command ||= Fanforce::Utils.curl_command(@request.method, @requested_url, @requested_params) - end + def next_page + @next_page ||= (@request.method == :get and total_pages > current_page) ? current_page + 1 : nil + end - def body - @response.to_s - end + def prev_page_url; + @prev_page_url ||= prev_page ? "#{@requested_url}?#{Fanforce.to_query_string(@requested_params.merge(page: current_page-1))}" : nil + end - def current_results - @current_results ||= @response_hash[:current_results] + def next_page_url; + @next_page_url ||= next_page ? "#{@requested_url}?#{Fanforce.to_query_string(@requested_params.merge(page: current_page+1))}" : nil + end + def code; @response.code end end - def total_results; - @total_results ||= @response_hash[:total_results] - end + class SingleResult < Hash + attr_reader :requested_url, :requested_params - def current_page; - @current_page ||= @response_hash[:current_page] - end + def initialize(response_hash, response, request, requested_url, requested_params) + @response = response + @request = request + @requested_url = requested_url + @requested_params = requested_params + @response_hash = response_hash + self.replace(response_hash) + end - def total_pages; - @total_pages ||= @response_hash[:total_pages] - end + def data + self + end + alias result data - def prev_page - @prev_page ||= (@request.method == :get and current_page > 1) ? current_page - 1 : nil - end + def curl_command + @curl_command ||= Fanforce.curl_command(@request.method, @requested_url, @requested_params) + end - def next_page - @next_page ||= (@request.method == :get and total_pages > current_page) ? current_page + 1 : nil - end + def body; + @response.to_s + end - def prev_page_url; - @prev_page_url ||= prev_page ? "#{@requested_url}?#{Fanforce::Utils.to_query_string(@requested_params.merge(page: current_page-1))}" : nil + def current_results; 1 end + def total_results; 1 end + def current_page; 1 end + def total_pages; 1 end + def prev_page; nil end + def next_page; nil end + def prev_page_url; nil end + def next_page_url; nil end + def code; @response.code end end - - def next_page_url; - @next_page_url ||= next_page ? "#{@requested_url}?#{Fanforce::Utils.to_query_string(@requested_params.merge(page: current_page+1))}" : nil - end - def code; @response.code end end