bridge_api 0.1.53 → 0.1.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/lib/bridge_api.rb +14 -1
 - data/lib/bridge_api/client.rb +24 -17
 - data/lib/bridge_api/version.rb +1 -1
 - metadata +2 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 2d39d608d1b30d380fb6fdb79ad8284d5c2e2e1fd132623b89717cf94a9f9071
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: a8a72c682e3868563aa290f61b1dabb3295e34aff8ef6d9c61cff9f927a32bba
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 8b93008180e1bdb8af16f4fbc88f4fa642cbe20e655fcac04dc34ea0c5ba20b1f38ef3b5c32e098224f48da463a7e7288fc7726c57232fc6e5c666d741c505b4
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 10af8a2e42a4ca6db28abdeba4258bf6559a4b6f4ffc3907a0fe3d0f8820015a82b92a2001e08ae25cc3af2bd0653f4de58526f0301db6f0320db96d65a11d0a
         
     | 
    
        data/lib/bridge_api.rb
    CHANGED
    
    | 
         @@ -4,7 +4,8 @@ require 'bridge_api/client' 
     | 
|
| 
       4 
4 
     | 
    
         
             
            module BridgeAPI
         
     | 
| 
       5 
5 
     | 
    
         
             
              class << self
         
     | 
| 
       6 
6 
     | 
    
         
             
                require 'logging'
         
     | 
| 
       7 
     | 
    
         
            -
                attr_writer :enforce_rate_limits, :rate_limit_min, :rate_limits, :max_sleep_seconds, :min_sleep_seconds, 
     | 
| 
      
 7 
     | 
    
         
            +
                attr_writer :enforce_rate_limits, :rate_limit_min, :rate_limits, :max_sleep_seconds, :min_sleep_seconds,
         
     | 
| 
      
 8 
     | 
    
         
            +
                            :logger, :master_rate_limit, :master_mutex, :rate_limit_threshold
         
     | 
| 
       8 
9 
     | 
    
         | 
| 
       9 
10 
     | 
    
         
             
                def configure
         
     | 
| 
       10 
11 
     | 
    
         
             
                  yield self if block_given?
         
     | 
| 
         @@ -26,6 +27,18 @@ module BridgeAPI 
     | 
|
| 
       26 
27 
     | 
    
         
             
                  @max_sleep_seconds ||= 60
         
     | 
| 
       27 
28 
     | 
    
         
             
                end
         
     | 
| 
       28 
29 
     | 
    
         | 
| 
      
 30 
     | 
    
         
            +
                def master_rate_limit
         
     | 
| 
      
 31 
     | 
    
         
            +
                  @master_rate_limit ||= false
         
     | 
| 
      
 32 
     | 
    
         
            +
                end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                def master_mutex
         
     | 
| 
      
 35 
     | 
    
         
            +
                  @master_mutex ||= Mutex.new
         
     | 
| 
      
 36 
     | 
    
         
            +
                end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                def rate_limit_threshold
         
     | 
| 
      
 39 
     | 
    
         
            +
                  @rate_limit_threshold ||= 10
         
     | 
| 
      
 40 
     | 
    
         
            +
                end
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
       29 
42 
     | 
    
         
             
                def logger
         
     | 
| 
       30 
43 
     | 
    
         
             
                  return @logger if defined? @logger
         
     | 
| 
       31 
44 
     | 
    
         
             
                  @logger = Logging.logger(STDOUT)
         
     | 
    
        data/lib/bridge_api/client.rb
    CHANGED
    
    | 
         @@ -60,42 +60,49 @@ module BridgeAPI 
     | 
|
| 
       60 
60 
     | 
    
         
             
                end
         
     | 
| 
       61 
61 
     | 
    
         | 
| 
       62 
62 
     | 
    
         
             
                def enforce_rate_limits
         
     | 
| 
       63 
     | 
    
         
            -
                  return unless BridgeAPI.enforce_rate_limits &&  
     | 
| 
       64 
     | 
    
         
            -
                  return unless  
     | 
| 
       65 
     | 
    
         
            -
             
     | 
| 
       66 
     | 
    
         
            -
                  BridgeAPI.logger.debug("Current LIMIT: #{current_limit}")
         
     | 
| 
       67 
     | 
    
         
            -
                  tts = ((BridgeAPI.rate_limit_min - current_limit) / 5).ceil
         
     | 
| 
      
 63 
     | 
    
         
            +
                  return unless BridgeAPI.enforce_rate_limits && limit_remaining.present?
         
     | 
| 
      
 64 
     | 
    
         
            +
                  return unless limit_remaining < BridgeAPI.rate_limit_threshold
         
     | 
| 
      
 65 
     | 
    
         
            +
                  tts = ((BridgeAPI.rate_limit_min - limit_remaining) / 5).ceil
         
     | 
| 
       68 
66 
     | 
    
         
             
                  tts = BridgeAPI.min_sleep_seconds if tts < BridgeAPI.min_sleep_seconds
         
     | 
| 
       69 
67 
     | 
    
         
             
                  tts = BridgeAPI.max_sleep_seconds if tts > BridgeAPI.max_sleep_seconds
         
     | 
| 
       70 
