Sha256: b2d139b8038a7912a0b893288284ce6fb50d979a1f32d383eb5364294640e307

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 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

        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 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)
          resp.cookies
        end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bamboo-client-0.0.9 lib/bamboo-client/http/json.rb
bamboo-client-0.0.8 lib/bamboo-client/http/json.rb