Class: Rubyhexagon::API Private

Inherits:
Object
  • Object
show all
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!

Author:

  • Maxine Michalski

Since:

  • 0.4.3

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

Since:

  • 0.4.3

{ '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.

Since:

  • 0.4.3

'/var/lock/rubyhexagon.lock'

Class Method Summary collapse

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

Parameters:

  • uri (URI)

    URI to download

Returns:

  • (Tempfile)

    A temporary file, that holds downloaded data.

Raises:

  • (ArgumentError)

Author:

  • Maxine Michalski

Since:

  • 0.4.3



71
72
73
74
75
76
77
78
79
# File 'lib/rubyhexagon/api.rb', line 71

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, 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

Parameters:

  • noun (Symbol)

    first part of interface, like ‘post’, ‘tag’, etc.

  • action (Symbol)

    second part of interface, like ‘show’, ‘index’

  • query (Hash)

    query hash for interface

Returns:

  • (Hash|Array)

    A decoded JSON string

Author:

  • Maxine Michalski

Since:

  • 0.4.3



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubyhexagon/api.rb', line 44

def self.fetch(noun, query)
  unless noun.is_a?(Symbol) && query.is_a?(Hash)
    raise ArgumentError, 'A symbol and a hash are required!'
  end
  http = Net::HTTP.new('e621.net', 443)
  http.use_ssl = true
  if query.key?(:id)
    lock do
      data = http.get("/#{noun}/#{query[:id]}.json", USER_AGENT).body
      JSON.parse(data, symbolize_names: true)
    end
  else
    query = query.map { |k, v| "#{k}=#{v}" }.join('&')
    lock do
      data = http.get("/#{noun}.json?#{query}", USER_AGENT).body
      JSON.parse(data, symbolize_names: true)
    end
  end
end