lib/prx/client.rb in prx_client-0.1.3 vs lib/prx/client.rb in prx_client-0.2.0

- old
+ new

@@ -1,57 +1,65 @@ require 'prx/model' require 'prx/representer' +require 'faraday' +require 'faraday_middleware' module PRX module Client class << self include PRX::Model - attr_accessor :key, :secret, :scheme, :host, :port, :version, :token, :client_options, :access_token_options - - # get information about the current user - def me - access_token.get('/me', default_options) - end - + attr_accessor :key, :secret, :scheme, :host, :port, :version, :token + def piece_create(piece) - client_options[:multipart] = true - access_token.post(create_url('pieces'), :body=>{:piece=>piece.as_json}, 'Content-Type' => 'application/json') + access_token.post(create_url('pieces'), :body=>{:piece=>piece.as_json}) end - - def client_options; @client_options ||= {}; end - def access_token_options; @access_token_options ||= {}; end - + + 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 create_url(path) + def api_path(path) "/api/#{version}/#{path}" end + def access_token + OAuth2::AccessToken.new(client, token, {}) + end + def client - options = client_options.clone - options[:site] = site unless options.has_key?(:site) OAuth2::Client.new(key, secret, {:site=>site}) do |b| b.request :multipart b.request :url_encoded b.request :json + # b.response :logger - b.adapter :net_http + + b.adapter :excon end end - def access_token - OAuth2::AccessToken.new(client, token, access_token_options.clone) - end - def site "#{scheme || 'http'}://#{host}:#{port}" end def default_options - {'Accept' => 'application/json'} + { + 'Accept' => 'application/json', + 'Content-Type' => 'application/json' + } end end end \ No newline at end of file