Class: Rubyhexagon::API Private

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

Author:

  • Maxine Michalski

Since:

  • 0.4.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

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.

Author:

  • Maxine Michalski

Since:

  • 0.4.3



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_agentHash (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.

Returns:

  • (Hash)

    hash that can be used with open-uri

Since:

  • 0.4.3



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.

Parameters:

  • noun (String)

    the e621 noun (post, tag, user, artist, etc.)

  • action (String)

    action to be performed on noun (list, show, etc.)

  • query (Hash)

    a hash with extra information, which is dependent on noun and action

Returns:

  • (Hash)

    JSON parsed response.

Author:

  • Maxine Michalski

Since:

  • 0.4.3



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.)
    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

#lockObject

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.

Author:

  • Maxine Michalski

Since:

  • 0.4.3



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