Class: Rubyhexagon::API Private
- Inherits:
-
Object
- Object
- Rubyhexagon::API
- Defined in:
- lib/rubyhexagon/helper/api.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
An interface class, to unify calls from other code to e621 [dot] net. This class handles rate limiting, as well as provides a more uniform approach in making requests. It also ensures that a proper user agent is sent, as required by the e621 API.
Instance Attribute Summary collapse
-
#user_agent ⇒ Hash
readonly
private
User agent, used to identify this gem against e621.
Instance Method Summary collapse
-
#fetch(noun, action, query, method = 'GET') ⇒ Hash
private
Fetches information from e621 and returns data to caller.
-
#initialize ⇒ Object
constructor
private
Initializer for API.
-
#lock ⇒ Object
private
(see #fetch) A wrapper for actual requests, to minimize method length.
Constructor Details
#initialize ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializer for API. All methods are instance methods.
41 42 43 44 45 46 47 48 49 |
# File 'lib/rubyhexagon/helper/api.rb', line 41 def initialize @user_agent = { 'User-Agent' => "#{Rubyhexagon::NAME}/#{Rubyhexagon::VERSION} "\ '(by maxine_red on e621' } @http = Net::HTTP.new('e621.net', 443) @http.use_ssl = true lock_path = '/tmp/rubyhexagon.lock' @lock_file = File.open(lock_path, 'a', 0o666) end |
Instance Attribute Details
#user_agent ⇒ Hash (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
User agent, used to identify this gem against e621.
34 35 36 |
# File 'lib/rubyhexagon/helper/api.rb', line 34 def user_agent @user_agent end |
Instance Method Details
#fetch(noun, action, query, method = 'GET') ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Fetches information from e621 and returns data to caller. This also takes into account rate limiting.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rubyhexagon/helper/api.rb', line 62 def fetch(noun, action, query, method = 'GET') if Login.instance.setup? query.store(:login, Login.instance.login) query.store(:password_hash, Login.instance.password_hash) end query = query.map { |k, v| "#{k}=#{v}" }.join('&') lock do data = if method == 'GET' @http.get("/#{noun}/#{action}.json?#{query}", @user_agent).body else @http.post("/#{noun}/#{action}.json", query, @user_agent).body end JSON.parse(data, symbolize_names: true) end end |
#lock ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
(see #fetch) A wrapper for actual requests, to minimize method length.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rubyhexagon/helper/api.rb', line 82 def lock @lock_file.flock(File::LOCK_EX) @lock_file.truncate(0) @lock_file.print $PROCESS_ID begin s = Time.now data = yield # There is a hard limit of 2 requests per second, but to be nice we wait # a whole second between requests. w = 1 - (Time.now - s) sleep w if w.positive? ensure @lock_file.flock(File::LOCK_UN) end data end |