Class: GNUSocial::Client

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

Overview

This class manages calls to GNU Social API

Instance Method Summary collapse

Constructor Details

#initialize(node, user, passwd, options = {}) ⇒ Client

Creates and configure a Client for make calls to GNU Social API.

Arguments:

node: GNU Social instance
user: Username
passwd: Password

Optional:
    ssl: Whether to use SSL, default false
    ssl_insecure: If is true, SSL cert will not be verified
    proxy_host: Proxy url used for the connection
    proxy_port: Proxy port used for the connection
    source: This is used for identify your app, default 'api'


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gsruby.rb', line 40

def initialize(node, user, passwd, options = {})
    node = URI(node)
    if options[:proxy_host] && options[:proxy_port]
        proxy = [options[:proxy_host],options[:proxy_port]]
    end
    @node = Net::HTTP.new(node.hostname,node.port,*proxy)
    @user = user
    @passwd = passwd
    @source = options[:source] ? options[:source] : 'api'
    @node.use_ssl = true if options[:ssl]
    @node.verify_mode = OpenSSL::SSL::VERIFY_NONE if options[:ssl_insecure]
end

Instance Method Details

#connect(path, params) ⇒ Object

Makes a request to the GNU Social instance.

Arguments:

path: Path to do the connection
params: A hash containing additional parameters


58
59
60
61
62
63
# File 'lib/gsruby.rb', line 58

def connect(path, params)
    request = Net::HTTP::Post.new(path)
    request.basic_auth(@user,@passwd)
    request.set_form_data(params)
    res = @node.request(request)
end

#conversation(notice, options = {}) ⇒ Object

Gets the full conversation of the given Notice.

Arguments:

notice: NoticeID (integer) or Notice object
options: Check get_timeline options


115
116
117
118
# File 'lib/gsruby.rb', line 115

def conversation(notice, options = {})
    id = get_id(notice)
    get_timeline("/api/statusnet/conversation/#{id}.json",options)
end

#delete(notice) ⇒ Object



188
189
190
# File 'lib/gsruby.rb', line 188

def delete(notice)
    connect('',params)
end

#fav(notice) ⇒ Object

Marks the given notice as favorite.

Arguments:

notice: Notice to be marked as fav


169
170
171
172
173
# File 'lib/gsruby.rb', line 169

def fav(notice)
    id = get_id(notice)
    params = {:id => id}
    connect("/api/favorites/create/#{id}.json",params)
end

#get_id(notice) ⇒ Object

Return the ID of the given notice.



144
145
146
147
148
149
150
151
152
# File 'lib/gsruby.rb', line 144

def get_id(notice)
    if notice.is_a? Fixnum
        return notice
    elsif notice.is_a? Notice
        return notice.id
    else
        raise "Must be a integer or a Notice"
    end
end

#get_timeline(path, options = {}) ⇒ Object

Makes a request to GNU Social instance and returns a Timeline object.

Arguments:

path: Path to do the connection
options: A hash containing additional parameters, that can be:
    since: Notice ID (integer). Request will return newer notices than the specified
    before: Notice ID (integer). Request will return older notices than the specified
    count: Integer. Number of notices that will be returned
    page: Number of page that will be returned, 1 page == 20 notices


74
75
76
77
78
79
80
81
# File 'lib/gsruby.rb', line 74

def get_timeline(path, options = {})
    params = {}
    params[:since_id] = options[:since] if options[:since]
    params[:max_id] = options[:before] if options[:before]
    params[:count] = options[:count] if options[:count]
    params[:page] = options[:page] if options[:page]
    Timeline.from_json(connect(path,params).body)
end

#home_timeline(options = {}) ⇒ Object

Gets the user’s Home Timeline.

Arguments:

options: Check get_timeline options


95
96
97
# File 'lib/gsruby.rb', line 95

def home_timeline(options = {})
    get_timeline('/api/statuses/home_timeline.json',options)
end

#mentions(options = {}) ⇒ Object

Gets the user’s mentions.

Arguments:

options: Check get_timeline options


103
104
105
# File 'lib/gsruby.rb', line 103

def mentions(options = {})
    get_timeline('/api/statuses/mentions.json',options)
end

#notice(id) ⇒ Object

Returns the notice with the given ID.

Arguments: id: ID of the notice to be returned



124
125
126
127
# File 'lib/gsruby.rb', line 124

def notice(id)
    res = @node.get("/api/statuses/show/#{id.to_s}.json")
    Notice.new(JSON.parse(res.body))
end

#public_timeline(options = {}) ⇒ Object

Gets the instance Public Timeline.

Arguments:

options: Check get_timeline options


87
88
89
# File 'lib/gsruby.rb', line 87

def public_timeline(options = {})
    get_timeline('/api/statuses/public_timeline.json',options)
end

#repeat(notice, options = {}) ⇒ Object

Repeat the given notice.

Arguments:

notice: Notice to be repeated
options: A hash containing additional parameters, that can be:
   source: Change the source of the status, default is source property


181
182
183
184
185
186
# File 'lib/gsruby.rb', line 181

def repeat(notice, options = {})
    id = get_id(notice)
    params = {:id => id}
    params[:source] = options[:source] ? options[:source] : @source
    connect("/api/statuses/retweet/#{id}.json",params)
end

#reply(msg, notice, options = {}) ⇒ Object

Publish a reply to the given Notice.

Arguments:

msg: Message to be published
notice: NoticeID (integer) or Notice object
options: Check send_status options


160
161
162
163
# File 'lib/gsruby.rb', line 160

def reply(msg, notice, options = {})
    options[:reply] = get_id(notice)
    send_status(msg,options)
end

#send_status(msg, options = {}) ⇒ Object

Publish a new status.

Arguments:

msg: Message to be published
options: A hash containing additional parameters, that can be:
    reply: Publish as reply of the given NoticeID (integer) or Notice object
    source: Change the source of the status, default is source property


136
137
138
139
140
141
# File 'lib/gsruby.rb', line 136

def send_status(msg, options = {})
    params = {:status => msg}
    params[:in_reply_to_status_id] = options[:reply] if options[:reply]
    params[:source] = options[:source] ? options[:source] : @source
    connect('/api/statuses/update.json',params)
end