Class: MastodonAPI

Inherits:
Object
  • Object
show all
Defined in:
app/mastodon_api.rb

Defined Under Namespace

Classes: APIError, UnauthenticatedError, UnexpectedResponseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, access_token = nil) ⇒ MastodonAPI

Returns a new instance of MastodonAPI.



27
28
29
30
31
# File 'app/mastodon_api.rb', line 27

def initialize(host, access_token = nil)
  @host = host
  @root = "https://#{@host}/api/v1"
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



25
26
27
# File 'app/mastodon_api.rb', line 25

def access_token
  @access_token
end

Instance Method Details

#account_infoObject



46
47
48
49
# File 'app/mastodon_api.rb', line 46

def 
  raise UnauthenticatedError.new unless @access_token
  get_json("/accounts/verify_credentials")
end

#account_statuses(user_id, params = {}) ⇒ Object



57
58
59
# File 'app/mastodon_api.rb', line 57

def (user_id, params = {})
  get_json("/accounts/#{user_id}/statuses", params)
end

#get_json(path, params = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/mastodon_api.rb', line 67

def get_json(path, params = {})
  url = URI(path.start_with?('https://') ? path : @root + path)
  url.query = URI.encode_www_form(params) if params

  headers = {}
  headers['Authorization'] = "Bearer #{@access_token}" if @access_token

  response = Net::HTTP.get_response(url, headers)
  status = response.code.to_i

  if status / 100 == 2
    JSON.parse(response.body)
  elsif status / 100 == 3
    get_json(response['Location'])
  else
    raise APIError.new(response)
  end
end

#lookup_account(username) ⇒ Object



51
52
53
54
55
# File 'app/mastodon_api.rb', line 51

def (username)
  json = get_json("/accounts/lookup", { acct: username })
  raise UnexpectedResponseError.new unless json.is_a?(Hash) && json['id'].is_a?(String)
  json
end

#oauth_login_with_password(client_id, client_secret, email, password, scopes) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/mastodon_api.rb', line 33

def (client_id, client_secret, email, password, scopes)
  params = {
    client_id: client_id,
    client_secret: client_secret,
    grant_type: 'password',
    scope: scopes,
    username: email,
    password: password
  }

  post_json("https://#{@host}/oauth/token", params)
end

#post_json(path, params = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/mastodon_api.rb', line 86

def post_json(path, params = {})
  url = URI(path.start_with?('https://') ? path : @root + path)

  headers = {}
  headers['Authorization'] = "Bearer #{@access_token}" if @access_token

  request = Net::HTTP::Post.new(url, headers)
  request.form_data = params

  response = Net::HTTP.start(url.hostname, url.port, :use_ssl => true) do |http|
    http.request(request)
  end

  status = response.code.to_i

  if status / 100 == 2
    JSON.parse(response.body)
  else
    raise APIError.new(response)
  end
end

#post_status(text) ⇒ Object



61
62
63
64
65
# File 'app/mastodon_api.rb', line 61

def post_status(text)
  post_json("/statuses", {
    status: text
  })
end