68 
     | 
    
         
             
                  message = "Bridge API rate limit minimum #{BridgeAPI.rate_limit_min} reached for key: '#{config[:api_key]}'. "\
         
     | 
| 
       71 
69 
     | 
    
         
             
                    "Sleeping for #{tts} second(s) to catch up ~zzZZ~. "\
         
     | 
| 
       72 
     | 
    
         
            -
                    "Limit Remaining: #{ 
     | 
| 
      
 70 
     | 
    
         
            +
                    "Limit Remaining: #{limit_remaining}"
         
     | 
| 
       73 
71 
     | 
    
         
             
                  BridgeAPI.logger.debug(message)
         
     | 
| 
       74 
72 
     | 
    
         
             
                  sleep(tts)
         
     | 
| 
       75 
73 
     | 
    
         
             
                end
         
     | 
| 
       76 
74 
     | 
    
         | 
| 
      
 75 
     | 
    
         
            +
                def using_master_rate_limit?
         
     | 
| 
      
 76 
     | 
    
         
            +
                  config[:master_rate_limit].present? || BridgeAPI.master_rate_limit.present?
         
     | 
| 
      
 77 
     | 
    
         
            +
                end
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
       77 
79 
     | 
    
         
             
                def apply_rate_limits(response)
         
     | 
| 
       78 
80 
     | 
    
         
             
                  limit = response.headers['x-rate-limit-remaining']
         
     | 
| 
       79 
81 
     | 
    
         
             
                  return if limit.nil?
         
     | 
| 
       80 
     | 
    
         
            -
                   
     | 
| 
      
 82 
     | 
    
         
            +
                  BridgeAPI.logger.debug("BRIDGE RATE LIMIT REMAINING: #{limit}")
         
     | 
| 
      
 83 
     | 
    
         
            +
                  self.limit_remaining = limit.to_i
         
     | 
| 
       81 
84 
     | 
    
         
             
                end
         
     | 
| 
       82 
85 
     | 
    
         | 
| 
       83 
     | 
    
         
            -
                def  
     | 
| 
       84 
     | 
    
         
            -
                  if  
     | 
| 
       85 
     | 
    
         
            -
                     
     | 
| 
       86 
     | 
    
         
            -
             
     | 
| 
       87 
     | 
    
         
            -
                       
     | 
| 
       88 
     | 
    
         
            -
             
     | 
| 
      
 86 
     | 
    
         
            +
                def limit_remaining
         
     | 
| 
      
 87 
     | 
    
         
            +
                  if using_master_rate_limit?
         
     | 
| 
      
 88 
     | 
    
         
            +
                    BridgeAPI.master_mutex.synchronize do
         
     | 
| 
      
 89 
     | 
    
         
            +
                      limit = PaulWalker::RateLimit.get(config[:api_key], config[:api_key])
         
     | 
| 
      
 90 
     | 
    
         
            +
                      if limit.nil?
         
     | 
| 
      
 91 
     | 
    
         
            +
                        PaulWalker::RateLimit.add(config[:api_key], config[:api_key], 0, BridgeAPI::rate_limit_min)
         
     | 
| 
      
 92 
     | 
    
         
            +
                        limit = {current: 0}.with_indifferent_access
         
     | 
| 
      
 93 
     | 
    
         
            +
                      end
         
     | 
| 
      
 94 
     | 
    
         
            +
                      limit['current']
         
     | 
| 
       89 
95 
     | 
    
         
             
                    end
         
     | 
| 
       90 
     | 
    
         
            -
                    limit['current']
         
     | 
| 
       91 
96 
     | 
    
         
             
                  else
         
     | 
| 
       92 
97 
     | 
    
         
             
                    BridgeAPI.rate_limits[config[:api_key]]
         
     | 
| 
       93 
98 
     | 
    
         
             
                  end
         
     | 
| 
       94 
99 
     | 
    
         
             
                end
         
     | 
| 
       95 
100 
     | 
    
         | 
| 
       96 
     | 
    
         
            -
                def  
     | 
| 
       97 
     | 
    
         
            -
                  if  
     | 
| 
       98 
     | 
    
         
            -
                     
     | 
| 
      
 101 
     | 
    
         
            +
                def limit_remaining=(value)
         
     | 
| 
      
 102 
     | 
    
         
            +
                  if using_master_rate_limit?
         
     | 
| 
      
 103 
     | 
    
         
            +
                    BridgeAPI.master_mutex.synchronize do
         
     | 
| 
      
 104 
     | 
    
         
            +
                      PaulWalker::RateLimit.add(config[:api_key], config[:api_key], value, BridgeAPI::rate_limit_min)
         
     | 
| 
      
 105 
     | 
    
         
            +
                    end
         
     | 
| 
       99 
106 
     | 
    
         
             
                  else
         
     | 
| 
       100 
107 
     | 
    
         
             
                    BridgeAPI.rate_limits[config[:api_key]] = value
         
     | 
| 
       101 
108 
     | 
    
         
             
                  end
         
     | 
    
        data/lib/bridge_api/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: bridge_api
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.54
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Jay Shaffer
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2019-01- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2019-01-23 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: bundler
         
     |