Class: GenderizeIoRb
- Inherits:
-
Object
- Object
- GenderizeIoRb
- Defined in:
- lib/genderize_io_rb.rb
Constant Summary collapse
- INITIALIZE_VALID_ARGS =
[:cache_as, :cache_db]
Instance Attribute Summary collapse
-
#cache_db ⇒ Object
readonly
Returns the value of attribute cache_db.
Class Method Summary collapse
Instance Method Summary collapse
- #destroy ⇒ Object
- #info_for_name(name) ⇒ Object
-
#initialize(args = {}) ⇒ GenderizeIoRb
constructor
A new instance of GenderizeIoRb.
Constructor Details
#initialize(args = {}) ⇒ GenderizeIoRb
Returns a new instance of GenderizeIoRb.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/genderize_io_rb.rb', line 16 def initialize(args = {}) args.each do |key, val| raise "Invalid key: '#{key}'." unless INITIALIZE_VALID_ARGS.include?(key) end @args = args @http = Http2.new(:host => "api.genderize.io") # Make sure the database-version is up-to-date. @cache_db = args[:cache_db] if @cache_db Baza::Revision.new.init_db(:db => @cache_db, :schema => GenderizeIoRb::DatabaseSchema::SCHEMA) end @cache_as = args[:cache_as] end |
Instance Attribute Details
#cache_db ⇒ Object (readonly)
Returns the value of attribute cache_db.
7 8 9 |
# File 'lib/genderize_io_rb.rb', line 7 def cache_db @cache_db end |
Class Method Details
.const_missing(name) ⇒ Object
9 10 11 12 13 |
# File 'lib/genderize_io_rb.rb', line 9 def self.const_missing(name) require_relative "../include/#{::StringCases.camel_to_snake(name)}" raise LoadError, "Still not defined: '#{name}'." unless ::GenderizeIoRb.const_defined?(name) return ::GenderizeIoRb.const_get(name) end |
Instance Method Details
#destroy ⇒ Object
89 90 91 |
# File 'lib/genderize_io_rb.rb', line 89 def destroy @http.close end |
#info_for_name(name) ⇒ 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/genderize_io_rb.rb', line 33 def info_for_name(name) name = name.to_s.strip name_lc = name.downcase # If a database-cache is enabled, try to look result up there first. if @cache_db cache_db_res = @cache_db.single(:genderize_io_rb_cache, {:name => name_lc}) if cache_db_res res = ::GenderizeIoRb::Result.new( :data => JSON.parse(cache_db_res[:result]), :genderize_io_rb => self ) end end cache_as_key = "genderize_io_rb_#{name_lc}" if @cache_as cache_as_res = @cache_as.read(cache_as_key) puts "CacheAsRes: #{cache_as_res}" if cache_as_res res = ::GenderizeIoRb::Result.new( :data => JSON.parse(cache_as_res), :genderize_io_rb => self ) end end unless res http_res = @http.get("?name=#{CGI.escape(name)}") json_res = JSON.parse(http_res.body) raise GenderizeIoRb::Errors::NameNotFound, "Name was not found on Genderize.io: '#{name}'." unless json_res["gender"] res = ::GenderizeIoRb::Result.new( :data => json_res, :genderize_io_rb => self ) end # Save result to the database cache. if @cache_db && !cache_db_res @cache_db.insert(:genderize_io_rb_cache, { :name => name_lc, :result => http_res.body, :created_at => Time.now }) end if @cache_as && !cache_as_res @cache_as.write(cache_as_key, http_res.body) end return {:result => res, :cache_db => @cache_db_res, :cache_as => cache_as_res} end |