Sha256: c3a845175d771b3598f14c9c5e96a8f3de2ad32b0e0b5531f0da7362072a8f19

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'prx/model'
require 'prx/representer'
require 'faraday'
require 'prx/middleware/json'

Faraday::Request.register_middleware(:prx_json => PRX::Middleware::Json)

module PRX
  module Client

    class << self
      
      include PRX::Model
      
      attr_accessor :key, :secret, :scheme, :host, :port, :version, :token


      def request(opts={})
        # puts "PRX::Client::request - opts: #{opts.inspect}"
        path = opts.delete(:path) || ''
        action = opts.delete(:action) || :get
        opts = default_options.merge(opts)

        path = api_path(path) unless path.starts_with?('/')
        response = access_token.send(action, path, opts)
        # puts response.inspect
        response
      end

      protected
      
      def api_path(path)
        "/api/#{version}/#{path}"
      end
      
      def access_token
        OAuth2::AccessToken.new(client, token, {})
      end

      def client
        OAuth2::Client.new(key, secret, {:site=>site})  do |b|
          b.request :multipart
          b.request :url_encoded
          b.request :prx_json

          # b.response :logger

          b.adapter  :excon
        end
      end

      def site
        "#{scheme || 'http'}://#{host}:#{port}"
      end
      
      def default_options
        {
          'Accept' => 'application/json',
          'Content-Type' => 'application/json'
        }
      end
      
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prx_client-0.3.1 lib/prx/client.rb
prx_client-0.3.0 lib/prx/client.rb