Class: Rubyhexagon::API Private
- Inherits:
-
Object
- Object
- Rubyhexagon::API
- Defined in:
- lib/rubyhexagon/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.
General API helper class.
Not to be used directly!
Constant Summary collapse
- USER_AGENT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
User agent to send with every request
{ 'User-Agent' => "#{Rubyhexagon::NAME}/#{Rubyhexagon::VERSION} "\ '(by maxine_red on e621' }.freeze
- LOCK_PATH =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Lock path for locking file. This is needed to ensure API request limits.
'/var/lock/rubyhexagon.lock'
Class Method Summary collapse
-
.download(uri) ⇒ Tempfile
private
Download from a URI, needed for image download.
-
.fetch(noun, action, query) ⇒ Hash|Array
private
Fetch data from the e621 web interface.
Class Method Details
.download(uri) ⇒ Tempfile
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.
Download from a URI, needed for image download
64 65 66 67 68 69 70 71 72 |
# File 'lib/rubyhexagon/api.rb', line 64 def self.download(uri) raise ArgumentError, 'URI required' unless uri.is_a?(URI) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.port == 443 temp = Tempfile.new(File.basename(uri.path)) temp.write(http.get("#{uri.path}?#{uri.query}", USER_AGENT).body) temp.rewind temp end |
.fetch(noun, action, query) ⇒ Hash|Array
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.
Fetch data from the e621 web interface
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rubyhexagon/api.rb', line 44 def self.fetch(noun, action, query) unless noun.is_a?(Symbol) && action.is_a?(Symbol) && query.is_a?(Hash) raise ArgumentError, 'Two symbols and a hash are required!' end http = Net::HTTP.new('e621.net', 443) http.use_ssl = true query = query.map { |k, v| "#{k}=#{v}" }.join('&') lock do data = http.get("/#{noun}/#{action}.json?#{query}", USER_AGENT).body JSON.parse(data, symbolize_names: true) end end |