Class: Pingfm::Client

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

Constant Summary collapse

API_KEY =

The registered API key for the Ping.fm Ruby library client.

'5fcb8b7041d5c32c7e1e60dc076989ba'
API_URL =

MUST NOT end with a trailing slash, as this string is interpolated like this: “#API_URL/user.services” FIXME: This should be handled better; not so brittle as to break on a trailing slash.

'http://api.ping.fm/v1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_app_key) ⇒ Client

Returns a new instance of Client.



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

def initialize(user_app_key)
  @user_app_key = user_app_key
end

Instance Attribute Details

#user_app_keyObject (readonly)

Returns the value of attribute user_app_key.



15
16
17
# File 'lib/pingfm/client.rb', line 15

def user_app_key
  @user_app_key
end

Instance Method Details

#latest(limit = 25, order = 'DESC') ⇒ Object

Returns the last limit messages a user has posted through Ping.fm.

Optional arguments:

limit

Limit the results returned; default is 25.

order

Which direction to order the returned results by date; default is descending.

If successful returns:

{'status' => 'OK', 'messages' => [{'id' => 'messageid', 'method' => 'messsagemethod', 'rfc' => 'date', 'unix' => 'date', 'title' => 'messagetitle', 'body' => 'messagebody', 'services' => [{'id' => 'serviceid', 'name' => 'servicename'}, ...]}, ...]}

If unsuccessful returns:

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


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pingfm/client.rb', line 31

def latest(limit = 25, order = 'DESC')
  response = get_response('user.latest', 'limit' => limit, 'order' => order)
  if response.elements['rsp'].attributes['status'] == 'OK'
    latest = status_ok
    latest['messages'] = []
    response.elements.each('rsp/messages/message') do |message|
      latest['messages'].push({})
      latest['messages'].last['id'] = message.attributes['id']
      latest['messages'].last['method'] = message.attributes['method']
      latest['messages'].last['rfc'] = message.elements['date'].attributes['rfc']
      latest['messages'].last['unix'] = message.elements['date'].attributes['unix']

      if message.elements['*/title'] != nil
        latest['messages'].last['title'] = message.elements['*/title'].text
      else
        latest['messages'].last['title'] = ''
      end
      if message.elements['location'] != nil
        latest['messages'].last['location'] = message.elements['location'].text
      else
        latest['messages'].last['location'] = ''
      end
      latest['messages'].last['body'] = message.elements['*/body'].text
      latest['messages'].last['services'] = []
      message.elements.each('services/service') do |service|
        latest['messages'].last['services'].push({'id' => service.attributes['id'], 'name' => service.attributes['name']})
      end
    end
    return latest
  else
    return status_fail(response)
  end
end

#post(body, opts = { :title => '', :post_method => 'default', :service => '', :debug => false }) ⇒ Object

Posts a message to the user’s Ping.fm services.

Arguments:

body

Message body.

Optional args:

title

Title of the posted message; title is required for ‘blog’ post method.

post_method

Posting method; either ‘default’, ‘blog’, ‘microblog’ or ‘status’.

service

A single service to post to.

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'}


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pingfm/client.rb', line 80

def post(body, opts = { :title => '', :post_method => 'default', :service => '', :debug => false })
  response = get_response('user.post',
                          'body' => body, 'title' => opts[:title],
                          'post_method' => opts[:post_method], 'service' => opts[:service],
                          'debug' => (opts[:debug] ? 1 : 0))

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

#servicesObject

Returns a list of services the user has set up through Ping.fm.

If successful returns:

{'status' => 'OK', 'services' => [{'id' => 'serviceid', 'name' => 'servicename', 'trigger' => 'servicetrigger', 'url' => 'serviceurl', 'icon' => 'serviceicon', 'methods' => 'status,blog'}, ...]}

If unsuccessful returns:

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


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/pingfm/client.rb', line 99

def services
  response = get_response('user.services')
  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'],
                    'trigger' => service.elements['trigger'].text,
                    'url' => service.elements['url'].text,
                    'icon' => service.elements['icon'].text,
                    'methods' => service.elements['methods'].text})
    end
    return services
  else
    return status_fail(response)
  end
end

#system_servicesObject

Return a complete list of supported services.

If successful returns:

{'status' => 'OK', 'services' => [{'id' => 'serviceid', 'name' => 'servicename', 'trigger' => 'servicetrigger', 'url' => 'serviceurl', 'icon' => 'serviceicon'}, ...]}

If unsuccessful returns:

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


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/pingfm/client.rb', line 124

def system_services
  response = get_response('system.services')
  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'],
                                 'trigger' => service.elements['trigger'].text,
                                 'url'     => service.elements['url'].text,
                                 'icon'    => service.elements['icon'].text})
    end
    return services
  else
    return status_fail(response)
  end
end

#tpost(body, trigger, opts = { :title => '', :debug => false }) ⇒ Object

Posts a message to the user’s Ping.fm services using one of their custom triggers.

Arguments:

body

Message body.

trigger

Custom trigger the user has defined from the Ping.fm website.

Optional arguments:

title

Title of the posted message; title is required for ‘blog’ post method.

debug

Set debug to true to avoid posting test data.

If successful returns:

{'status' => 'OK'}

If unsuccessful returns:

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


156
157
158
159
160
161
162
163
164
165
# File 'lib/pingfm/client.rb', line 156

def tpost(body, trigger, opts = { :title => '', :debug => false })
  response = get_response('user.tpost',
                          'body' => body, 'title' => opts[:title],
                          'trigger' => trigger, 'debug' => (opts[:debug] ? 1 : 0))
  if response.elements['rsp'].attributes['status'] == 'OK'
    return status_ok
  else
    return status_fail(response)
  end
end

#triggersObject

Returns a user’s custom triggers.

If successful returns:

{'status' => 'OK', 'triggers' => [{'id' => 'triggerid', 'method' => 'triggermethod', 'services' => [{'id' => 'serviceid', 'name' => 'servicename'}, ...]}, ...]}

If unsuccessful returns:

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


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/pingfm/client.rb', line 173

def triggers
  response = get_response('user.triggers')
  if response.elements['rsp'].attributes['status'] == 'OK'
    triggers = status_ok
    triggers['triggers'] = []
    response.elements.each('rsp/triggers/trigger') do |trigger|
      triggers['triggers'].push({'id' => trigger.attributes['id'], 'method' => trigger.attributes['method'], 'services' => []})

      trigger.elements.each('services/service') do |service|
        triggers['triggers'].last['services'].push({'id' => service.attributes['id'], 'name' => service.attributes['name']})
      end
    end
    return triggers
  else
    return status_fail(response)
  end
end

#validateObject

Validates the API key and user APP key.

If successful returns:

{'status' => 'OK'}

If unsuccessful returns:

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


197
198
199
200
201
202
203
204
# File 'lib/pingfm/client.rb', line 197

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