Class: Blackbaud::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/blackbaud-client/connector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, save_request_data_to) ⇒ Connector

Returns a new instance of Connector.



4
5
6
7
# File 'lib/blackbaud-client/connector.rb', line 4

def initialize(url, save_request_data_to)
  @web_services_url = url
  @save_request_data_to = save_request_data_to
end

Instance Attribute Details

#save_request_data_toObject

Returns the value of attribute save_request_data_to.



3
4
5
# File 'lib/blackbaud-client/connector.rb', line 3

def save_request_data_to
  @save_request_data_to
end

#tokenObject

Returns the value of attribute token.



3
4
5
# File 'lib/blackbaud-client/connector.rb', line 3

def token
  @token
end

#web_services_urlObject

Returns the value of attribute web_services_url.



3
4
5
# File 'lib/blackbaud-client/connector.rb', line 3

def web_services_url
  @web_services_url
end

Instance Method Details

#construct_url(web_services_url, endpoint, filters = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/blackbaud-client/connector.rb', line 22

def construct_url(web_services_url, endpoint, filters=nil)
  params = "?token=#{@token}" if @token
  if filters
    filters = Array(filters).map do |k,v|
      v = Array(v)
      "(#{k}%20eq%20#{v.join(',')})" if v && !v.join.empty?
    end
    params << "&filter=#{filters.join}"
  end
  url = "#{web_services_url}/#{endpoint}#{params}"
end

#get(endpoint, filters = nil) ⇒ Object



9
10
11
12
13
14
# File 'lib/blackbaud-client/connector.rb', line 9

def get(endpoint, filters=nil)
  url = construct_url(@web_services_url, endpoint, filters)
  response = RestClient.get(url)
  write_json_to_file(url, response) if @save_request_data_to
  JSON.parse(response)
end

#post(endpoint, body) ⇒ Object



16
17
18
19
20
# File 'lib/blackbaud-client/connector.rb', line 16

def post(endpoint, body)
  url = construct_url(@web_services_url, endpoint)
  response = RestClient.post(url, body, {:content_type=>'application/json'})
  JSON.parse(response)
end

#write_json_to_file(url, data) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/blackbaud-client/connector.rb', line 34

def write_json_to_file(url, data)
  return unless data
  file = url.gsub( /\/|\\/, ':' ).match(/.{,250}$/).to_s + '.json'
  file = File.expand_path(File.join(@save_request_data_to, file))
  FileUtils.mkdir_p @save_request_data_to
  File.open(file, 'w') { |f| f.write(JSON.pretty_unparse(JSON.parse(data)))}
end