Class: UtopianRuby::UtopianRubyAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/utopian_ruby_api.rb

Constant Summary collapse

@@host =
'https://api.utopian.io'

Class Method Summary collapse

Class Method Details

.get_connectionObject



9
10
11
12
13
14
15
# File 'lib/utopian_ruby_api.rb', line 9

def self.get_connection()
  conn = Faraday.new(:url => @@host) do |c|
    c.use Faraday::Request::UrlEncoded 
    #c.use Faraday::Response::Logger     
    c.use Faraday::Adapter::NetHttp     
  end
end

.get_moderator(user) ⇒ Object

get a particular moderator’s metadata



53
54
55
56
57
58
59
60
# File 'lib/utopian_ruby_api.rb', line 53

def self.get_moderator(user)
  get_moderators()["results"].each do |moderator|
    if moderator['account']==user
      return moderator
    end
  end
  nil
end

.get_moderatorsObject

list all Utopian moderators



28
29
30
# File 'lib/utopian_ruby_api.rb', line 28

def self.get_moderators()
  JSON.parse(get_request('/api/moderators').body)
end

.get_post(author, permlink) ⇒ Object

return a particular post information



96
97
98
# File 'lib/utopian_ruby_api.rb', line 96

def self.get_post(author,permlink)
  JSON.parse(get_request('/api/posts/'+author+'/'+permlink).body)
end

.get_posts(params = nil) ⇒ Object

return JSON containing all post information that satisfies params



88
89
90
91
92
93
# File 'lib/utopian_ruby_api.rb', line 88

def self.get_posts(params=nil)
  if params.nil?
     params = {}
  end
  JSON.parse(get_request('/api/posts?'+get_url_parameters(params)).body)
end

.get_request(endpoint) ⇒ Object



17
18
19
# File 'lib/utopian_ruby_api.rb', line 17

def self.get_request(endpoint)
  get_connection().get endpoint
end

.get_sponsorsObject

return sponsor list



63
64
65
# File 'lib/utopian_ruby_api.rb', line 63

def self.get_sponsors()
  JSON.parse(get_request('/api/sponsors').body)
end

.get_url_parameters(params) ⇒ Object



21
22
23
24
25
# File 'lib/utopian_ruby_api.rb', line 21

def self.get_url_parameters(params)
  uri = Addressable::URI.new
  uri.query_values = params
  uri.query
end

.is_moderator(user) ⇒ Object

check if a user is a moderator



33
34
35
36
37
38
39
40
# File 'lib/utopian_ruby_api.rb', line 33

def self.is_moderator(user)
  get_moderators()["results"].each do |moderator|
    if moderator['account']==user
      return true
    end
  end
  false
end

.is_sponsor(user) ⇒ Object

check if a user is an utopian sponsor



68
69
70
71
72
73
74
75
# File 'lib/utopian_ruby_api.rb', line 68

def self.is_sponsor(user)
  get_sponsors()["results"].each do |sponsor|
    if sponsor['account']==user
      return true
    end
  end
  false
end

.is_supervisor(user) ⇒ Object

check if a user is an utopian supervisor



43
44
45
46
47
48
49
50
# File 'lib/utopian_ruby_api.rb', line 43

def self.is_supervisor(user)
  get_moderators()["results"].each do |moderator|
    if moderator['account']==user and moderator['supermoderator']
      return true
    end
  end
  false
end

.is_votingObject

return if Utopian is currently voting



83
84
85
# File 'lib/utopian_ruby_api.rb', line 83

def self.is_voting()
  self.stats()["stats"]["bot_is_voting"]
end

.post_count(params) ⇒ Object

return total number of Utopian posts that satisfy params



101
102
103
104
105
106
# File 'lib/utopian_ruby_api.rb', line 101

def self.post_count(params)
  if params.nil?
     params = {"limit":1}
  end
  get_posts(params)["total"]
end

.statsObject

return Utopian current stats



78
79
80
# File 'lib/utopian_ruby_api.rb', line 78

def self.stats()
  JSON.parse(get_request('/api/stats').body)
end