|
| 1 | +class RemoteModel |
| 2 | + class << self |
| 3 | + attr_accessor :host, :api_version, :max_concurrency, :cache |
| 4 | + |
| 5 | + def max_concurrency |
| 6 | + @max_concurrency ||= 1 |
| 7 | + end |
| 8 | + |
| 9 | + def api_version |
| 10 | + @api_version ||= '1' |
| 11 | + end |
| 12 | + |
| 13 | + def host |
| 14 | + raise "#{self.name}.host needs to be set" unless @host |
| 15 | + @host |
| 16 | + end |
| 17 | + |
| 18 | + def resource |
| 19 | + @resource ||= self.name.underscore.split('_').last |
| 20 | + end |
| 21 | + |
| 22 | + def resources |
| 23 | + resource.pluralize |
| 24 | + end |
| 25 | + |
| 26 | + def find_by_name(name) |
| 27 | + with_possible_cache(cache_key_for('find_by_name', name)) do |
| 28 | + Hashie::Mash.new( |
| 29 | + JSON.parse(Typhoeus.get("#{host}/api/v#{api_version}/#{resources}/#{name}").body) |
| 30 | + ) |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + def find_by_id(id) |
| 35 | + find_by_name(id) |
| 36 | + end |
| 37 | + |
| 38 | + def find_by_ids(ids, concurrent = false) |
| 39 | + return [] unless ids.size > 0 |
| 40 | + with_possible_cache(cache_key_for('find_by_ids', *ids)) do |
| 41 | + if !concurrent |
| 42 | + ids.map{ |id| find_by_id(id) } |
| 43 | + else |
| 44 | + requests = [] |
| 45 | + ids.each do |id| |
| 46 | + requests << Typhoeus::Request.new("#{host}/api/v#{api_version}/#{resources}/#{id}", followlocation: true) |
| 47 | + end |
| 48 | + get_concurrent_results(requests) |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + def destroy_by_name(name) |
| 54 | + Typhoeus::Request.delete("#{host}/api/v#{api_version}/#{resources}/#{name}", followlocation: true) |
| 55 | + end |
| 56 | + |
| 57 | + def destroy_by_id(id) |
| 58 | + destroy_by_name(id) |
| 59 | + end |
| 60 | + |
| 61 | + def destroy_by_ids(ids, concurrent = false) |
| 62 | + return [] unless ids.size > 0 |
| 63 | + if !concurrent |
| 64 | + ids.map{ |id| destroy_by_id(id) } |
| 65 | + else |
| 66 | + requests = [] |
| 67 | + ids.each do |id| |
| 68 | + requests << Typhoeus::Request.new("#{host}/api/v#{api_version}/#{resources}/#{id}", method: :delete, followlocation: true) |
| 69 | + end |
| 70 | + get_concurrent_results(requests) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + def get_concurrent_results(requests) |
| 75 | + return [] unless requests.size > 0 |
| 76 | + requests.each { |r| hydra.queue(r) } |
| 77 | + hydra.run |
| 78 | + requests.keep_if{ |req| req.response.code >= 200 && req.response.code < 300 } |
| 79 | + requests.map {|req| req.response.body.blank? ? [] : JSON.parse(req.response.body)}.flatten(1).map {|hsh| Hashie::Mash.new(hsh)} |
| 80 | + end |
| 81 | + |
| 82 | + private |
| 83 | + |
| 84 | + def with_possible_cache(cache_key) |
| 85 | + if cache |
| 86 | + cache.fetch(cache_key) do |
| 87 | + yield |
| 88 | + end |
| 89 | + else |
| 90 | + yield |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + def cache_key_for(*parts) |
| 95 | + ([resource, host, api_version] + parts).join('-') |
| 96 | + end |
| 97 | + |
| 98 | + def hydra |
| 99 | + @hydra ||= begin |
| 100 | + Typhoeus::Config.memoize = true |
| 101 | + Typhoeus::Hydra.new(max_concurrency: max_concurrency) |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | +end |
0 commit comments