Class: SimpleMapper::HttpAdapter

Inherits:
Object
  • Object
show all
Includes:
CallbacksExtension, Oauth
Defined in:
lib/simple_mapper/adapters/http_adapter.rb,
lib/simple_mapper/default_plugins/oauth.rb,
lib/simple_mapper/default_plugins/callbacks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CallbacksExtension

#add_callback, #callbacks, #run_callback

Methods included from Oauth

#oauth, #requires_oauth, #set_oauth, with_oauth

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



14
15
16
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 14

def base_url
  @base_url
end

#raise_http_errorsObject

Returns the value of attribute raise_http_errors.



15
16
17
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 15

def raise_http_errors
  @raise_http_errors
end

Instance Method Details

#base_uriObject



18
19
20
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 18

def base_uri
  URI.parse(base_url)
end

#delete(identifier, options = {}) ⇒ Object

In the http adapter, the identifier is a url.



80
81
82
83
84
85
86
87
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 80

def delete(identifier,options={})
  begin
    http.request(request('delete', URI.parse(identifier).path + query_string_from_options(options))).body
  rescue => e
    raise e if !!raise_http_errors
    nil
  end
end

#display_optionsObject



39
40
41
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 39

def display_options
  @display_options ||= {}
end

#display_options=(options) ⇒ Object Also known as: set_display_options

Raises:

  • (TypeError)


42
43
44
45
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 42

def display_options=(options)
  raise TypeError, "options must be a hash!" unless options.is_a?(Hash)
  @display_options = options
end

#finder_optionsObject



31
32
33
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 31

def finder_options
  @finder_options ||= {}
end

#finder_options=(options) ⇒ Object Also known as: set_finder_options

Raises:

  • (TypeError)


34
35
36
37
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 34

def finder_options=(options)
  raise TypeError, "options must be a hash!" unless options.is_a?(Hash)
  @finder_options = options
end

#get(options = {}) ⇒ Object



48
49
50
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 48

def get(options={})
  raw_get(base_uri.path + query_string_from_options(finder_options.merge(display_options.merge(options))))
end

#headersObject



22
23
24
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 22

def headers
  @headers ||= {}
end

#headers=(v) ⇒ Object Also known as: set_headers

Raises:

  • (TypeError)


25
26
27
28
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 25

def headers=(v)
  raise TypeError, "headers set must be a hash" unless v.is_a?(Hash)
  headers.merge!(v)
end

#post(data, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 70

def post(data,options={})
  begin
    http.request(request('post', base_uri.path + query_string_from_options(display_options.merge(options)), data)).body
  rescue => e
    raise e if !!raise_http_errors
    nil
  end
end

#put(identifier, data, options = {}) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 61

def put(identifier,data,options={})
  begin
    http.request(request('put', URI.parse(identifier).path + query_string_from_options(display_options.merge(options)), data)).body
  rescue => e
    raise e if !!raise_http_errors
    nil
  end
end

#raw_get(url) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/simple_mapper/adapters/http_adapter.rb', line 52

def raw_get(url)
  begin
    http.request(request('get', url)).body
  rescue => e
    raise e if !!raise_http_errors
    nil
  end
end