diff options
Diffstat (limited to 'lib/iev_api/client.rb')
| -rw-r--r-- | lib/iev_api/client.rb | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/iev_api/client.rb b/lib/iev_api/client.rb new file mode 100644 index 000000000..11a39dd10 --- /dev/null +++ b/lib/iev_api/client.rb @@ -0,0 +1,103 @@ +module IevApi + class Client + + PER_PAGE = 12 + PARALLEL_WORKERS = 10 + + attr_accessor *IevApi::Configuration::VALID_OPTIONS_KEYS + + def initialize(options={}) + attrs = IevApi.options.merge(options) + IevApi::Configuration::VALID_OPTIONS_KEYS.each do |key| + send("#{key}=", attrs[key]) + end + end + + def url_for(endpoint, *args) + path = case endpoint.to_s + when 'jobs' then jobs_path(args) + when 'job' then job_path(args) + when 'report' then report_path(args) + else raise ArgumentError.new("Unrecognized path: #{path}") + end + + [account_path, path.split('.').first].join('') + end + + def jobs(referential_id, options = {}) + results = request(:get, jobs_path(referential_id), options) + results.respond_to?(:jobs) ? results.jobs : [] + end + + def job(referential_id, job_id, options = {}) + results = request(:get, job_path(referential_id, job_id), options) + results.respond_to?(:job) ? results.job : [] + end + + def jobs_path(referential_id) + "/referentials/#{referential_id}/jobs" + end + + def job_path(referential_id, job_id) + "/referentials/#{referential_id}/jobs/#{job_id}" + end + + def report(referential_id, report_id, options = {}) + results = request(:get, report_path(referential_id, report_id), options) + results.respond_to?(:report) ? results.report : [] + end + + def report_path(referential_id, report_id) + "/referential/#{referential_id}/job/#{report_id}" + end + + def account_path + "#{protocol}://#{Rails.application.config.iev_url}" + end + + def protocol + @secure ? "https" : "http" + end + + # Perform an HTTP request + def request(method, path, params = {}, options = {}) + + response = connection(options).run_request(method, nil, nil, nil) do |request| + case method + when :delete, :get + request.url(connection.path_prefix + path, params) + when :post, :put + request.url(connection.path_prefix + path) + request.body = params unless params.empty? + end + end + + response.body + end + + def connection(options={}) + default_options = { + :headers => { + :accept => 'application/json', + :user_agent => user_agent, + }, + :ssl => {:verify => false}, + :url => account_path, + } + + @connection ||= Faraday.new(default_options.deep_merge(connection_options)) do |builder| + middleware.each { |mw| builder.use *mw } + + builder.adapter adapter + end + + # cache_dir = File.join(ENV['TMPDIR'] || '/tmp', 'cache') + + # @connection.response :caching do + # ActiveSupport::Cache::FileStore.new cache_dir, :namespace => 'iev', + # :expires_in => 3600 # one hour + # end + end + + end +end |
