Sha256: 982cbf9edaab10b8914feeb13fb723ca4c456b73dda8179b2a6ec4fda958b785

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

path = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)

require 'httparty'
require 'ostruct'

require 'hashie'
require 'json'
require 'rubyoverflow/sites'
require 'rubyoverflow/users'
require "rubyoverflow/version"

module Rubyoverflow
  class Client
    include HTTParty
    format :plain
    HOST = 'http://api.stackoverflow.com'
    VERSION = '1.1'

    attr_accessor :host
    attr_accessor :api_key

    def initialize(options = OpenStruct.new)
      if options.kind_of? OpenStruct
        @host = options.host || HOST
        @version = options.version || VERSION
        @api_key = options.api_key if options.api_key
      end
    end

    def request(path, parameters = {})
      parameters['key'] = @api_key unless @api_key.nil? || @api_key.empty?
      url = host_path + normalize(path) + query_string(parameters)
      response = self.class.get url
      return JSON.parse(response.body), url
    end

    def host_path
      normalize(@host) +  normalize(@version)
    end

    class << self
      def stackauth_client(api_key = '')
        options = OpenStruct.new
        options.host = 'http://stackauth.com/'
        options.api_key = api_key if api_key
        Client.new options
      end
    end

    private
    def normalize(path)
      path.end_with?('/') ? path : path+ '/'
    end

    def query_string(parameters)
      if !parameters.empty?
        params = parameters.sort_by { |k, v| k.to_s }
        pairs  = params.map { |key, value| "#{key}=#{value}" }

        '?' + pairs.join('&')
      else
        ''
      end
   end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubyoverflow-0.5 lib/rubyoverflow.rb