Class: Slash::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/slash/resource.rb

Direct Known Subclasses

SimpleResource

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @connection = connection
  @uri = Addressable::URI.parse(uri)
  query = options[:query]
  unless query.blank?
    @uri = @uri.dup
    uq = @uri.query_values
    @uri.query_values = uq ? uq.merge(query) : query
  end
  @params, @headers = (options[:params] || {}).to_mash, options[:headers] || {}
  self.user_agent ||= options[:user_agent] || Slash::USER_AGENT
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



32
33
34
# File 'lib/slash/resource.rb', line 32

def connection
  @connection
end

#headersObject

Returns the value of attribute headers.



32
33
34
# File 'lib/slash/resource.rb', line 32

def headers
  @headers
end

#paramsObject

Returns the value of attribute params.



32
33
34
# File 'lib/slash/resource.rb', line 32

def params
  @params
end

#passwordObject

Returns the value of attribute password.



32
33
34
# File 'lib/slash/resource.rb', line 32

def password
  @password
end

#proxyObject

Returns the value of attribute proxy.



32
33
34
# File 'lib/slash/resource.rb', line 32

def proxy
  @proxy
end

#timeoutObject

Returns the value of attribute timeout.



32
33
34
# File 'lib/slash/resource.rb', line 32

def timeout
  @timeout
end

#uriObject

Returns the value of attribute uri.



32
33
34
# File 'lib/slash/resource.rb', line 32

def uri
  @uri
end

#userObject

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(options = {}, &block)
  request(options.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(options = {}, &block)
  request(options.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(options = {}, &block)
  request(options.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(options = {}, &block)
  request(options.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(options = {}, &block)
  request(options.merge(:method => :put), &block)
end

#request(options) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/slash/resource.rb', line 109

def request(options)
  rq = prepare_request(merge(options))
  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(options = {})
  self.class.new!(self, options)
end

#user_agentObject



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