Permalink
Browse files

Merge branch 'master' of github.com:dawanda/dawanda-api-client-ruby

  • Loading branch information...
2 parents 0e639c8 + f36d295 commit da35bcd92b0c8d3ff794da74206ecef0cc07e247 @sdepold sdepold committed Mar 23, 2010
Showing with 19 additions and 11 deletions.
  1. +11 −0 lib/dawanda.rb
  2. +8 −11 lib/dawanda/request.rb
View
@@ -82,6 +82,17 @@ def self.domain
@domain || 'dawanda.com'
end
+ # retrieve HTTP Basic credentials
+ def self.http_basic
+ @http_basic || nil
+ end
+
+ # set credetials for HTTP Basic Authentification
+ # like: Dawanda.http_basic = {:user => 'myusername', :password => 'mypassword'}
+ def self.http_basic= credentials=nil
+ @http_basic = credentials
+ end
+
# Find a user by username. See Dawanda::User for more information.
def self.user(username_or_id, options={})
User.find_by_user_id(username_or_id, options)
@@ -15,7 +15,6 @@ def self.base_url
# A Response object with the payload data
#
def self.get(resource_path, parameters = {})
- parameters = {:format => 'json'}.update(parameters)
request = Request.new(resource_path, parameters)
Response.new(request.get)
end
@@ -29,7 +28,11 @@ def initialize(resource_path, parameters = {})
# Perform a GET request against the API endpoint and return the raw
# response data
def get
- response = Net::HTTP.get_response(endpoint_uri)
+ request = Net::HTTP::Get.new endpoint_uri.path
+ request.basic_auth Dawanda.http_basic[:user], Dawanda.http_basic[:password] if Dawanda.http_basic
+ request.set_form_data parameters
+ response = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port).start {|http| http.request(request) }
+
case response
when Net::HTTPSuccess, Net::HTTPRedirection
return response.body
@@ -39,17 +42,11 @@ def get
end
def parameters # :nodoc:
- @parameters.merge(:api_key => Dawanda.api_key)
- end
-
- def query # :nodoc:
- parameters.map {|k,v| "#{k}=#{v}"}.join('&')
+ @parameters.merge(:api_key => Dawanda.api_key, :format => 'json')
end
def endpoint_uri # :nodoc:
- uri = URI.parse("#{self.class.base_url}#{@resource_path}")
- uri.query = query
- uri
+ URI.parse("#{self.class.base_url}#{@resource_path}")
end
end
-end
+end

0 comments on commit da35bcd

Please sign in to comment.