aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/iev_api.rb23
-rw-r--r--lib/iev_api/client.rb103
-rw-r--r--lib/iev_api/configuration.rb58
-rw-r--r--lib/iev_api/middleware/raise_response_error.rb13
-rw-r--r--lib/iev_api/middleware/raise_server_error.rb18
5 files changed, 215 insertions, 0 deletions
diff --git a/lib/iev_api.rb b/lib/iev_api.rb
new file mode 100644
index 000000000..77a10d1ac
--- /dev/null
+++ b/lib/iev_api.rb
@@ -0,0 +1,23 @@
+require 'iev_api/configuration'
+
+module IevApi
+ extend Configuration
+
+ class IevError < StandardError; end
+
+ def self.client(options={})
+ IevApi::Client.new(options)
+ end
+
+ # Delegate to Instapaper::Client
+ def self.method_missing(method, *args, &block)
+ return super unless client.respond_to?(method)
+ client.send(method, *args, &block)
+ end
+
+ def self.respond_to?(method, include_private = false)
+ client.respond_to?(method, include_private) || super(method, include_private)
+ end
+end
+
+require 'iev_api/client'
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
diff --git a/lib/iev_api/configuration.rb b/lib/iev_api/configuration.rb
new file mode 100644
index 000000000..e01cbe102
--- /dev/null
+++ b/lib/iev_api/configuration.rb
@@ -0,0 +1,58 @@
+module IevApi
+ module Configuration
+ VALID_OPTIONS_KEYS = [
+ :account,
+ :auth_token,
+ :secure,
+ :connection_options,
+ :adapter,
+ :user_agent,
+ :middleware]
+
+ attr_accessor *VALID_OPTIONS_KEYS
+
+ DEFAULT_ADAPTER = :net_http
+ DEFAULT_USER_AGENT = "IEV Ruby Gem Api"
+ DEFAULT_CONNECTION_OPTIONS = {}
+ DEFAULT_MIDDLEWARE = [
+ Faraday::Request::UrlEncoded,
+ IevApi::Middleware::RaiseResponseError,
+ Faraday::Request::Multipart,
+ FaradayMiddleware::Mashify,
+ #FaradayMiddleware::Caching,
+ FaradayMiddleware::FollowRedirects,
+ FaradayMiddleware::ParseJson,
+ IevApi::Middleware::RaiseServerError,
+ ]
+
+ def self.extended(base)
+ base.reset
+ end
+
+ def configure(options={})
+ @account = options[:account] if options.has_key?(:account)
+ @auth_token = options[:auth_token] if options.has_key?(:auth_token)
+ @secure = options[:secure] if options.has_key?(:secure)
+ @middleware = options[:middleware] if options.has_key?(:middleware)
+ yield self if block_given?
+ self
+ end
+
+ def options
+ options = {}
+ VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
+ options
+ end
+
+ def reset
+ @account = nil
+ @auth_token = nil
+ @secure = false
+ @adapter = DEFAULT_ADAPTER
+ @user_agent = DEFAULT_USER_AGENT
+ @connection_options = DEFAULT_CONNECTION_OPTIONS
+ @middleware = DEFAULT_MIDDLEWARE
+ end
+
+ end
+end
diff --git a/lib/iev_api/middleware/raise_response_error.rb b/lib/iev_api/middleware/raise_response_error.rb
new file mode 100644
index 000000000..e299e3410
--- /dev/null
+++ b/lib/iev_api/middleware/raise_response_error.rb
@@ -0,0 +1,13 @@
+require 'faraday'
+
+module IevApi
+ module Middleware
+ class RaiseResponseError < Faraday::Response::Middleware
+
+ def on_complete(env)
+ raise IevError.new('No results found.') if env[:body].nil?
+ end
+
+ end
+ end
+end
diff --git a/lib/iev_api/middleware/raise_server_error.rb b/lib/iev_api/middleware/raise_server_error.rb
new file mode 100644
index 000000000..cb6f96f98
--- /dev/null
+++ b/lib/iev_api/middleware/raise_server_error.rb
@@ -0,0 +1,18 @@
+require 'faraday'
+
+module IevApi
+ module Middleware
+ class RaiseServerError < Faraday::Response::Middleware
+
+ def on_complete(env)
+ case env[:status].to_i
+ when 403
+ raise IevError.new('SSL should be enabled - use AirbrakeAPI.secure = true in configuration')
+ when 404
+ raise IevError.new('No resource found')
+ end
+ end
+
+ end
+ end
+end