Class: Anilibria::Api::Client
- Inherits:
-
Object
- Object
- Anilibria::Api::Client
- Defined in:
- lib/anilibria/api/client.rb,
lib/anilibria/api/client/_methods.rb
Constant Summary collapse
- DryTypes =
Types::DryTypes
- API_VERSION =
'2.13'.freeze
- ENDPOINT =
"https://api.anilibria.tv/v#{API_VERSION}".freeze
- AUTH_ENDPOINT =
'https://www.anilibria.tv/public/login.php'.freeze
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Class Method Summary collapse
- .api_method(method_name, api_method_name = nil, returns:, http_method: :get) ⇒ Object
- .api_methods ⇒ Object
Instance Method Summary collapse
- #api_version ⇒ Object
- #auth(mail, passwd) ⇒ Object
- #auth!(mail, passwd) ⇒ Object
- #call(method_name, params = {}, http_method = :get) ⇒ Object
-
#initialize(url: ENDPOINT) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(url: ENDPOINT) ⇒ Client
Returns a new instance of Client.
43 44 45 46 47 48 49 50 51 |
# File 'lib/anilibria/api/client.rb', line 43 def initialize(url: ENDPOINT) @connection = Faraday.new( url: url, headers: { 'User-Agent' => "anilibria-api-ruby/#{VERSION}" } ) do |f| f.request :url_encoded f.response :json, parser_options: { symbolize_names: true } end end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
41 42 43 |
# File 'lib/anilibria/api/client.rb', line 41 def connection @connection end |
Class Method Details
.api_method(method_name, api_method_name = nil, returns:, http_method: :get) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/anilibria/api/client.rb', line 5 def api_method(method_name, api_method_name = nil, returns:, http_method: :get) (@api_methods ||= []) << method_name.to_sym api_method_name ||= camelize(method_name.to_s) define_method(method_name) do |params = {}| response_body = call(api_method_name, params, http_method) returns[response_body] end end |
.api_methods ⇒ Object
15 16 17 |
# File 'lib/anilibria/api/client.rb', line 15 def api_methods (@api_methods ||= []).dup end |
Instance Method Details
#api_version ⇒ Object
84 85 86 |
# File 'lib/anilibria/api/client.rb', line 84 def api_version connection.head.headers['API-Version'] end |
#auth(mail, passwd) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/anilibria/api/client.rb', line 63 def auth(mail, passwd) response = auth_response(mail, passwd) return unless auth_successful?(response) response[:sessionId] end |
#auth!(mail, passwd) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/anilibria/api/client.rb', line 71 def auth!(mail, passwd) response = auth_response(mail, passwd) unless auth_successful?(response) raise( Exceptions::AuthError.new(response), 'Failed authorization attempt' ) end response[:sessionId] end |
#call(method_name, params = {}, http_method = :get) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/anilibria/api/client.rb', line 53 def call(method_name, params = {}, http_method = :get) http_method = :get unless %i[get post put patch delete].include?(http_method) response = connection.public_send(http_method, method_name) do |req| req.params.merge!(params) end check_response!(response) response.body end |