Class: ElvantoAPI::Client
- Inherits:
-
Object
- Object
- ElvantoAPI::Client
- Defined in:
- lib/elvanto/client.rb
Constant Summary collapse
- DEFAULTS =
{ :scheme => 'http', :host => 'localhost', :api_version => nil, :port => 3000, :version => '1', :logging_level => 'WARN', :connection_timeout => 60, :read_timeout => 60, :logger => nil, :ssl_verify => false, :faraday_adapter => Faraday.default_adapter, :accept_type => 'application/json' }
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#config ⇒ Object
Returns the value of attribute config.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
Instance Method Summary collapse
- #api_version ⇒ Object
- #build_conn ⇒ Object
- #get(href, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #post(href, options = {}) ⇒ Object
-
#url ⇒ Object
Building the host url of the API Endpoint.
Constructor Details
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
26 27 28 |
# File 'lib/elvanto/client.rb', line 26 def access_token @access_token end |
#config ⇒ Object
Returns the value of attribute config.
26 27 28 |
# File 'lib/elvanto/client.rb', line 26 def config @config end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
25 26 27 |
# File 'lib/elvanto/client.rb', line 25 def conn @conn end |
Instance Method Details
#api_version ⇒ Object
80 81 82 83 |
# File 'lib/elvanto/client.rb', line 80 def api_version return "" unless ElvantoAPI.config[:api_version] ElvantoAPI.config[:api_version] + "/" end |
#build_conn ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/elvanto/client.rb', line 33 def build_conn if config[:logger] logger = config[:logger] else logger = Logger.new(STDOUT) logger.level = Logger.const_get(config[:logging_level].to_s) end Faraday::Response.register_middleware :handle_elvanto_errors => lambda { Faraday::Response::RaiseElvantoError } = { :request => { :open_timeout => config[:connection_timeout], :timeout => config[:read_timeout] }, :ssl => { :verify => @config[:ssl_verify] # Only set this to false for testing } } @conn = Faraday.new(url, ) do |cxn| cxn.request :json cxn.response :logger, logger cxn.response :handle_elvanto_errors cxn.response :json #cxn.response :raise_error # raise exceptions on 40x, 50x responses cxn.adapter config[:faraday_adapter] end conn.path_prefix = '/' conn.headers['User-Agent'] = "elvanto-ruby/" + config[:version] if config[:access_token] # Authenticating with OAuth conn.headers["Authorization"] = "Bearer " + config[:access_token] elsif config[:api_key] # Authenticating with an API key conn.basic_auth(config[:api_key], '') end end |
#get(href, options = {}) ⇒ Object
90 91 92 |
# File 'lib/elvanto/client.rb', line 90 def get(href, ={}) conn.get href, end |
#post(href, options = {}) ⇒ Object
85 86 87 88 |
# File 'lib/elvanto/client.rb', line 85 def post(href, ={}) uri = api_version + href + "." + config[:accept] conn.post uri, end |
#url ⇒ Object
Building the host url of the API Endpoint
75 76 77 78 |
# File 'lib/elvanto/client.rb', line 75 def url builder = (config[:scheme] == 'http') ? URI::HTTP : URI::HTTPS builder.build({:host => config[:host],:port => config[:port],:scheme => config[:scheme]}) end |