Sha256: e2d940ac550d1149e365d4392e1c0b313059d54ec973473c6a40ed8bd7fa98c4

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require 'json'

module Bamboo
  module Client
    module Http
      class Json < Abstract
        class Doc
          attr_reader :data

          def self.from(str)
            new JSON.parse(str)
          end

          def initialize(data)
            @data = data
            pp @data if $DEBUG
          end

          def doc_for(key)
            Doc.new @data.fetch(key)
          end

          def auto_expand(klass, client)
            key_to_expand = @data.fetch('expand')

            obj = @data[key_to_expand]
            case obj
            when Hash
              if obj.has_key?('expand')
                Doc.new(obj).auto_expand(klass, client)
              else
                klass.new(obj, client)
              end
            when Array
              obj.map { |e| klass.new(e, client) }
            else
              raise TypeError, "don't know how to auto-expand #{obj.inspect}"
            end
          end
        end # Doc

        attr_reader :cookies

        def post(uri_or_path, data = {}, cookies = nil)
          resp = RestClient.post(uri_for(uri_or_path), data.to_json, :accept => :json, :content_type => :json, :cookies => cookies)
          Doc.from(resp) unless resp.empty?
        end

        def post_with_query(uri_or_path, query = {}, cookies = nil)
          resp = RestClient.post(uri_for(uri_or_path, query), '{}', :accept => :json, :content_type => :json, :cookies => cookies)
          Doc.from(resp) unless resp.empty?
        end

        def get(uri_or_path, params = nil, cookies = nil)
          uri = uri_for(uri_or_path, params)
          Doc.from RestClient.get(uri, :accept => :json, :cookies => cookies)
        end

        def get_cookies(uri_or_path, params = nil)
          uri = uri_for(uri_or_path, nil)
          resp = RestClient.get(uri, :params => params)
          @cookies = resp.cookies
        end

      end # Json
    end # Http
  end # Client
end # Bamboo

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bamboo-client-0.1.7 lib/bamboo-client/http/json.rb