Class: RestMan::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/restman/resource.rb

Overview

:include: _doc/lib/restman/resource.rdoc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}, backwards_compatibility = nil, &block) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
10
11
12
13
14
# File 'lib/restman/resource.rb', line 6

def initialize(url, options={}, backwards_compatibility=nil, &block)
  @url = url
  @block = block
  if options.class == Hash
    @options = options
  else # compatibility with previous versions
    @options = { :user => options, :password => backwards_compatibility }
  end
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



4
5
6
# File 'lib/restman/resource.rb', line 4

def block
  @block
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/restman/resource.rb', line 4

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/restman/resource.rb', line 4

def url
  @url
end

Instance Method Details

#[](suburl, &new_block) ⇒ Object

:include: _doc/lib/restman/resource/[].rdoc



102
103
104
105
106
107
108
# File 'lib/restman/resource.rb', line 102

def [](suburl, &new_block)
  case
  when block_given? then self.class.new(concat_urls(url, suburl), options, &new_block)
  when block        then self.class.new(concat_urls(url, suburl), options, &block)
  else                   self.class.new(concat_urls(url, suburl), options)
  end
end

#concat_urls(url, suburl) ⇒ Object

:nodoc:



110
111
112
113
114
115
116
117
118
# File 'lib/restman/resource.rb', line 110

def concat_urls(url, suburl) # :nodoc:
  url = url.to_s
  suburl = suburl.to_s
  if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
    url + suburl
  else
    "#{url}/#{suburl}"
  end
end

#delete(additional_headers = {}, &block) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/restman/resource.rb', line 64

def delete(additional_headers={}, &block)
  headers = (options[:headers] || {}).merge(additional_headers)
  Request.execute(options.merge(
          :method => :delete,
          :url => url,
          :headers => headers,
          :log => log), &(block || @block))
end

#get(additional_headers = {}, &block) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/restman/resource.rb', line 16

def get(additional_headers={}, &block)
  headers = (options[:headers] || {}).merge(additional_headers)
  Request.execute(options.merge(
          :method => :get,
          :url => url,
          :headers => headers,
          :log => log), &(block || @block))
end

#head(additional_headers = {}, &block) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/restman/resource.rb', line 25

def head(additional_headers={}, &block)
  headers = (options[:headers] || {}).merge(additional_headers)
  Request.execute(options.merge(
          :method => :head,
          :url => url,
          :headers => headers,
          :log => log), &(block || @block))
end

#headersObject



85
86
87
# File 'lib/restman/resource.rb', line 85

def headers
  options[:headers] || {}
end

#logObject



97
98
99
# File 'lib/restman/resource.rb', line 97

def log
  options[:log] || RestMan.log
end

#open_timeoutObject



93
94
95
# File 'lib/restman/resource.rb', line 93

def open_timeout
  options[:open_timeout]
end

#passwordObject



81
82
83
# File 'lib/restman/resource.rb', line 81

def password
  options[:password]
end

#patch(payload, additional_headers = {}, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/restman/resource.rb', line 54

def patch(payload, additional_headers={}, &block)
  headers = (options[:headers] || {}).merge(additional_headers)
  Request.execute(options.merge(
          :method => :patch,
          :url => url,
          :payload => payload,
          :headers => headers,
          :log => log), &(block || @block))
end

#post(payload, additional_headers = {}, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/restman/resource.rb', line 34

def post(payload, additional_headers={}, &block)
  headers = (options[:headers] || {}).merge(additional_headers)
  Request.execute(options.merge(
          :method => :post,
          :url => url,
          :payload => payload,
          :headers => headers,
          :log => log), &(block || @block))
end

#put(payload, additional_headers = {}, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/restman/resource.rb', line 44

def put(payload, additional_headers={}, &block)
  headers = (options[:headers] || {}).merge(additional_headers)
  Request.execute(options.merge(
          :method => :put,
          :url => url,
          :payload => payload,
          :headers => headers,
          :log => log), &(block || @block))
end

#read_timeoutObject



89
90
91
# File 'lib/restman/resource.rb', line 89

def read_timeout
  options[:read_timeout]
end

#to_sObject



73
74
75
# File 'lib/restman/resource.rb', line 73

def to_s
  url
end

#userObject



77
78
79
# File 'lib/restman/resource.rb', line 77

def user
  options[:user]
end