Class: GenderizeIoRb

Inherits:
Object
  • Object
show all
Defined in:
lib/genderize_io_rb.rb

Constant Summary collapse

INITIALIZE_VALID_ARGS =
[:cache_as, :cache_db, :debug]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
32
33
34
35
36
37
38
39
40
# 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

  @debug = args[:debug]
  @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]

  if block_given?
    begin
      yield self
    ensure
      destroy
    end
  end
end

Instance Attribute Details

#cache_dbObject (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

Raises:

  • (LoadError)


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

#destroyObject



145
146
147
148
# File 'lib/genderize_io_rb.rb', line 145

def destroy
  @http.destroy
  @destroyed = true
end

#destroyed?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/genderize_io_rb.rb', line 150

def destroyed?
  return @destroyed
end

#info_for_name(name) ⇒ Object



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
143
# File 'lib/genderize_io_rb.rb', line 101

def info_for_name(name)
  name_lc = name.to_s.strip.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,
        from_cache_db: true
      )
    end
  end

  if @cache_as
    cache_as_res = @cache_as.read(cache_key_for_name(name_lc))

    if cache_as_res
      res = ::GenderizeIoRb::Result.new(
        data: JSON.parse(cache_as_res),
        genderize_io_rb: self,
        from_cache_as: true
      )
    end
  end

  unless res
    http_res = @http.get("?name=#{CGI.escape(name_lc)}")
    json_res = JSON.parse(http_res.body)

    raise GenderizeIoRb::Errors::NameNotFound, "Name was not found on Genderize.io: '#{name_lc}'." unless json_res["gender"]

    res = ::GenderizeIoRb::Result.new(
      data: json_res,
      genderize_io_rb: self,
      from_http_request: true
    )
    store_cache_for_name(name_lc, json_res)
  end

  return res
end

#info_for_names(names, &blk) ⇒ Object



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
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/genderize_io_rb.rb', line 42

def info_for_names(names, &blk)
  names_lc = names.map{ |name| name.to_s.strip.downcase }
  results = []

  extract_cache_db_results_from_names(names) if @cache_db
  if @cache_db
    debug "Looking names up in db: #{names_lc}" if @debug
    @cache_db.select(:genderize_io_rb_cache, name: names_lc) do |data|
      debug "Found in db-cache: #{data}" if @debug

      result = ::GenderizeIoRb::Result.new(
        data: JSON.parse(data[:result]),
        genderize_io_rb: self,
        from_cache_db: true
      )

      raise "Could not delete name: #{data[:name]}" unless names_lc.delete(data[:name]) == data[:name]

      handle_result(result, results, blk)
    end
  end

  unless names_lc.empty?
    debug "Looking names up using an HTTP request: #{names_lc}" if @debug
    urls = generate_urls_from_names(names_lc)

    # Request all the URL's.
    urls.each do |url|
      http_result = @http.get(url)
      json_results = JSON.parse(http_result.body)

      json_results.each do |json_result|
        if json_result["gender"] == nil
          error = GenderizeIoRb::Errors::NameNotFound.new("Name was not found on Genderize.io: '#{json_result["name"]}'.")
          error.name = json_result["name"]

          handle_result(error, results, blk)
        else
          store_cache_for_name(json_result["name"], json_result)

          result = ::GenderizeIoRb::Result.new(
            data: json_result,
            genderize_io_rb: self,
            from_http_request: true
          )

          handle_result(result, results, blk)
        end
      end
    end
  end

  if blk
    return nil
  else
    return results
  end
end