Class: Pingfm::Client

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

Overview

TODO: The status_ok and status_fail methods need a little thought.

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.



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

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.



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

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 ‘DESC’.

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


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
64
# File 'lib/pingfm/client.rb', line 32

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

Returns the last limit number of links the user has shortened and posted through Ping.fm.

Optional arguments:

limit

Limit the results returned; default is 25.

order

The sort order of the results; default is ‘DESC’.

If successful returns:

{'status' => 'OK', 'links' => [{ 'short' => 'http://ping.fm/}, ...]}


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pingfm/client.rb', line 74

def links(limit = 25, order = 'DESC')
  response = get_response('user.links', 'limit' => limit, 'order' => order)
  if response.elements['rsp'].attributes['status'] == 'OK'
    links = []
    response.elements.each('rsp/links/link') do |link|
      links << {
        'date'  => link.elements['date'].attributes['rfc'],
        'short' => link.elements['short'].text,
        'long'  => link.elements['long'].text,
      }
    end
    status_ok('links' => links)
  else
    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'}


112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/pingfm/client.rb', line 112

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


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/pingfm/client.rb', line 131

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


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/pingfm/client.rb', line 156

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


188
189
190
191
192
193
194
195
196
197
# File 'lib/pingfm/client.rb', line 188

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


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/pingfm/client.rb', line 205

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

#url_createObject



91
92
# File 'lib/pingfm/client.rb', line 91

def url_create
end

#url_resolveObject



94
95
# File 'lib/pingfm/client.rb', line 94

def url_resolve
end

#user_key(mobile_key) ⇒ Object

Returns the full user app key from the provided mobile_key.



224
225
226
227
228
229
230
231
232
# File 'lib/pingfm/client.rb', line 224

def user_key(mobile_key)
  response = get_response('user.key', 'mobile_key' => mobile_key)
  if response.elements['rsp'].attributes['status'] == 'OK'
    app_key = response.elements['rsp'].elements['key'].text
    status_ok('app_key' => app_key)
  else
    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'}


240
241
242
243
244
245
246
247
# File 'lib/pingfm/client.rb', line 240

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