Class: Slash::Resource
- Inherits:
-
Object
- Object
- Slash::Resource
- Extended by:
- Forwardable
- Defined in:
- lib/slash/resource.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Response
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#params ⇒ Object
Returns the value of attribute params.
-
#password ⇒ Object
Returns the value of attribute password.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#uri ⇒ Object
Returns the value of attribute uri.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #[](path) ⇒ Object
-
#delete(options = {}, &block) ⇒ Object
Execute a DELETE request (see HTTP protocol documentation if unfamiliar).
-
#get(options = {}, &block) ⇒ Object
Execute a GET request.
-
#head(options = {}, &block) ⇒ Object
Execute a HEAD request.
-
#initialize(connection, uri, options = {}) ⇒ Resource
constructor
A new instance of Resource.
-
#post(options = {}, &block) ⇒ Object
Execute a POST request.
-
#put(options = {}, &block) ⇒ Object
Execute a PUT request.
- #request(options) ⇒ Object
- #slash(options = {}) ⇒ Object
- #user_agent ⇒ Object
- #user_agent=(value) ⇒ Object
Constructor Details
#initialize(connection, uri, options = {}) ⇒ Resource
Returns a new instance of Resource.
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/slash/resource.rb', line 51 def initialize(connection, uri, = {}) @connection = connection @uri = Addressable::URI.parse(uri) query = [:query] unless query.blank? @uri = @uri.dup uq = @uri.query_values @uri.query_values = uq ? uq.merge(query) : query end @params, @headers = ([:params] || {}).to_mash, [:headers] || {} self.user_agent ||= [:user_agent] || Slash::USER_AGENT end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
32 33 34 |
# File 'lib/slash/resource.rb', line 32 def connection @connection end |
#headers ⇒ Object
Returns the value of attribute headers.
32 33 34 |
# File 'lib/slash/resource.rb', line 32 def headers @headers end |
#params ⇒ Object
Returns the value of attribute params.
32 33 34 |
# File 'lib/slash/resource.rb', line 32 def params @params end |
#password ⇒ Object
Returns the value of attribute password.
32 33 34 |
# File 'lib/slash/resource.rb', line 32 def password @password end |
#proxy ⇒ Object
Returns the value of attribute proxy.
32 33 34 |
# File 'lib/slash/resource.rb', line 32 def proxy @proxy end |
#timeout ⇒ Object
Returns the value of attribute timeout.
32 33 34 |
# File 'lib/slash/resource.rb', line 32 def timeout @timeout end |
#uri ⇒ Object
Returns the value of attribute uri.
32 33 34 |
# File 'lib/slash/resource.rb', line 32 def uri @uri end |
#user ⇒ Object
Returns the value of attribute user.
32 33 34 |
# File 'lib/slash/resource.rb', line 32 def user @user end |
Class Method Details
.new!(*args, &block) ⇒ Object
45 46 47 48 49 |
# File 'lib/slash/resource.rb', line 45 def self.new!(*args, &block) r = allocate r.send(:initialize!, *args, &block) r end |
Instance Method Details
#[](path) ⇒ Object
75 76 77 |
# File 'lib/slash/resource.rb', line 75 def [](path) slash(:path => path) end |
#delete(options = {}, &block) ⇒ Object
Execute a DELETE request (see HTTP protocol documentation if unfamiliar). Used to delete resources.
87 88 89 |
# File 'lib/slash/resource.rb', line 87 def delete( = {}, &block) request(.merge(:method => :delete), &block) end |
#get(options = {}, &block) ⇒ Object
Execute a GET request. Used to get (find) resources.
81 82 83 |
# File 'lib/slash/resource.rb', line 81 def get( = {}, &block) request(.merge(:method => :get), &block) end |
#head(options = {}, &block) ⇒ Object
Execute a HEAD request. Used to obtain meta-information about resources, such as whether they exist and their size (via response headers).
105 106 107 |
# File 'lib/slash/resource.rb', line 105 def head( = {}, &block) request(.merge(:method => :head), &block) end |
#post(options = {}, &block) ⇒ Object
Execute a POST request. Used to create new resources.
99 100 101 |
# File 'lib/slash/resource.rb', line 99 def post( = {}, &block) request(.merge(:method => :post), &block) end |
#put(options = {}, &block) ⇒ Object
Execute a PUT request. Used to update resources.
93 94 95 |
# File 'lib/slash/resource.rb', line 93 def put( = {}, &block) request(.merge(:method => :put), &block) end |
#request(options) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/slash/resource.rb', line 109 def request() rq = prepare_request(merge()) connection.request(rq.delete(:method), rq.delete(:uri), rq) do |response| resp = handle_response(response) block_given? ? yield(resp) : resp end end |
#slash(options = {}) ⇒ Object
71 72 73 |
# File 'lib/slash/resource.rb', line 71 def slash( = {}) self.class.new!(self, ) end |
#user_agent ⇒ Object
37 38 39 |
# File 'lib/slash/resource.rb', line 37 def user_agent headers['User-Agent'] end |
#user_agent=(value) ⇒ Object
41 42 43 |
# File 'lib/slash/resource.rb', line 41 def user_agent=(value) headers['User-Agent'] = value end |