Sha256: 6fa2f9f0305e6adfe89db2ec415f3ac32189c01adf95f9320c5b562df6fcad37

Contents?: true

Size: 1.73 KB

Versions: 28

Compression:

Stored size: 1.73 KB

Contents

module Sawyer
  class Response
    attr_reader :agent,
      :status,
      :headers,
      :env,
      :body,
      :rels

    # Builds a Response after a completed request.
    #
    # agent - The Sawyer::Agent that is managing the API connection.
    # res   - A Faraday::Response.
    def initialize(agent, res, options = {})
      @agent   = agent
      @status  = res.status
      @headers = res.headers
      @env     = res.env
      @body    = res.body
      @rels    = process_rels
      @started = options[:sawyer_started]
      @ended   = options[:sawyer_ended]
    end

    def data
      @data ||= begin
        return(body) unless (headers[:content_type] =~ /json|msgpack/) 
        process_data(agent.decode_body(body))
      end
    end

    # Turns parsed contents from an API response into a Resource or
    # collection of Resources.
    #
    # data - Either an Array or Hash parsed from JSON.
    #
    # Returns either a Resource or Array of Resources.
    def process_data(data)
      case data
      when Hash  then Resource.new(agent, data)
      when Array then data.map { |hash| process_data(hash) }
      when nil   then nil
      else data
      end
    end

    # Finds link relations from 'Link' response header
    #
    # Returns an array of Relations
    def process_rels
      links = ( @headers["Link"] || "" ).split(', ').map do |link|
        href, name = link.match(/<(.*?)>; rel="(\w+)"/).captures

        [name.to_sym, Relation.from_link(@agent, name, :href => href)]
      end

      Hash[*links.flatten]
    end

    def timing
      @timing ||= @ended - @started
    end

    def time
      @ended
    end

    def inspect
      %(#<#{self.class}: #{@status} @rels=#{@rels.inspect} @data=#{data.inspect}>)
    end
  end
end

Version data entries

28 entries across 17 versions & 2 rubygems

Version Path
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/sawyer-0.9.2/lib/sawyer/response.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/sawyer-0.9.2/lib/sawyer/response.rb
sawyer-0.9.2 lib/sawyer/response.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/sawyer-0.9.1/lib/sawyer/response.rb
sawyer-0.9.1 lib/sawyer/response.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
sawyer-0.9.0 lib/sawyer/response.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
tdiary-5.1.6 vendor/bundle/ruby/3.0.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
tdiary-5.1.5 vendor/bundle/ruby/2.7.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/sawyer-0.8.2/lib/sawyer/response.rb
tdiary-5.1.4 vendor/bundle/ruby/2.7.0/gems/sawyer-0.8.2/lib/sawyer/response.rb