Sha256: 1a0fc1147c2dddc36ccaee91e3a32b2a9c11e4f14c7fee5b436ed33422c3f36f
Contents?: true
Size: 1.34 KB
Versions: 2
Compression:
Stored size: 1.34 KB
Contents
require 'json' module Asynk class Response attr_reader :status, :body, :error_message def initialize(status: , body: nil, error_message: nil) @status, @body, @error_message = status, body, error_message end def success?; @status.to_s == 'ok'; end def fail?; !success?; end def to_h(options = {}) { status: @status, body: @body, error_message: @error_message } end def status ActiveSupport::StringInquirer.new(@status) end def errors; @error_message; end def [](key) @body[key] end def to_s to_h end def to_json to_h.to_json end alias_method :to_s, :to_h alias_method :as_json, :to_h alias_method :inspect, :to_s def self.try_to_create_from_hash(payload) return nil if payload.nil? parsed_payload = try_parse_json(payload) return payload unless parsed_payload return payload unless parsed_payload.kind_of?(Hash) hiwa = parsed_payload.with_indifferent_access return payload unless (hiwa.has_key?(:status) && hiwa.has_key?(:body) && hiwa.has_key?(:error_message)) new(status: hiwa[:status], body: hiwa[:body], error_message: hiwa[:error_message]) end def self.try_parse_json(str) begin JSON.parse(str) rescue JSON::ParserError => e return false end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
asynk-0.0.2 | lib/asynk/response.rb |
asynk-0.0.1 | lib/asynk/response.rb |