class OneEye::API
Public Class Methods
debug(options={})
click to toggle source
# File lib/api.rb, line 16 def self.debug(options={}) OneEye::API.debug_output $stdout unless options[:stop] OneEye::API.debug_output $stderr if options[:stop] end
http_methods()
click to toggle source
# File lib/api.rb, line 8 def self.http_methods [:get, :post, :put, :patch, :delete] end
new(api_token=ENV["ONE_EYE_API_KEY"])
click to toggle source
# File lib/api.rb, line 12 def initialize(api_token=ENV["ONE_EYE_API_KEY"]) @options = { query: {api_token: api_token} } end
Public Instance Methods
handle_errors(&block)
click to toggle source
# File lib/api.rb, line 21 def handle_errors(&block) @response = block.call @code_level = @response.code.to_s[0] if @code_level == "4" || @code_level == "5" def @response.to_hash {:status_code => @response.code, :error => @response.message} end end @response end
Private Instance Methods
method_missing(name, options={})
click to toggle source
# File lib/api.rb, line 42 def method_missing(name, options={}) name, options = standardize_request(:get, name, options) self.class.get name, options end
standardize_options(options={})
click to toggle source
# File lib/api.rb, line 58 def standardize_options(options={}) if options.class == Fixnum options = {id: options} end options.each { |k, v| @options[k] ||= v } @options[:query] = @options[:query].merge(options) @options[:body] = options.to_json @options[:headers] = {"Content-Type"=>"application/json"} symbolize_keys(@options) end
standardize_request(http_method, name, options)
click to toggle source
# File lib/api.rb, line 47 def standardize_request(http_method, name, options) name = "/#{name}" options = standardize_options(options) name += "/#{options[:id]}" if options.keys.include? :id [name, options] end
symbolize_keys(hash)
click to toggle source
# File lib/api.rb, line 54 def symbolize_keys(hash) hash.inject({}) { |h, (k, v)| h[k.to_sym] = v; h } end