Class: Rst::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri = "https://rstat.us") ⇒ Client

Returns a new instance of Client.



8
9
10
# File 'lib/rst/client.rb', line 8

def initialize(base_uri = "https://rstat.us")
  @base_uri = base_uri
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



6
7
8
# File 'lib/rst/client.rb', line 6

def base_uri
  @base_uri
end

Instance Method Details

#messages_all(params = {:page => 1}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rst/client.rb', line 12

def messages_all(params = {:page => 1})
  messages_all_path = find_a_in(root_response, :rel => "messages-all")
  messages_all_uri = resolve_relative_uri(
    :relative => messages_all_path,
    :base     => base_uri
  )

  all_response = get_body(messages_all_uri)
  current_uri = messages_all_uri

  (params[:page] - 1).times do
    next_page_path = find_a_in(all_response, :rel => "next")
    current_uri = resolve_relative_uri(
      :relative => next_page_path,
      :base     => current_uri
    )
    all_response = get_body(current_uri)
  end

  messages = all_response.css("div#messages ul.all li").map { |li|
               Rst::Status.parse(li)
             }
end

#messages_user(params = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rst/client.rb', line 36

def messages_user(params = {})
  search_results = users_search(:pattern => params[:username])

  result = search_results.detect { |sr|
    sr.username.match(/^#{params[:username]}$/i)
  }

  user_uri = resolve_relative_uri(
    :relative => result.path,
    :base     => base_uri
  )

  user_response = get_body(user_uri)

  message_nodes = user_response.css("div#messages ul.messages-user li")
  message_nodes.map { |li| Rst::Status.parse(li) }
end

#users_search(params = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rst/client.rb', line 54

def users_search(params = {})
  users_search_path = find_a_in(root_response, :rel => "users-search")
  users_search_uri = resolve_relative_uri(
    :relative => users_search_path,
    :base     => base_uri
  )

  users_search_response = get_body(users_search_uri)

  form = users_search_response.css("form.users-search").first
  search_uri = resolve_relative_uri(
    :relative => form["action"],
    :base     => users_search_uri
  )

  user_lookup_query = "#{search_uri}?search=#{params[:pattern]}"

  user_lookup_response = get_body(user_lookup_query)

  search_results = user_lookup_response.css("div#users ul.search li.user")
  search_results.map { |li| Rst::User.parse(li) }
end