Sha256: 166ec1273c57a146574a886fb4f33ef32a89809abaf1082d425c701d153da7da

Contents?: true

Size: 1.94 KB

Versions: 6

Compression:

Stored size: 1.94 KB

Contents

module Wordnik

  class Response
    require 'active_model'
    require 'json'
    require 'nokogiri'
    require 'htmlentities'
    include ActiveModel::Validations
    include ActiveModel::Conversion
    extend ActiveModel::Naming

    attr_accessor :raw

    validates_presence_of :raw

    def initialize(raw)
      self.raw = raw
      catch_errors
    end    

    def code
      raw.code
    end
        
    def catch_errors
      case self.code
      when 500..510 then raise(ServerError, error_message)
      when 299..426 then raise(ClientError, error_message)
      end
    end

    # If the error message is not helpful, dump the whole response object
    # 
    def error_message
      msg = body['message']
      return self.raw.to_yaml if msg.blank? || msg =~ /system error/i || msg =~ /ServerError/
      return msg
    rescue
      self.raw.to_yaml
    end

    # If body is JSON, parse it
    # TODO: If body is XML, parse it
    # Otherwise return raw string
    def body
      JSON.parse raw.body
    rescue
      raw.body
    end

    def headers
      h = {}
      raw.headers_hash.each {|k,v| h[k] = v }
      h
    end

    # Extract the response format from the header hash
    # e.g. {'Content-Type' => 'application/json'}
    def format
      headers['Content-Type'].split("/").last.to_s
    end

    def json?
      format == 'json'
    end

    def xml?
      format == 'xml'
    end

    def pretty_body
      return unless body.present?
      case format
      when 'json'
        JSON.pretty_generate(body).gsub(/\n/, '<br/>')
      when 'xml'
        xsl = Nokogiri::XSLT(File.open(File.join(File.dirname(__FILE__), "../../config/pretty_print.xsl")))
        xml = Nokogiri(body)
        coder = HTMLEntities.new
        coder.encode(xsl.apply_to(xml).to_s)
      end
    end

    def pretty_headers
      JSON.pretty_generate(headers).gsub(/\n/, '<br/>')
    end

    # It's an ActiveModel thing..
    def persisted?
      false
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
wordnik-4.11 lib/wordnik/response.rb
wordnik-4.10 lib/wordnik/response.rb
wordnik-4.09 lib/wordnik/response.rb
wordnik-4.08 lib/wordnik/response.rb
wordnik-4.07 lib/wordnik/response.rb
wordnik-4.06.15 lib/wordnik/response.rb