Class: NewsmastMastodon::AltTextAiApiService

Inherits:
Object
  • Object
show all
Defined in:
app/services/newsmast_mastodon/alt_text_ai_api_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AltTextAiApiService

Returns a new instance of AltTextAiApiService.



10
11
12
13
14
15
# File 'app/services/newsmast_mastodon/alt_text_ai_api_service.rb', line 10

def initialize(options = {})
  @options = options
  @base_url = ENV['ALT_TEXT_URL']
  @api_key =  ENV['ALT_TEXT_SECRET']
  @payload = @options[:payload] if @options.key?(:payload)
end

Instance Method Details

#create_imageObject



22
23
24
25
# File 'app/services/newsmast_mastodon/alt_text_ai_api_service.rb', line 22

def create_image
  response = make_post_request('images')
  return response
end

#get_accountObject



17
18
19
20
# File 'app/services/newsmast_mastodon/alt_text_ai_api_service.rb', line 17

def 
  response = make_get_request('account')
  return response
end

#make_get_request(endpoint) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/newsmast_mastodon/alt_text_ai_api_service.rb', line 27

def make_get_request(endpoint)
  base_url = @base_url + endpoint
  headers = {
    'X-API-Key' => @api_key,
    'Content-Type' => 'application/json',
  }
  begin
    response = HTTParty.get(base_url, headers: headers)
    resp_body_obj = NewsmastMastodon::AlttextGetAccount.new(JSON.parse(response.body))
    return resp_body_obj
  rescue StandardError => e
    Rails.logger.error "Error making GET request: #{e.message}"
  end
end

#make_post_request(endpoint) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/newsmast_mastodon/alt_text_ai_api_service.rb', line 42

def make_post_request(endpoint)
  base_url = @base_url + endpoint
  headers = {
    'X-API-Key' => @api_key,
    'Content-Type' => 'application/json',
  }
  begin
    response = HTTParty.post(base_url,
                             body: @payload.to_json, headers: headers)
    resp_body_obj = NewsmastMastodon::AlttextCreateImage.new(JSON.parse(response.body))
    return resp_body_obj
  rescue StandardError => e
    Rails.logger.error "Error making POST request: #{e.message}"
  end
end