Class: Restfully::Session

Inherits:
Object show all
Includes:
HTTP::Headers, Parsing
Defined in:
lib/restfully/session.rb

Constant Summary

Constants included from Parsing

Parsing::PARSERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parsing

#select_parser_for, #serialize, #unserialize

Methods included from HTTP::Headers

#sanitize_http_headers

Constructor Details

#initialize(options = {}) {|@root.load, _self| ... } ⇒ Session

Returns a new instance of Session.

Yields:

  • (@root.load, _self)

Yield Parameters:

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/restfully/session.rb', line 15

def initialize(options = {})
  options = options.symbolize_keys
  if (config_filename = options.delete(:configuration_file)) && File.exists?(File.expand_path(config_filename))
    config = YAML.load_file(File.expand_path(config_filename)).symbolize_keys
    options.merge!(config)
  end
  @base_uri               =   URI.parse(options.delete(:base_uri) || "http://localhost:8888") rescue nil
  raise ArgumentError.new("#{@base_uri} is not a valid URI") if @base_uri.nil? || @base_uri.scheme !~ /^http/i
  @logger                 =   options.delete(:logger) || NullLogger.new
  @logger.level           =   options.delete(:verbose) ? Logger::DEBUG : @logger.level
  user_default_headers    =   sanitize_http_headers(options.delete(:default_headers) || {})
  @default_headers        =   {'User-Agent' => "Restfully/#{Restfully::VERSION}", 'Accept' => 'application/json'}.merge(user_default_headers)
  @connection             =   Restfully.adapter.new(base_uri.to_s, options.merge(:logger => logger))
  @root                   =   Resource.new(@base_uri, self)
  yield @root.load, self if block_given?
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



13
14
15
# File 'lib/restfully/session.rb', line 13

def base_uri
  @base_uri
end

#connectionObject (readonly)

Returns the value of attribute connection.



13
14
15
# File 'lib/restfully/session.rb', line 13

def connection
  @connection
end

#default_headersObject (readonly)

Returns the value of attribute default_headers.



13
14
15
# File 'lib/restfully/session.rb', line 13

def default_headers
  @default_headers
end

#loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'lib/restfully/session.rb', line 13

def logger
  @logger
end

Instance Method Details

#delete(path, options = {}) ⇒ Object

returns an HTTP::Response object or raise a Restfully::HTTP::Error



64
65
66
67
# File 'lib/restfully/session.rb', line 64

def delete(path, options = {})
  options = options.symbolize_keys
  transmit :delete, HTTP::Request.new(uri_for(path), :headers => options.delete(:headers), :query => options.delete(:query))
end

#get(path, options = {}) ⇒ Object

returns an HTTP::Response object or raise a Restfully::HTTP::Error



44
45
46
47
# File 'lib/restfully/session.rb', line 44

def get(path, options = {})
  options = options.symbolize_keys
  transmit :get, HTTP::Request.new(uri_for(path), :headers => options.delete(:headers), :query => options.delete(:query))
end

#head(path, options = {}) ⇒ Object

returns an HTTP::Response object or raise a Restfully::HTTP::Error



38
39
40
41
# File 'lib/restfully/session.rb', line 38

def head(path, options = {})
  options = options.symbolize_keys
  transmit :head, HTTP::Request.new(uri_for(path), :headers => options.delete(:headers), :query => options.delete(:query))
end

#post(path, body, options = {}) ⇒ Object

returns an HTTP::Response object or raise a Restfully::HTTP::Error



50
51
52
53
54
# File 'lib/restfully/session.rb', line 50

def post(path, body, options = {})
  options = options.symbolize_keys
  uri = uri_for(path)
  transmit :post, HTTP::Request.new(uri_for(path), :body => body, :headers => options.delete(:headers), :query => options.delete(:query))
end

#put(path, body, options = {}) ⇒ Object

returns an HTTP::Response object or raise a Restfully::HTTP::Error



57
58
59
60
61
# File 'lib/restfully/session.rb', line 57

def put(path, body, options = {})
  options = options.symbolize_keys
  uri = uri_for(path)
  transmit :put, HTTP::Request.new(uri_for(path), :body => body, :headers => options.delete(:headers), :query => options.delete(:query))
end

#rootObject

returns the root resource



33
34
35
# File 'lib/restfully/session.rb', line 33

def root
  @root.load
end

#uri_for(path) ⇒ Object

builds the complete URI, based on the given path and the session’s base_uri



70
71
72
# File 'lib/restfully/session.rb', line 70

def uri_for(path)
  URI.join(base_uri.to_s, path.to_s)
end