Class: CollectiveAccess

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/collectiveaccess.rb

Constant Summary collapse

DEFAULT_REQUEST_OPTS =
{
  protocol: 'http',
  hostname: 'localhost',
  url_root: '/',
  script_name: 'service.php',
  table_name: 'ca_objects',
  endpoint: 'item',
  request_body: {},
  get_params: {},
  url_string: '',
  verify: false,
  port: 80
}

Class Method Summary collapse

Class Method Details

.build_simple_uri(opts) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/collectiveaccess.rb', line 118

def self.build_simple_uri(opts)
  # URI available params: scheme, userinfo, host, port, registry, path, opaque, query and fragment
  uri_opts = { :host => opts[:hostname],
               :port => opts[:port],
               :path => opts[:url_root]+opts[:script_name]+'/simple/'+opts[:endpoint],
               :query => URI.encode_www_form(opts[:get_params].merge(authToken: stored_auth_token)) }

  if opts[:protocol] == 'http'
    url = URI::HTTP.build uri_opts
  else
    url = URI::HTTPS.build uri_opts
  end

  url.path.gsub! %r{/+}, '/'
  url
end

.delete(request_opts = {}) ⇒ Object

HTTP DELETE request



56
57
58
# File 'lib/collectiveaccess.rb', line 56

def self.delete(request_opts = {})
  custom_request :delete, request_opts
end

.get(request_opts = {}) ⇒ Object

HTTP GET request



36
37
38
# File 'lib/collectiveaccess.rb', line 36

def self.get(request_opts = {})
  custom_request :get, request_opts
end

.options(request_opts = {}) ⇒ Object

HTTP OPTIONS request



46
47
48
# File 'lib/collectiveaccess.rb', line 46

def self.options(request_opts = {})
  custom_request :options, request_opts
end

.post(request_opts = {}) ⇒ Object

HTTP POST request



41
42
43
# File 'lib/collectiveaccess.rb', line 41

def self.post(request_opts = {})
  custom_request :post, request_opts
end

.put(request_opts = {}) ⇒ Object

HTTP PUT request



51
52
53
# File 'lib/collectiveaccess.rb', line 51

def self.put(request_opts = {})
  custom_request :put, request_opts
end

.set_credentials(user_name, key) ⇒ Object

Overrides the default user credentials that read from ENV and ENV



31
32
33
# File 'lib/collectiveaccess.rb', line 31

def self.set_credentials(user_name, key)
  @auth = basic_auth user_name, key
end

.simple(request_opts = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/collectiveaccess.rb', line 60

def self.simple(request_opts = {})
  opts = parse_options request_opts
  uri = build_simple_uri opts

  resp = HTTParty.public_send :get, uri

  # if 401 (access denied), try to re-authenticate
  if resp.code == 401
    delete_stored_auth_token # nuke old token
    authenticate opts
    # have to re-build the URI to include the new auth token
    new_uri = build_simple_uri opts
    # try again
    resp = HTTParty.public_send :get, new_uri
  end
  resp.parsed_response
end