Class: E621::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

#initialize(rspec = false) ⇒ 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.

Author:

  • Maxine Michalski

Since:

  • 0.4.3



39
40
41
42
43
44
45
46
47
48
# File 'lib/rubyhexagon/helper/api.rb', line 39

def initialize(rspec = false)
  @user_agent = { 'User-Agent' =>
                 "#{E621::NAME}/#{E621::VERSION} (by maxine_red on e621" }
  @rspec = rspec
  @base = if @rspec || !defined? RSpec then 'https://e621.net'
          else 'spec/data'
          end
  lock_path = '/tmp/rubyhexagon.lock'
  @lock_file = File.open(lock_path, 'a')
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



32
33
34
# File 'lib/rubyhexagon/helper/api.rb', line 32

def user_agent
  @user_agent
end

Instance Method Details

#fetch(noun, action, query) ⇒ 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



61
62
63
64
65
66
# File 'lib/rubyhexagon/helper/api.rb', line 61

def fetch(noun, action, query)
  query = query.to_a.map { |q| "#{q.first}=#{q.last}" }.join('&')
  query = "?#{query}" unless query == ''
  uri = "#{@base}/#{noun}/#{action}.json#{query}"
  fetch_data(uri)
end