Class: Services::SltcApi

Inherits:
Object
  • Object
show all
Defined in:
app/models/services/sltc_api.rb

Constant Summary collapse

BATCH_SIZE =
100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri, api_key, account, username, password) ⇒ SltcApi

Returns a new instance of SltcApi.



21
22
23
24
25
26
27
# File 'app/models/services/sltc_api.rb', line 21

def initialize(base_uri, api_key, , username, password)
  @base_uri = base_uri
  @api_key = api_key
  @account = 
  @username = username
  @password = password
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



7
8
9
# File 'app/models/services/sltc_api.rb', line 7

def 
  @account
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



7
8
9
# File 'app/models/services/sltc_api.rb', line 7

def api_key
  @api_key
end

#base_uriObject (readonly)

Returns the value of attribute base_uri.



7
8
9
# File 'app/models/services/sltc_api.rb', line 7

def base_uri
  @base_uri
end

#passwordObject (readonly)

Returns the value of attribute password.



7
8
9
# File 'app/models/services/sltc_api.rb', line 7

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



7
8
9
# File 'app/models/services/sltc_api.rb', line 7

def username
  @username
end

Class Method Details

.for_configured_account(configured_account) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/services/sltc_api.rb', line 9

def self.()
  service_definition = .service_definition
   = .
  credentials = .credentials.last

  SltcApi.new(service_definition.server_base_url,
              service_definition.credentials.last.token,
              .,
              .username,
              credentials.password)
end

Instance Method Details

#assessment_request(provider_id, uploaded_after, uploaded_before, status) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/services/sltc_api.rb', line 120

def assessment_request(provider_id, uploaded_after, uploaded_before, status)
  parameters = {
    'provider_id' => provider_id,
    'status_updated_after' => uploaded_after.strftime("%Y-%m-%dT%H:%MZ"),
    'status_updated_before' => uploaded_before.strftime("%Y-%m-%dT%H:%MZ"),
    'status' => status
  }

  get_response do
    RestClient::Request.execute(method: :post,
                                url: build_url(:MdsAssessmentsAsync),
                                payload: parameters.to_json,
                                user: "#{username}@#{}",
                                password: password,
                                headers: authentication_headers.merge({ :content_type => :json,
                                                                        'Accept' => 'application/json' }))
  end
end

#assessment_request_status(request_id) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'app/models/services/sltc_api.rb', line 110

def assessment_request_status(request_id)
  get_response do
    RestClient::Request.execute(method: :get,
                                url: build_url(:MdsAssessmentsAsync, request_id),
                                user: "#{username}@#{}",
                                password: password,
                                headers: authentication_headers.merge( { 'Accept' => 'application/json' }))
  end
end

#download_assessment_request(request_id) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'app/models/services/sltc_api.rb', line 100

def download_assessment_request(request_id)
  get_response(true) do
    RestClient::Request.execute(method: :get,
                                url: build_url(:MdsAssessmentsAsync, request_id),
                                user: "#{username}@#{}",
                                password: password,
                                headers: authentication_headers.merge({ 'Accept' => 'application/octet-stream' }))
  end
end

#download_batch(batch_id) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'app/models/services/sltc_api.rb', line 90

def download_batch(batch_id)
  get_response(true) do
    RestClient::Request.execute(method: :get,
                                url: build_url(:MdsBatches, batch_id),
                                user: "#{username}@#{}",
                                password: password,
                                headers: authentication_headers.merge( { 'Accept' => 'application/octet-stream' } ))
  end
end

#list_batches(provider, pull_from, pull_to) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/services/sltc_api.rb', line 39

def list_batches(provider, pull_from, pull_to)
  batches = []
  page = 1
  while true
    response = get_response do
      RestClient::Request.execute(method: :get,
                                  url: build_url(:MdsBatches, { provider_id: provider,
                                                                page: page,
                                                                uploaded_after: pull_from,
                                                                uploaded_before: pull_to }),
                                  user: "#{username}@#{}",
                                  password: password,
                                  headers: authentication_headers.merge({ 'Accept' => 'application/json' }))
    end
    batches += response
    if response.size < BATCH_SIZE
      break
    else
      page += 1
    end
  end
  batches
end

#list_providersObject



29
30
31
32
33
34
35
36
37
# File 'app/models/services/sltc_api.rb', line 29

def list_providers
  get_response do
    RestClient::Request.execute(method: :get,
                                url: build_url(:Providers),
                                user: "#{username}@#{}",
                                password: password,
                                headers: authentication_headers.merge({ 'Accept' => 'application/json' }))
  end
end

#search_assessments(provider, pull_from, pull_to, status = 'Accepted') ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/services/sltc_api.rb', line 63

def search_assessments(provider, pull_from, pull_to, status='Accepted')
  get_response do
    RestClient::Request.execute(method: :get,
                                url: build_url(:MdsAssessments, { provider_id: provider,
                                                                  status_updated_after: pull_from,
                                                                  status_updated_before: pull_to, 
                                                                  status: status }),
                                user: "#{username}@#{}",
                                password: password,
                                headers: authentication_headers.merge({ 'Accept' => 'application/json' }))
  end
end

#upload_batch(provider_id, path) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/services/sltc_api.rb', line 76

def upload_batch(provider_id, path)
  file = nil
  File.open(path, "rb") { |f| file = f.read }
  get_response do
    RestClient::Request.execute(method: :post,
                                url: build_url(:MdsBatches, { filename: path }),
                                payload: file,
                                user: "#{username}@#{}",
                                password: password,
                                headers: authentication_headers.merge( { :content_type => 'application/octet-stream',
                                                                         'Accept' => 'application/json' } ))
  end
end