Class: Datmachine::Client
- Inherits:
-
Object
- Object
- Datmachine::Client
- Defined in:
- lib/datmachine/client.rb
Constant Summary collapse
- DEFAULTS =
{ :scheme => 'http', :host => 'api.datmachine.co/', :port => 80, :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
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#config ⇒ Object
Returns the value of attribute config.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
Instance Method Summary collapse
- #build_conn ⇒ Object
-
#initialize(api_key, site_id, options = {}) ⇒ Client
constructor
A new instance of Client.
- #method_missing(method, *args, &block) ⇒ Object
- #unstore(*args, &block) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(api_key, site_id, options = {}) ⇒ Client
Returns a new instance of Client.
27 28 29 30 31 32 |
# File 'lib/datmachine/client.rb', line 27 def initialize(api_key, site_id, ={}) @api_key = api_key.nil? ? api_key : api_key.strip @config = DEFAULTS.merge @config[:site_id] = site_id build_conn end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/datmachine/client.rb', line 79 def method_missing(method, *args, &block) if is_http_method? method conn.token_auth(api_key) unless api_key.nil? args[0] = "/sites/#{config[:site_id]}/" + args[0] conn.send method, *args else super method, *args, &block end end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
25 26 27 |
# File 'lib/datmachine/client.rb', line 25 def api_key @api_key end |
#config ⇒ Object
Returns the value of attribute config.
25 26 27 |
# File 'lib/datmachine/client.rb', line 25 def config @config end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
24 25 26 |
# File 'lib/datmachine/client.rb', line 24 def conn @conn end |
Instance Method Details
#build_conn ⇒ Object
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 |
# File 'lib/datmachine/client.rb', line 34 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 = { :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 :json cxn.response :raise_error # raise exceptions on 40x, 50x responses cxn.adapter config[:faraday_adapter] end conn.path_prefix = '/' conn.headers['User-Agent'] = "datmachine-ruby/1" conn.headers['Content-Type'] = "application/json" conn.headers['Accept'] = "#{@config[:accept_type]}" end |
#unstore(*args, &block) ⇒ Object
75 76 77 |
# File 'lib/datmachine/client.rb', line 75 def unstore(*args, &block) delete(*args, &block) end |
#url ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/datmachine/client.rb', line 67 def url builder = (config[:scheme] == 'http') ? URI::HTTP : URI::HTTPS builder.build({:host => config[:host], :port => config[:port], :scheme => config[:scheme]}) end |