Class: HelloTxt::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key, user_key) ⇒ Client

Returns a new instance of Client.



13
14
15
16
# File 'lib/hellotxt/client.rb', line 13

def initialize(api_key, user_key)
  @api_key = api_key
  @user_key = user_key
end

Instance Method Details

#latestObject

Returns a list of 20 status update sent through HelloTxt. if successful returns:

{'status' => 'OK', 'messages' => [{'id' => 'messageid', 'title' => 'titletext', 'body' => 'bodytext'}, ...]}

if unsuccessful returns:

{'status' => 'FAIL', 'message' => 'message what went wrong'}


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/hellotxt/client.rb', line 87

def latest
  response = get_response('user.latest')
  if response.elements['rsp'].attributes['status'] == 'OK'
    messages = status_ok()
    messages['messages'] = []
    response.elements.each('rsp/messages/message') do |message|
      messages['messages'].push({'id' => message.attributes['id'],
                                 'title' => message.elements['title'].text,
                                 'body' => message.elements['body'].text})
    end
    return messages
  else
    return status_fail(response)
  end
end

#post(body, title = '', group = 'inhome', image = '', debug = 0) ⇒ Object

Posts a message to the user’s social services Arguments: body = message body Optional arguments: title = title of the posted message, only used if the service supports it group = service group type; either ‘inhome’, ‘friend’, ‘colleague’ image = raw bytes of an image to post debug = set debug to 1 to avoid posting test data if successful returns:

{'status' => 'OK'}

if unsuccessful returns:

{'status' => 'FAIL', 'message' => 'message what went wrong'}


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hellotxt/client.rb', line 70

def post(body, title = '', group = 'inhome', image = '', debug = 0)
  response = get_response('user.post',
                          'body' => body, 'title' => title,
                          'group' => group, 'image' => image,
                          'debug' => debug)
  if response.elements['rsp'].attributes['status'] == 'OK'
    return status_ok
  else
    return status_fail(response)
  end
end

#user_services(group = 'all') ⇒ Object

Returns a list of services the user has set up through HelloTxt Optional arguments: group = service group type; either ‘inhome’, ‘friend’, ‘colleague’, ‘all’ if successful returns:

{'status' => 'OK', 'services' => [{'id' => 'serviceid', 'name' => 'servicename', 'code' => 'servicecode', 'inhome' => 'checked', 'friend' => 'checked', 'colleague' => 'checked'}, ...]}

if unsuccessful returns:

{'status' => 'FAIL', 'message' => 'message what went wrong'}


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hellotxt/client.rb', line 39

def user_services(group = 'all')
  response = get_response('user.services', 'group' => group)
  if response.elements['rsp'].attributes['status'] == 'OK'
    services = status_ok()
    services['services'] = []
    response.elements.each('rsp/services/service') do |service|
      services['services'].push({'id' => service.attributes['id'],
                                 'name' => service.attributes['name'],
                                 'code' => service.elements['code'].text,
                                 'inhome' => service.elements['inhome'].text,
                                 'friend' => service.elements['friend'].text,
                                 'colleague' => service.elements['colleague'].text})
    end
    return services
  else
    return status_fail(response)
  end
end

#validateObject

Validates API key and USER key if successful returns:

{'status' => 'OK'}

if unsuccessful returns:

{'status' => 'FAIL', 'message' => 'message what went wrong'}


23
24
25
26
27
28
29
30
# File 'lib/hellotxt/client.rb', line 23

def validate
  response = get_response('user.validate')
  if response.elements['rsp'].attributes['status'] == 'OK'
    return status_ok
  else
    return status_fail(response)
  end
end