Class: AnkiConnect::Client

Inherits:
Object
  • Object
show all
Includes:
Cards, Decks, Graphical, Media, Miscellaneous, Models, Notes, Statistics
Defined in:
lib/anki_connect/client.rb,
lib/anki_connect/cards.rb,
lib/anki_connect/decks.rb,
lib/anki_connect/media.rb,
lib/anki_connect/notes.rb,
lib/anki_connect/models.rb,
lib/anki_connect/graphical.rb,
lib/anki_connect/statistics.rb,
lib/anki_connect/miscellaneous.rb

Overview

Main client class that includes all API modules and provides the core request mechanism.

Defined Under Namespace

Modules: Cards, Decks, Graphical, Media, Miscellaneous, Models, Notes, Statistics

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Miscellaneous

#active_profile, #api_reflect, #export_deck, #import_deck, #load_profile, #multi, #profiles, #reload_collection, #request_permission, #sync, #version

Methods included from Statistics

#cards_reviewed_by_day, #cards_reviewed_today, #collection_stats_html, #get_reviews, #get_reviews_for_cards, #insert_reviews, #latest_review_time

Methods included from Graphical

#gui_add_cards, #gui_add_note_set_data, #gui_answer_card, #gui_browse, #gui_check_database, #gui_current_card, #gui_deck_browser, #gui_deck_overview, #gui_deck_review, #gui_edit_note, #gui_exit_anki, #gui_import_file, #gui_play_audio, #gui_select_card, #gui_selected_notes, #gui_show_answer, #gui_show_question, #gui_start_card_timer, #gui_undo

Methods included from Media

#delete_media, #list_media, #media_dir_path, #retrieve_media, #store_media

Methods included from Notes

#add_note, #add_notes, #add_tags, #all_tags, #can_add_notes, #change_note_model, #clear_unused_tags, #delete_notes, #get_note_tags, #get_notes, #get_notes_mod_time, #remove_empty_notes, #remove_tags, #replace_tag, #search_notes, #update_note

Methods included from Models

#add_field, #add_template, #create_model, #find_and_replace_in_model, #get_field_descriptions, #get_field_fonts, #get_field_names, #get_fields_on_templates, #get_models_by_id, #get_models_by_name, #get_styling, #get_templates, #model_names, #model_names_and_ids, #remove_field, #remove_template, #rename_field, #rename_template, #reposition_field, #reposition_template, #set_field_description, #set_field_font, #set_field_font_size, #update_model

Methods included from Decks

#clone_deck_config, #create_deck, #deck_names, #deck_names_and_ids, #delete_decks, #get_deck_config, #get_deck_stats, #get_decks_for_cards, #move_cards, #remove_deck_config, #save_deck_config, #set_deck_config

Methods included from Cards

#answer_cards, #due?, #forget_cards, #get_cards, #get_cards_mod_time, #get_ease_factors, #get_intervals, #get_note_ids, #relearn_cards, #search_cards, #set_due_date, #set_ease_factors, #suspend_cards, #suspended?, #unsuspend_cards, #update_card

Constructor Details

#initialize(host: '127.0.0.1', port: 8765, api_key: nil) ⇒ Client

Creates a new AnkiConnect client.

Parameters:

  • host (String) (defaults to: '127.0.0.1')

    AnkiConnect server host (default: “127.0.0.1”)

  • port (Integer) (defaults to: 8765)

    AnkiConnect server port (default: 8765)

  • api_key (String, nil) (defaults to: nil)

    Optional API key for authentication



32
33
34
35
36
37
# File 'lib/anki_connect/client.rb', line 32

def initialize(host: '127.0.0.1', port: 8765, api_key: nil)
  @host = host
  @port = port
  @api_key = api_key
  @uri = URI("http://#{host}:#{port}")
end

Instance Attribute Details

#api_keyString? (readonly)

Returns API key for authentication (if configured).

Returns:

  • (String, nil)

    API key for authentication (if configured)



25
26
27
# File 'lib/anki_connect/client.rb', line 25

def api_key
  @api_key
end

#hostString (readonly)

Returns AnkiConnect server host.

Returns:

  • (String)

    AnkiConnect server host



21
22
23
# File 'lib/anki_connect/client.rb', line 21

def host
  @host
end

#portInteger (readonly)

Returns AnkiConnect server port.

Returns:

  • (Integer)

    AnkiConnect server port



23
24
25
# File 'lib/anki_connect/client.rb', line 23

def port
  @port
end

Instance Method Details

#request(action, **params) ⇒ Object

Makes a request to the AnkiConnect API. This is the core method used by all API operations.

Parameters:

  • action (Symbol)

    The API action to perform

  • params (Hash)

    Parameters to send with the request

Returns:

  • (Object)

    The result from the API

Raises:

  • (Error)

    If the API returns an error



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/anki_connect/client.rb', line 46

def request(action, **params)
  body = {
    action: action,
    version: API_VERSION,
    params: params
  }
  body[:key] = @api_key if @api_key

  response = Net::HTTP.post(
    @uri,
    body.to_json,
    'Content-Type' => 'application/json'
  )

  result = JSON.parse(response.body)

  raise Error, result['error'] if result['error']

  result['result']
end