Class: Wesabe::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/wesabe/request.rb

Defined Under Namespace

Classes: Exception, Redirect, RequestFailed, ResourceNotFound, ServerBrokeConnection, Unauthorized

Constant Summary collapse

DEFAULT_HEADERS =
{
  'User-Agent' => "Wesabe-RubyGem/#{Wesabe::VERSION} (Ruby #{RUBY_VERSION}; #{RUBY_PLATFORM})"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



2
3
4
# File 'lib/wesabe/request.rb', line 2

def method
  @method
end

#passwordObject (readonly)

Returns the value of attribute password.



2
3
4
# File 'lib/wesabe/request.rb', line 2

def password
  @password
end

#payloadObject (readonly)

Returns the value of attribute payload.



2
3
4
# File 'lib/wesabe/request.rb', line 2

def payload
  @payload
end

#proxyObject (readonly)

Returns the value of attribute proxy.



2
3
4
# File 'lib/wesabe/request.rb', line 2

def proxy
  @proxy
end

#urlObject (readonly)

Returns the value of attribute url.



2
3
4
# File 'lib/wesabe/request.rb', line 2

def url
  @url
end

#usernameObject (readonly)

Returns the value of attribute username.



2
3
4
# File 'lib/wesabe/request.rb', line 2

def username
  @username
end

Class Method Details

.base_urlObject

Gets the base url for the Wesabe API.



135
136
137
# File 'lib/wesabe/request.rb', line 135

def self.base_url
  @base_url ||= "https://www.wesabe.com"
end

.base_url=(base_url) ⇒ Object

Sets the base url for the Wesabe API.



140
141
142
# File 'lib/wesabe/request.rb', line 140

def self.base_url=(base_url)
  @base_url = base_url
end

.ca_fileObject



126
127
128
129
130
131
132
# File 'lib/wesabe/request.rb', line 126

def self.ca_file
  [File.expand_path("~/.wesabe"), File.join(File.dirname(__FILE__), '..')].each do |dir|
    file = File.join(dir, "cacert.pem")
    return file if File.exist?(file)
  end
  raise "Unable to find a CA pem file to use for www.wesabe.com"
end

.execute(options = Hash.new) ⇒ Net::HTTPResponse

Executes a request and returns the response.

Parameters:

  • options[:url] (String)

    The url relative to Wesabe::Request.base_url to request (required).

  • options[:username] (String)

    The Wesabe username (required).

  • options[:password] (String)

    The Wesabe password (required).

  • options[:proxy] (String)

    The proxy url to use (optional).

  • options[:method] (String, Symbol)

    The HTTP method to use (defaults to :get).

  • options[:payload] (String)

    The post-body to use (defaults to an empty string).

Returns:

  • (Net::HTTPResponse)

    The response object for the request just made.

Raises:

  • (EOFError)

    If the connection with the server breaks.

  • (Timeout::Error)

    If the request takes too long.



122
123
124
# File 'lib/wesabe/request.rb', line 122

def self.execute(options=Hash.new)
  new(options).execute
end

Instance Method Details

#executeString

Executes the request and returns the response.

Returns:

  • (String)

    The response object for the request just made.

Raises:

  • (Wesabe::ServerConnectionBroken)

    If the connection with the server breaks.

  • (Timeout::Error)

    If the request takes too long.



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/wesabe/request.rb', line 80

def execute
  # set up the uri
  @username = uri.user if uri.user
  @password = uri.password if uri.password

  # set up the request
  req = Net::HTTP.const_get(method.to_s.capitalize).new(uri.request_uri, DEFAULT_HEADERS)
  req.basic_auth(username, password)

  net.start do |http|
    process_response http.request(req, payload || "")
  end
end