Class: APIConsumer
- Inherits:
-
Object
- Object
- APIConsumer
- Defined in:
- lib/api_consumer.rb
Constant Summary collapse
- DEFAULT_REQUEST_OPTS =
{ :method => :get, :headers => { "Accept" => "application/json", "Content-Type" => "application/json", "User-Agent" => "API-CONSUMER-#{ENV['RACK_ENV'] || 'dev'}" }, :ttl => 300 }
Class Method Summary collapse
- .cache ⇒ Object
- .connection(connection_flag = :normal) ⇒ Object
- .create_connection(debug = false) ⇒ Object
- .do_request(path, conn, opts = {}, &blk) ⇒ Object
- .inherited(subclass) ⇒ Object
- .log ⇒ Object
- .memcache? ⇒ Boolean
- .memcache_hosts ⇒ Object
- .memcache_settings ⇒ Object
- .set(key, val) ⇒ Object
- .set_log_level(level = nil) ⇒ Object
- .set_logger(logger, level = nil) ⇒ Object
- .settings ⇒ Object
Class Method Details
.cache ⇒ Object
166 167 168 |
# File 'lib/api_consumer.rb', line 166 def cache @cache ||= UberCache.new(settings[:cache_prefix], settings[:memcache_hosts], memcache_settings) end |
.connection(connection_flag = :normal) ⇒ Object
144 145 146 147 148 |
# File 'lib/api_consumer.rb', line 144 def connection(connection_flag = :normal) @connections ||= {} return @connections[connection_flag] if @connections[connection_flag] @connections[connection_flag] = create_connection end |
.create_connection(debug = false) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/api_consumer.rb', line 150 def create_connection(debug = false) if @uri.nil? || @uri.port.nil? log.info "CONNECTING TO: #{settings[:url]}" @uri = URI.parse("#{settings[:url]}/") end http = Net::HTTP.new(@uri.host, @uri.port) if settings[:ssl] == true http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.set_debug_output $stderr if debug http.open_timeout = 7 http.read_timeout = 15 http end |
.do_request(path, conn, opts = {}, &blk) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/api_consumer.rb', line 88 def do_request(path, conn, opts = {}, &blk) if(opts[:verbose]) log.debug("Sending request to: #{conn.address}#{':' + conn.port.to_s if conn.port}#{path}") end if opts[:key] # cache if key sent read_val = nil return read_val if !opts[:reload] && read_val = cache.obj_read(opts[:key]) opts[:ttl] ||= settings[:ttl] || DEFAULT_REQUEST_OPTS[:ttl] end opts[:headers] = DEFAULT_REQUEST_OPTS[:headers].merge(opts[:headers] || {}) opts[:method] = opts[:method] || DEFAULT_REQUEST_OPTS[:method] path = decorate_path( path ) req = if( opts[:method] == :get) Net::HTTP::Get.new(path) elsif( opts[:method] == :post) Net::HTTP::Post.new(path) elsif( opts[:method] == :delete) Net::HTTP::Delete.new(path) elsif( opts[:method] == :put) Net::HTTP::Put.new(path) else log.error "BUG - method=>(#{opts[:method]})" end opts[:headers].each { |k,v| req[k] = v } settings[:headers].each { |k,v| req[k] = v } if settings[:headers] req.basic_auth settings[:api_user], settings[:api_password] if settings[:api_user] && settings[:api_password] #req["connection"] = 'keep-alive' req.body = opts[:body] if opts[:body] response = nil begin log.debug "CONN:" + conn.inspect log.debug "REQ:" + req.inspect response = conn.request(req) results = JSON.parse(response.body) accept_codes = [200, 201, 202] accept_codes += settings[:accept_codes].map(&:to_i) if settings[:accept_codes] if !accept_codes.include?(response.code.to_i) results = error_code(response.code, opts[:errors], results) end results = blk.call(results) if blk cache.obj_write(opts[:key], results, :ttl => opts[:ttl]) if opts[:key] return results rescue Exception => exception log.error exception. log.error exception.backtrace return error_code(response ? response.code : "NO CODE" , opts[:errors]) end data = response.body data = blk.call(data) if blk cache.obj_write(opts[:key], data, :ttl => opts[:ttl]) if opts[:key] return data end |
.inherited(subclass) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/api_consumer.rb', line 12 def inherited(subclass) if File.exist?("config/#{snake_case(subclass)}.yml") yml_template = ERB.new File.new("config/#{snake_case(subclass)}.yml").read configs = YAML.load yml_template.result(binding) configs[snake_case(subclass)].each{ |k,v| subclass.set(k.to_sym, v) } subclass.set_logger(Logger.new(subclass.settings[:log_file] || "./log/#{snake_case(subclass)}_api.log"), subclass.settings[:log_level]) super else raise RuntimeError, "Please create config file: 'config/#{snake_case(subclass)}.yml'" end end |
.log ⇒ Object
29 30 31 |
# File 'lib/api_consumer.rb', line 29 def log @logger end |
.memcache? ⇒ Boolean
55 56 57 |
# File 'lib/api_consumer.rb', line 55 def memcache? settings[:use_memcache] end |
.memcache_hosts ⇒ Object
59 60 61 |
# File 'lib/api_consumer.rb', line 59 def memcache_hosts settings[:memcache_hosts] end |
.memcache_settings ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/api_consumer.rb', line 63 def memcache_settings mem_settings = (settings[:memcache_settings] || {}) mem_settings.keys.each do |key| mem_settings[(key.to_sym rescue key) || key] = mem_settings.delete(key) end mem_settings end |
.set(key, val) ⇒ Object
71 72 73 |
# File 'lib/api_consumer.rb', line 71 def set(key, val) settings[key] = val end |
.set_log_level(level = nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/api_consumer.rb', line 33 def set_log_level(level=nil) if level.nil? level = if([nil, "development", "test"].include?(ENV['RACK_ENV'])) :info else :warn end end @logger.level = case level.to_sym when :debug Logger::DEBUG when :info Logger::INFO when :error Logger::ERROR when :fatal Logger::FATAL else #warn Logger::WARN end end |
.set_logger(logger, level = nil) ⇒ Object
24 25 26 27 |
# File 'lib/api_consumer.rb', line 24 def set_logger(logger, level=nil) @logger = logger.nil? ? Logger.new(STDERR) : logger set_log_level(level) end |
.settings ⇒ Object
75 76 77 |
# File 'lib/api_consumer.rb', line 75 def settings @settings ||= {} end |