lib/wordnik/response.rb in wordnik-0.0.1 vs lib/wordnik/response.rb in wordnik-0.0.2
- old
+ new
@@ -1,69 +1,73 @@
-class Response
- include ActiveModel::Validations
- include ActiveModel::Conversion
- extend ActiveModel::Naming
+module Wordnik
+
+ class Response
+ require 'active_model'
+ include ActiveModel::Validations
+ include ActiveModel::Conversion
+ extend ActiveModel::Naming
- attr_accessor :raw
+ attr_accessor :raw
- validates_presence_of :raw
+ validates_presence_of :raw
- def initialize(raw)
- self.raw = raw
- end
+ def initialize(raw)
+ self.raw = raw
+ end
- def code
- raw.code
- end
+ def code
+ raw.code
+ 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
+ # 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
+ 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_sym
- end
+ # Extract the response format from the header hash
+ # e.g. {'Content-Type' => 'application/json'}
+ def format
+ headers['Content-Type'].split("/").last.to_sym
+ end
- def json?
- format == :json
- end
+ def json?
+ format == :json
+ end
- def xml?
- format == :xml
- end
+ def xml?
+ format == :xml
+ end
- def pretty_body
- return unless body.present?
- case format
- when :json
- JSON.pretty_generate(body).gsub(/\n/, '<br/>').html_safe
- when :xml
- xsl = Nokogiri::XSLT(File.open(Rails.root.join("config", "pretty_print.xsl")))
- xml = Nokogiri(body)
- coder = HTMLEntities.new
- coder.encode(xsl.apply_to(xml).to_s)
+ def pretty_body
+ return unless body.present?
+ case format
+ when :json
+ JSON.pretty_generate(body).gsub(/\n/, '<br/>').html_safe
+ when :xml
+ xsl = Nokogiri::XSLT(File.open(Rails.root.join("config", "pretty_print.xsl")))
+ xml = Nokogiri(body)
+ coder = HTMLEntities.new
+ coder.encode(xsl.apply_to(xml).to_s)
+ end
end
- end
- def pretty_headers
- JSON.pretty_generate(headers).gsub(/\n/, '<br/>').html_safe
- end
+ def pretty_headers
+ JSON.pretty_generate(headers).gsub(/\n/, '<br/>').html_safe
+ end
- # It's an ActiveModel thing..
- def persisted?
- false
- end
+ # It's an ActiveModel thing..
+ def persisted?
+ false
+ end
+ end
end
\ No newline at end of file