Class: Restfully::Session
- Includes:
- HTTP::Headers, Parsing
- Defined in:
- lib/restfully/session.rb
Constant Summary
Constants included from Parsing
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#default_headers ⇒ Object
readonly
Returns the value of attribute default_headers.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#delete(path, options = {}) ⇒ Object
returns an HTTP::Response object or raise a Restfully::HTTP::Error.
-
#get(path, options = {}) ⇒ Object
returns an HTTP::Response object or raise a Restfully::HTTP::Error.
-
#head(path, options = {}) ⇒ Object
returns an HTTP::Response object or raise a Restfully::HTTP::Error.
-
#initialize(options = {}) {|@root.load, _self| ... } ⇒ Session
constructor
A new instance of Session.
-
#post(path, body, options = {}) ⇒ Object
returns an HTTP::Response object or raise a Restfully::HTTP::Error.
-
#put(path, body, options = {}) ⇒ Object
returns an HTTP::Response object or raise a Restfully::HTTP::Error.
-
#root ⇒ Object
returns the root resource.
-
#uri_for(path) ⇒ Object
builds the complete URI, based on the given path and the session’s base_uri.
Methods included from Parsing
#select_parser_for, #serialize, #unserialize
Methods included from HTTP::Headers
Constructor Details
#initialize(options = {}) {|@root.load, _self| ... } ⇒ Session
Returns a new instance of Session.
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( = {}) = .symbolize_keys if (config_filename = .delete(:configuration_file)) && File.exists?(File.(config_filename)) config = YAML.load_file(File.(config_filename)).symbolize_keys .merge!(config) end @base_uri = URI.parse(.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 = .delete(:logger) || NullLogger.new @logger.level = .delete(:verbose) ? Logger::DEBUG : @logger.level user_default_headers = sanitize_http_headers(.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, .merge(:logger => logger)) @root = Resource.new(@base_uri, self) yield @root.load, self if block_given? end |
Instance Attribute Details
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
13 14 15 |
# File 'lib/restfully/session.rb', line 13 def base_uri @base_uri end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
13 14 15 |
# File 'lib/restfully/session.rb', line 13 def connection @connection end |
#default_headers ⇒ Object (readonly)
Returns the value of attribute default_headers.
13 14 15 |
# File 'lib/restfully/session.rb', line 13 def default_headers @default_headers end |
#logger ⇒ Object (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, = {}) = .symbolize_keys transmit :delete, HTTP::Request.new(uri_for(path), :headers => .delete(:headers), :query => .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, = {}) = .symbolize_keys transmit :get, HTTP::Request.new(uri_for(path), :headers => .delete(:headers), :query => .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, = {}) = .symbolize_keys transmit :head, HTTP::Request.new(uri_for(path), :headers => .delete(:headers), :query => .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, = {}) = .symbolize_keys uri = uri_for(path) transmit :post, HTTP::Request.new(uri_for(path), :body => body, :headers => .delete(:headers), :query => .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, = {}) = .symbolize_keys uri = uri_for(path) transmit :put, HTTP::Request.new(uri_for(path), :body => body, :headers => .delete(:headers), :query => .delete(:query)) end |
#root ⇒ Object
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 |