Class: OData::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Resource



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_odata/resource.rb', line 5

def initialize(url, options={}, backwards_compatibility=nil, &block)
  @url = url
  @block = block
  @options = options.is_a?(Hash) ? options : { user: options, password: backwards_compatibility }

  @conn = Faraday.new(url: url, ssl: { verify: verify_ssl }) do |faraday|
    faraday.use      :gzip
    faraday.response :raise_error
    faraday.adapter  :excon

    faraday.options.timeout      = timeout if timeout
    faraday.options.open_timeout = open_timeout if open_timeout

    faraday.headers = (faraday.headers || {}).merge(@options[:headers] || {})
    faraday.headers = (faraday.headers).merge({
      :accept => '*/*; q=0.5, application/xml',
    })

    faraday.basic_auth user, password if user# this adds to headers so must be behind
  end

  @conn.headers[:user_agent] = 'Ruby'
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block



3
4
5
# File 'lib/ruby_odata/resource.rb', line 3

def block
  @block
end

#optionsObject (readonly)

Returns the value of attribute options



3
4
5
# File 'lib/ruby_odata/resource.rb', line 3

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url



3
4
5
# File 'lib/ruby_odata/resource.rb', line 3

def url
  @url
end

Instance Method Details

#[](suburl, &new_block) ⇒ Object

Construct a subresource, preserving authentication.

Example:

site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd') site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'

This is especially useful if you wish to define your site in one place and call it in multiple locations:

def orders RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd') end

orders.get # GET http://example.com/orders orders['1'].get # GET http://example.com/orders/1 orders['1/items'].delete # DELETE http://example.com/orders/1/items

Nest resources as far as you want:

site = RestClient::Resource.new('http://example.com') posts = site['posts'] first_post = posts['1'] comments = first_post['comments'] comments.post 'Hello', :content_type => 'text/plain'



128
129
130
131
132
133
134
135
# File 'lib/ruby_odata/resource.rb', line 128

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:



137
138
139
140
141
142
143
144
145
# File 'lib/ruby_odata/resource.rb', line 137

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 = {}) ⇒ Object



67
68
69
70
71
72
# File 'lib/ruby_odata/resource.rb', line 67

def delete(additional_headers={})
  @conn.delete do |req|
    req.url url
    req.headers = (headers || {}).merge(additional_headers)
  end
end

#get(additional_headers = {}) ⇒ Object



29
30
31
32
33
34
# File 'lib/ruby_odata/resource.rb', line 29

def get(additional_headers={})
  @conn.get do |req|
    req.url url
    req.headers = (headers || {}).merge(additional_headers)
  end
end

#head(additional_headers = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/ruby_odata/resource.rb', line 36

def head(additional_headers={})
  @conn.head do |req|
    req.url url
    req.headers = (headers || {}).merge(additional_headers)
  end
end

#headersObject



90
91
92
# File 'lib/ruby_odata/resource.rb', line 90

def headers
  @conn.headers || {}
end

#open_timeoutObject



98
99
100
# File 'lib/ruby_odata/resource.rb', line 98

def open_timeout
  options[:open_timeout]
end

#passwordObject



82
83
84
# File 'lib/ruby_odata/resource.rb', line 82

def password
  options[:password]
end

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



59
60
61
62
63
64
65
# File 'lib/ruby_odata/resource.rb', line 59

def patch(payload, additional_headers={})
  @conn.patch do |req|
    req.url url
    req.headers = (headers || {}).merge(additional_headers)
    req.body = prepare_payload payload
  end
end

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



43
44
45
46
47
48
49
# File 'lib/ruby_odata/resource.rb', line 43

def post(payload, additional_headers={})
  @conn.post do |req|
    req.url url
    req.headers = (headers || {}).merge(additional_headers)
    req.body = prepare_payload payload
  end
end

#prepare_payload(payload) ⇒ Object



147
148
149
150
151
# File 'lib/ruby_odata/resource.rb', line 147

def prepare_payload payload
  JSON.generate(payload)
rescue JSON::GeneratorError
  payload
end

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



51
52
53
54
55
56
57
# File 'lib/ruby_odata/resource.rb', line 51

def put(payload, additional_headers={})
  @conn.put do |req|
    req.url url
    req.headers = (headers || {}).merge(additional_headers)
    req.body = prepare_payload payload
  end
end

#timeoutObject



94
95
96
# File 'lib/ruby_odata/resource.rb', line 94

def timeout
  options[:timeout]
end

#to_sObject



74
75
76
# File 'lib/ruby_odata/resource.rb', line 74

def to_s
  url
end

#userObject



78
79
80
# File 'lib/ruby_odata/resource.rb', line 78

def user
  options[:user]
end

#verify_sslObject



86
87
88
# File 'lib/ruby_odata/resource.rb', line 86

def verify_ssl
  options[:verify_ssl]
end