pingfm 1.0.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +26 -0
- data/{README → README.rdoc} +40 -50
- data/Rakefile +11 -26
- data/bin/pingfm +37 -31
- data/lib/pingfm.rb +5 -53
- data/lib/pingfm/client.rb +194 -179
- data/lib/pingfm/config.rb +36 -0
- data/pingfm.gemspec +27 -0
- data/spec/pingfm/client_spec.rb +300 -0
- data/spec/pingfm/config_spec.rb +21 -0
- data/spec/pingfm_spec.rb +3 -305
- data/spec/pingfm_spec.yml +2 -0
- data/spec/spec_helper.rb +28 -40
- metadata +63 -60
- data/History.txt +0 -12
- data/Manifest.txt +0 -25
- data/lib/pingfm/keyloader.rb +0 -58
- data/spec/keyloader_spec.rb +0 -43
- data/tasks/ann.rake +0 -81
- data/tasks/bones.rake +0 -21
- data/tasks/gem.rake +0 -126
- data/tasks/git.rake +0 -41
- data/tasks/manifest.rake +0 -49
- data/tasks/notes.rake +0 -28
- data/tasks/post_load.rake +0 -39
- data/tasks/rdoc.rake +0 -51
- data/tasks/rubyforge.rake +0 -57
- data/tasks/setup.rb +0 -268
- data/tasks/spec.rake +0 -55
- data/tasks/svn.rake +0 -48
- data/tasks/test.rake +0 -38
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pingfm (2.0.0)
|
5
|
+
slop
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.1.2)
|
11
|
+
rspec (2.6.0)
|
12
|
+
rspec-core (~> 2.6.0)
|
13
|
+
rspec-expectations (~> 2.6.0)
|
14
|
+
rspec-mocks (~> 2.6.0)
|
15
|
+
rspec-core (2.6.4)
|
16
|
+
rspec-expectations (2.6.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.6.0)
|
19
|
+
slop (1.7.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
pingfm!
|
26
|
+
rspec (>= 2.6.0)
|
data/{README → README.rdoc}
RENAMED
@@ -1,85 +1,75 @@
|
|
1
|
-
Ping.fm Ruby
|
1
|
+
= Ping.fm Ruby Library
|
2
2
|
|
3
|
-
==
|
4
|
-
|
5
|
-
* Krunoslav Husak (http://h00s.net)
|
6
|
-
* Dale Campbell (http://corrupt.save-state.net)
|
7
|
-
* Kevin Williams (http://almostserio.us)
|
3
|
+
== Library Usage
|
8
4
|
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
# Require the library and initialize it.
|
6
|
+
require 'pingfm'
|
7
|
+
pingfm = Pingfm::Client.new('user_app_key')
|
12
8
|
|
13
|
-
|
9
|
+
# Ensure proper API and User App keys.
|
10
|
+
pingfm.validate['status']
|
11
|
+
# => {'status' => 'OK'} # if success, otherwise 'FAIL'
|
14
12
|
|
15
|
-
|
13
|
+
# Grab latest 5 posts.
|
14
|
+
pingfm.latest(5) # => {'messages' => [...]}
|
16
15
|
|
17
|
-
|
16
|
+
# Posting to all services.
|
17
|
+
pingfm.post('The Dark Knight was amazing.')
|
18
|
+
# => {'status' => 'OK'} # if success, otherwise 'FAIL'
|
18
19
|
|
19
|
-
|
20
|
-
|
20
|
+
# Post using custom user trigger; must be defined on the Ping.fm site.
|
21
|
+
pingfm.tpost('The message here.', '#something_custom', 'Optional Title')
|
22
|
+
# => {'status' => 'OK'} # if success, otherwise 'FAIL'
|
21
23
|
|
22
|
-
|
24
|
+
Check the {documentation}[http://rdoc.info/projects/Oshuma/pingfm] for more details.
|
23
25
|
|
24
|
-
Shell
|
26
|
+
== Shell Usage
|
25
27
|
|
26
28
|
$ pingfm This message will post to my default services.
|
27
29
|
|
28
|
-
Everything after the 'pingfm' command is what will be posted to the service. You
|
30
|
+
Everything after the '<tt>pingfm</tt>' command is what will be posted to the service. You
|
29
31
|
may also include the message within quotes (ex. using the client within a shell script).
|
30
32
|
|
31
|
-
If your
|
32
|
-
in a YAML file in your home directory and you won't be asked for
|
33
|
+
If your key has not been stored, it will ask for it. This key will be saved
|
34
|
+
in a YAML file in your home directory and you won't be asked for it again.
|
33
35
|
|
34
|
-
You can obtain your
|
36
|
+
You can obtain your key here:
|
35
37
|
|
36
|
-
* User API Key - http://ping.fm/key/
|
37
|
-
* Application API Key - http://ping.fm/developers/
|
38
|
+
* User API Key - {http://ping.fm/key/}[http://ping.fm/key/]
|
38
39
|
|
39
|
-
Library usage:
|
40
40
|
|
41
|
-
|
42
|
-
require 'pingfm'
|
43
|
-
pingfm = Pingfm::Client.new('api_key', 'user_app_key')
|
41
|
+
== Authors
|
44
42
|
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
* {Dale Campbell}[http://oshuma.github.com/]
|
44
|
+
* {Kevin Williams}[http://kevwil.com/]
|
45
|
+
* {Krunoslav Husak}[http://h00s.net]
|
48
46
|
|
49
|
-
|
50
|
-
pingfm.latest(5) # => {'messages' => [...]}
|
47
|
+
== Code
|
51
48
|
|
52
|
-
|
53
|
-
pingfm.post('The Dark Knight was amazing.')
|
54
|
-
# => {'status' => 'OK'} if success, otherwise 'FAIL'
|
55
|
-
|
56
|
-
# Post using custom user trigger; must be defined on the Ping.fm site.
|
57
|
-
pingfm.tpost('The message here.', '#something_custom', 'Optional Title')
|
58
|
-
# => {'status' => 'OK'} if success, otherwise 'FAIL'
|
49
|
+
{http://github.com/Oshuma/pingfm}[http://github.com/Oshuma/pingfm]
|
59
50
|
|
51
|
+
== Description
|
60
52
|
|
61
|
-
|
53
|
+
{Ping.fm}[http://ping.fm/] is a simple service that makes updating your social networks a snap; and this it's Ruby library.
|
62
54
|
|
63
|
-
==
|
55
|
+
== Additional Features
|
64
56
|
|
65
|
-
|
57
|
+
* Installing the gem also installs a '<tt>pingfm</tt>' binary to post from the command-line.
|
58
|
+
* User API key is stored in a YAML file in your home directory (<tt>$HOME/.pingfm.yml</tt>).
|
66
59
|
|
67
|
-
==
|
60
|
+
== Requirements
|
68
61
|
|
69
|
-
|
70
|
-
$ sudo gem install pingfm
|
62
|
+
Ruby!
|
71
63
|
|
72
|
-
|
64
|
+
== Install
|
73
65
|
|
74
|
-
|
75
|
-
$ sudo gem sources -a http://gems.github.com (only need to do this once)
|
76
|
-
$ sudo gem install oshuma-pingfm
|
66
|
+
$ [sudo] gem install pingfm
|
77
67
|
|
78
|
-
==
|
68
|
+
== License
|
79
69
|
|
80
70
|
(The MIT License)
|
81
71
|
|
82
|
-
Copyright (c)
|
72
|
+
Copyright (c) 2010 Dale Campbell, Kevin Williams, Krunoslav Husak
|
83
73
|
|
84
74
|
Permission is hereby granted, free of charge, to any person obtaining
|
85
75
|
a copy of this software and associated documentation files (the
|
data/Rakefile
CHANGED
@@ -1,31 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# are where the options are used.
|
1
|
+
require 'bundler'
|
2
|
+
require 'rspec/core/rake_task'
|
4
3
|
|
5
|
-
|
4
|
+
Bundler::GemHelper.install_tasks
|
6
5
|
|
7
|
-
|
8
|
-
require 'pingfm'
|
6
|
+
task :default => :spec
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
PROJ.authors = ['Krunoslav Husak', 'Dale Campbell', 'Kevin Williams']
|
14
|
-
PROJ.email = ['dale@save-state.net', 'kevwil@gmail.com']
|
15
|
-
PROJ.url = 'http://pingfm.rubyforge.org/'
|
16
|
-
PROJ.version = ENV['VERSION'] || Pingfm.version
|
17
|
-
PROJ.rubyforge.name = 'pingfm'
|
18
|
-
PROJ.readme_file = 'README'
|
19
|
-
|
20
|
-
PROJ.spec.opts << '--color'
|
21
|
-
|
22
|
-
namespace :gem do
|
23
|
-
desc 'create a gemspec file to support github gems'
|
24
|
-
task :gemspec => 'gem:prereqs' do
|
25
|
-
File.open("#{PROJ.name}.gemspec", 'w+') do |f|
|
26
|
-
f.write PROJ.gem._spec.to_ruby
|
27
|
-
end
|
28
|
-
end
|
8
|
+
desc 'Start a console loaded with the library'
|
9
|
+
task :console do
|
10
|
+
sh "irb -I ./lib -r 'pingfm'"
|
29
11
|
end
|
30
12
|
|
31
|
-
|
13
|
+
desc 'Run the specs'
|
14
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
15
|
+
t.rspec_opts = ['--color']
|
16
|
+
end
|
data/bin/pingfm
CHANGED
@@ -1,43 +1,49 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# TODO: Eventually add some flags to support all Ping.fm functionality.
|
3
2
|
|
4
|
-
|
5
|
-
|
3
|
+
require 'rubygems' if RUBY_VERSION =~ /1\.8/
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'slop'
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
opts = Slop.parse!(:help => true) do
|
8
|
+
banner "Usage: #{File.basename(__FILE__)} [options] <message>"
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
print 'Enter your Ping.fm User API key (http://ping.fm/key/): '
|
14
|
-
keyloader.app_key = STDIN.gets.chomp
|
15
|
-
keyloader.save
|
16
|
-
end
|
17
|
-
|
18
|
-
# post message from ARGV
|
19
|
-
|
20
|
-
pingfm = ::Pingfm::Client.new(keyloader.api_key, keyloader.app_key)
|
10
|
+
on :debug, 'Enable debugging' do
|
11
|
+
$DEBUG = true
|
12
|
+
end
|
21
13
|
|
22
|
-
|
23
|
-
|
24
|
-
|
14
|
+
# TODO: Implement these.
|
15
|
+
# on :c, :config, 'Use the specified (YAML) config file', :optional => false
|
16
|
+
# on :l, :latest, 'List your latest <count> posts', :optional => false
|
17
|
+
# on :t, :title, 'Title of the post'
|
25
18
|
|
26
|
-
|
27
|
-
|
28
|
-
puts 'Must provide a message to send.'
|
19
|
+
on :version, 'Print the version' do
|
20
|
+
STDOUT.puts "Ping.fm v#{Pingfm::VERSION}"
|
29
21
|
exit
|
30
22
|
end
|
23
|
+
end
|
24
|
+
|
25
|
+
if ARGV.empty?
|
26
|
+
STDERR.puts "Must provide a message."
|
27
|
+
STDERR.puts # spacer
|
28
|
+
STDERR.puts opts.help
|
29
|
+
exit
|
30
|
+
end
|
31
31
|
|
32
|
-
|
32
|
+
# Join ARGV just in case the user didn't quote the message.
|
33
|
+
message = ARGV.join(' ')
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
else
|
40
|
-
puts s['message']
|
35
|
+
begin
|
36
|
+
app_key = Pingfm::Config['app_key']
|
37
|
+
rescue Pingfm::ConfigNotFound => error
|
38
|
+
Pingfm::Config.ask_for_app_key!
|
39
|
+
app_key = Pingfm::Config['app_key']
|
41
40
|
end
|
42
41
|
|
43
|
-
|
42
|
+
client = Pingfm::Client.new(app_key)
|
43
|
+
response = client.post(message)
|
44
|
+
|
45
|
+
if response['status'] == 'OK'
|
46
|
+
STDOUT.puts 'Message sent.'
|
47
|
+
else
|
48
|
+
STDERR.puts response['message']
|
49
|
+
end
|
data/lib/pingfm.rb
CHANGED
@@ -1,56 +1,8 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
unless defined? Pingfm
|
3
|
+
require 'pingfm/client'
|
4
|
+
require 'pingfm/config'
|
6
5
|
|
7
6
|
module Pingfm
|
8
|
-
|
9
|
-
|
10
|
-
VERSION = '1.0.2'
|
11
|
-
LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
|
12
|
-
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
13
|
-
# :startdoc:
|
14
|
-
|
15
|
-
# Returns the version string for the library.
|
16
|
-
#
|
17
|
-
def self.version
|
18
|
-
VERSION
|
19
|
-
end
|
20
|
-
|
21
|
-
# Returns the library path for the module. If any arguments are given,
|
22
|
-
# they will be joined to the end of the libray path using
|
23
|
-
# <tt>File.join</tt>.
|
24
|
-
#
|
25
|
-
def self.libpath( *args )
|
26
|
-
args.empty? ? LIBPATH : ::File.join(LIBPATH, *args)
|
27
|
-
end
|
28
|
-
|
29
|
-
# Returns the lpath for the module. If any arguments are given,
|
30
|
-
# they will be joined to the end of the path using
|
31
|
-
# <tt>File.join</tt>.
|
32
|
-
#
|
33
|
-
def self.path( *args )
|
34
|
-
args.empty? ? PATH : ::File.join(PATH, *args)
|
35
|
-
end
|
36
|
-
|
37
|
-
# Utility method used to rquire all files ending in .rb that lie in the
|
38
|
-
# directory below this file that has the same name as the filename passed
|
39
|
-
# in. Optionally, a specific _directory_ name can be passed in such that
|
40
|
-
# the _filename_ does not have to be equivalent to the directory.
|
41
|
-
#
|
42
|
-
def self.require_all_libs_relative_to( fname, dir = nil )
|
43
|
-
dir ||= ::File.basename(fname, '.*')
|
44
|
-
search_me = ::File.expand_path(
|
45
|
-
::File.join(::File.dirname(fname), dir, '**', '*.rb'))
|
46
|
-
|
47
|
-
Dir.glob(search_me).sort.each {|rb| require rb}
|
48
|
-
end
|
49
|
-
|
50
|
-
end # module Pingfm
|
51
|
-
|
52
|
-
Pingfm.require_all_libs_relative_to __FILE__
|
53
|
-
|
54
|
-
end # unless defined?
|
55
|
-
|
56
|
-
# EOF
|
7
|
+
VERSION = '2.0.0'
|
8
|
+
end
|
data/lib/pingfm/client.rb
CHANGED
@@ -1,38 +1,125 @@
|
|
1
|
-
# Ping.fm Ruby Client
|
2
1
|
require 'net/http'
|
3
|
-
require 'rexml/document'
|
2
|
+
require 'rexml/document' # TODO: Rewrite this to use something faster (Nokogiri, possibly).
|
4
3
|
|
5
4
|
module Pingfm
|
5
|
+
class Client
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
API_URL = 'http://api.ping.fm/v1'
|
7
|
+
# The registered API key for the Ping.fm Ruby library client.
|
8
|
+
API_KEY = '5fcb8b7041d5c32c7e1e60dc076989ba'
|
10
9
|
|
11
|
-
|
10
|
+
# MUST NOT end with a trailing slash, as this string is interpolated like this:
|
11
|
+
# "#{API_URL}/user.services"
|
12
|
+
# FIXME: This should be handled better; not so brittle as to break on a trailing slash.
|
13
|
+
API_URL = 'http://api.ping.fm/v1'
|
14
|
+
|
15
|
+
attr_reader :user_app_key
|
16
|
+
|
17
|
+
def initialize(user_app_key)
|
18
|
+
@user_app_key = user_app_key
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns the last <tt>limit</tt> messages a user has posted through Ping.fm.
|
22
|
+
#
|
23
|
+
# Optional arguments:
|
24
|
+
# [limit] Limit the results returned; default is 25.
|
25
|
+
# [order] = Which direction to order the returned results by date; default is descending.
|
26
|
+
#
|
27
|
+
# If successful returns:
|
28
|
+
# {'status' => 'OK', 'messages' => [{'id' => 'messageid', 'method' => 'messsagemethod', 'rfc' => 'date', 'unix' => 'date', 'title' => 'messagetitle', 'body' => 'messagebody', 'services' => [{'id' => 'serviceid', 'name' => 'servicename'}, ...]}, ...]}
|
29
|
+
# If unsuccessful returns:
|
30
|
+
# {'status' => 'FAIL', 'message' => 'message what went wrong'}
|
31
|
+
def latest(limit = 25, order = 'DESC')
|
32
|
+
response = get_response('user.latest', 'limit' => limit, 'order' => order)
|
33
|
+
if response.elements['rsp'].attributes['status'] == 'OK'
|
34
|
+
latest = status_ok
|
35
|
+
latest['messages'] = []
|
36
|
+
response.elements.each('rsp/messages/message') do |message|
|
37
|
+
latest['messages'].push({})
|
38
|
+
latest['messages'].last['id'] = message.attributes['id']
|
39
|
+
latest['messages'].last['method'] = message.attributes['method']
|
40
|
+
latest['messages'].last['rfc'] = message.elements['date'].attributes['rfc']
|
41
|
+
latest['messages'].last['unix'] = message.elements['date'].attributes['unix']
|
42
|
+
|
43
|
+
if message.elements['*/title'] != nil
|
44
|
+
latest['messages'].last['title'] = message.elements['*/title'].text
|
45
|
+
else
|
46
|
+
latest['messages'].last['title'] = ''
|
47
|
+
end
|
48
|
+
if message.elements['location'] != nil
|
49
|
+
latest['messages'].last['location'] = message.elements['location'].text
|
50
|
+
else
|
51
|
+
latest['messages'].last['location'] = ''
|
52
|
+
end
|
53
|
+
latest['messages'].last['body'] = message.elements['*/body'].text
|
54
|
+
latest['messages'].last['services'] = []
|
55
|
+
message.elements.each('services/service') do |service|
|
56
|
+
latest['messages'].last['services'].push({'id' => service.attributes['id'], 'name' => service.attributes['name']})
|
57
|
+
end
|
58
|
+
end
|
59
|
+
return latest
|
60
|
+
else
|
61
|
+
return status_fail(response)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Posts a message to the user's Ping.fm services.
|
66
|
+
#
|
67
|
+
# Arguments:
|
68
|
+
# [body] Message body.
|
69
|
+
#
|
70
|
+
# Optional <tt>args</tt>:
|
71
|
+
# [title] Title of the posted message; title is required for 'blog' post method.
|
72
|
+
# [post_method] Posting method; either 'default', 'blog', 'microblog' or 'status'.
|
73
|
+
# [service] A single service to post to.
|
74
|
+
# [debug] Set debug to 1 to avoid posting test data.
|
75
|
+
#
|
76
|
+
# If successful returns:
|
77
|
+
# {'status' => 'OK'}
|
78
|
+
# If unsuccessful returns:
|
79
|
+
# {'status' => 'FAIL', 'message' => 'message what went wrong'}
|
80
|
+
def post(body, opts = { :title => '', :post_method => 'default', :service => '', :debug => false })
|
81
|
+
response = get_response('user.post',
|
82
|
+
'body' => body, 'title' => opts[:title],
|
83
|
+
'post_method' => opts[:post_method], 'service' => opts[:service],
|
84
|
+
'debug' => (opts[:debug] ? 1 : 0))
|
85
|
+
|
86
|
+
if response.elements['rsp'].attributes['status'] == 'OK'
|
87
|
+
return status_ok
|
88
|
+
else
|
89
|
+
return status_fail(response)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Returns a list of services the user has set up through Ping.fm.
|
94
|
+
#
|
95
|
+
# If successful returns:
|
96
|
+
# {'status' => 'OK', 'services' => [{'id' => 'serviceid', 'name' => 'servicename', 'trigger' => 'servicetrigger', 'url' => 'serviceurl', 'icon' => 'serviceicon', 'methods' => 'status,blog'}, ...]}
|
97
|
+
# If unsuccessful returns:
|
98
|
+
# {'status' => 'FAIL', 'message' => 'message what went wrong'}
|
99
|
+
def services
|
100
|
+
response = get_response('user.services')
|
101
|
+
if response.elements['rsp'].attributes['status'] == 'OK'
|
102
|
+
services = status_ok()
|
103
|
+
services['services'] = []
|
104
|
+
response.elements.each('rsp/services/service') do |service|
|
105
|
+
services['services'].push({'id' => service.attributes['id'],
|
106
|
+
'name' => service.attributes['name'],
|
107
|
+
'trigger' => service.elements['trigger'].text,
|
108
|
+
'url' => service.elements['url'].text,
|
109
|
+
'icon' => service.elements['icon'].text,
|
110
|
+
'methods' => service.elements['methods'].text})
|
111
|
+
end
|
112
|
+
return services
|
113
|
+
else
|
114
|
+
return status_fail(response)
|
115
|
+
end
|
116
|
+
end
|
12
117
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
# Validates API key and user APP key
|
19
|
-
# if successful returns:
|
20
|
-
# {'status' => 'OK'}
|
21
|
-
# if unsuccessful returns:
|
22
|
-
# {'status' => 'FAIL', 'message' => 'message what went wrong'}
|
23
|
-
def validate
|
24
|
-
response = get_response('user.validate')
|
25
|
-
if response.elements['rsp'].attributes['status'] == 'OK'
|
26
|
-
return status_ok
|
27
|
-
else
|
28
|
-
return status_fail(response)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# Return a complete list of supported services
|
33
|
-
# if successful returns:
|
118
|
+
# Return a complete list of supported services.
|
119
|
+
#
|
120
|
+
# If successful returns:
|
34
121
|
# {'status' => 'OK', 'services' => [{'id' => 'serviceid', 'name' => 'servicename', 'trigger' => 'servicetrigger', 'url' => 'serviceurl', 'icon' => 'serviceicon'}, ...]}
|
35
|
-
#
|
122
|
+
# If unsuccessful returns:
|
36
123
|
# {'status' => 'FAIL', 'message' => 'message what went wrong'}
|
37
124
|
def system_services
|
38
125
|
response = get_response('system.services')
|
@@ -52,174 +139,102 @@ module Pingfm
|
|
52
139
|
end
|
53
140
|
end
|
54
141
|
|
55
|
-
|
56
|
-
|
57
|
-
#
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
142
|
+
# Posts a message to the user's Ping.fm services using one of their custom triggers.
|
143
|
+
#
|
144
|
+
# Arguments:
|
145
|
+
# [body] Message body.
|
146
|
+
# [trigger] Custom trigger the user has defined from the Ping.fm website.
|
147
|
+
#
|
148
|
+
# Optional arguments:
|
149
|
+
# [title] Title of the posted message; title is required for 'blog' post method.
|
150
|
+
# [debug] Set debug to +true+ to avoid posting test data.
|
151
|
+
#
|
152
|
+
# If successful returns:
|
153
|
+
# {'status' => 'OK'}
|
154
|
+
# If unsuccessful returns:
|
155
|
+
# {'status' => 'FAIL', 'message' => 'message what went wrong'}
|
156
|
+
def tpost(body, trigger, opts = { :title => '', :debug => false })
|
157
|
+
response = get_response('user.tpost',
|
158
|
+
'body' => body, 'title' => opts[:title],
|
159
|
+
'trigger' => trigger, 'debug' => (opts[:debug] ? 1 : 0))
|
160
|
+
if response.elements['rsp'].attributes['status'] == 'OK'
|
161
|
+
return status_ok
|
162
|
+
else
|
75
163
|
return status_fail(response)
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
96
|
-
return triggers
|
97
|
-
else
|
98
|
-
return status_fail(response)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
# Returns the last <tt>limit</tt> messages a user has posted through Ping.fm
|
103
|
-
# Optional arguments:
|
104
|
-
# limit = limit the results returned, default is 25
|
105
|
-
# order = which direction to order the returned results by date, default is DESC (descending)
|
106
|
-
# if successful returns:
|
107
|
-
# {'status' => 'OK', 'messages' => [{'id' => 'messageid', 'method' => 'messsagemethod', 'rfc' => 'date', 'unix' => 'date', 'title' => 'messagetitle', 'body' => 'messagebody', 'services' => [{'id' => 'serviceid', 'name' => 'servicename'}, ...]}, ...]}
|
108
|
-
# if unsuccessful returns:
|
109
|
-
# {'status' => 'FAIL', 'message' => 'message what went wrong'}
|
110
|
-
def latest(limit = 25, order = 'DESC')
|
111
|
-
response = get_response('user.latest', 'limit' => limit, 'order' => order)
|
112
|
-
if response.elements['rsp'].attributes['status'] == 'OK'
|
113
|
-
latest = status_ok
|
114
|
-
latest['messages'] = []
|
115
|
-
response.elements.each('rsp/messages/message') do |message|
|
116
|
-
latest['messages'].push({})
|
117
|
-
latest['messages'].last['id'] = message.attributes['id']
|
118
|
-
latest['messages'].last['method'] = message.attributes['method']
|
119
|
-
latest['messages'].last['rfc'] = message.elements['date'].attributes['rfc']
|
120
|
-
latest['messages'].last['unix'] = message.elements['date'].attributes['unix']
|
121
|
-
|
122
|
-
if message.elements['*/title'] != nil
|
123
|
-
latest['messages'].last['title'] = message.elements['*/title'].text
|
124
|
-
else
|
125
|
-
latest['messages'].last['title'] = ''
|
126
|
-
end
|
127
|
-
if message.elements['location'] != nil
|
128
|
-
latest['messages'].last['location'] = message.elements['location'].text
|
129
|
-
else
|
130
|
-
latest['messages'].last['location'] = ''
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
# Returns a user's custom triggers.
|
168
|
+
#
|
169
|
+
# If successful returns:
|
170
|
+
# {'status' => 'OK', 'triggers' => [{'id' => 'triggerid', 'method' => 'triggermethod', 'services' => [{'id' => 'serviceid', 'name' => 'servicename'}, ...]}, ...]}
|
171
|
+
# If unsuccessful returns:
|
172
|
+
# {'status' => 'FAIL', 'message' => 'message what went wrong'}
|
173
|
+
def triggers
|
174
|
+
response = get_response('user.triggers')
|
175
|
+
if response.elements['rsp'].attributes['status'] == 'OK'
|
176
|
+
triggers = status_ok
|
177
|
+
triggers['triggers'] = []
|
178
|
+
response.elements.each('rsp/triggers/trigger') do |trigger|
|
179
|
+
triggers['triggers'].push({'id' => trigger.attributes['id'], 'method' => trigger.attributes['method'], 'services' => []})
|
180
|
+
|
181
|
+
trigger.elements.each('services/service') do |service|
|
182
|
+
triggers['triggers'].last['services'].push({'id' => service.attributes['id'], 'name' => service.attributes['name']})
|
131
183
|
end
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
# {'status' => 'FAIL', 'message' => 'message what went wrong'}
|
156
|
-
def post(body, title = '', post_method = 'default', service = '', debug = 0)
|
157
|
-
response = get_response('user.post',
|
158
|
-
'body' => body, 'title' => title,
|
159
|
-
'post_method' => post_method, 'service' => service,
|
160
|
-
'debug' => debug)
|
161
|
-
if response.elements['rsp'].attributes['status'] == 'OK'
|
162
|
-
return status_ok
|
163
|
-
else
|
164
|
-
return status_fail(response)
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
# Posts a message to the user's Ping.fm services using one of their custom triggers
|
169
|
-
# Arguments:
|
170
|
-
# body = message body
|
171
|
-
# trigger = custom trigger the user has defined from the Ping.fm website
|
172
|
-
# Optional arguments:
|
173
|
-
# title = title of the posted message, title is required for 'blog' post method
|
174
|
-
# debug = set debug to 1 to avoid posting test data
|
175
|
-
# if successful returns:
|
176
|
-
# {'status' => 'OK'}
|
177
|
-
# if unsuccessful returns:
|
178
|
-
# {'status' => 'FAIL', 'message' => 'message what went wrong'}
|
179
|
-
def tpost(body, trigger, title = '', debug = 0)
|
180
|
-
response = get_response('user.tpost',
|
181
|
-
'body' => body, 'title' => title,
|
182
|
-
'trigger' => trigger, 'debug' => debug)
|
183
|
-
if response.elements['rsp'].attributes['status'] == 'OK'
|
184
|
-
return status_ok
|
185
|
-
else
|
186
|
-
return status_fail(response)
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
private
|
184
|
+
end
|
185
|
+
return triggers
|
186
|
+
else
|
187
|
+
return status_fail(response)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
# Validates the API key and user APP key.
|
192
|
+
#
|
193
|
+
# If successful returns:
|
194
|
+
# {'status' => 'OK'}
|
195
|
+
# If unsuccessful returns:
|
196
|
+
# {'status' => 'FAIL', 'message' => 'message what went wrong'}
|
197
|
+
def validate
|
198
|
+
response = get_response('user.validate')
|
199
|
+
if response.elements['rsp'].attributes['status'] == 'OK'
|
200
|
+
return status_ok
|
201
|
+
else
|
202
|
+
return status_fail(response)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
private
|
191
207
|
|
192
208
|
# Gets a particular ping.fm response.
|
193
|
-
#
|
194
|
-
#
|
195
|
-
# with the request. The API key and user app key are merged with this on every call.
|
209
|
+
#
|
210
|
+
# [type] The service type (ex. 'user.services'). Gets appended to <tt>API_URL</tt>.
|
211
|
+
# [parameters] Optional (depending on the <tt>type</tt>) parameters to be passed along. with the request. The API key and user app key are merged with this on every call.
|
196
212
|
def get_response(type, parameters = {})
|
197
|
-
parameters.merge!('api_key' =>
|
198
|
-
|
213
|
+
parameters.merge!('api_key' => API_KEY, 'user_app_key' => @user_app_key)
|
214
|
+
REXML::Document.new(http_request("#{API_URL}/#{type}", parameters))
|
199
215
|
end
|
200
216
|
|
201
217
|
# This makes the actual HTTP request.
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
218
|
+
def http_request(url, parameters)
|
219
|
+
response = Net::HTTP.post_form(URI.parse(url), parameters)
|
220
|
+
return response.body
|
221
|
+
end
|
206
222
|
|
207
223
|
# Successful response.
|
208
|
-
|
209
|
-
|
210
|
-
|
224
|
+
def status_ok
|
225
|
+
return {'status' => 'OK'}
|
226
|
+
end
|
211
227
|
|
212
228
|
# Failed response.
|
213
|
-
|
229
|
+
def status_fail(response)
|
214
230
|
if response.elements.include? 'rsp/message'
|
215
231
|
message = response.elements['rsp/message'].text
|
216
232
|
else
|
217
|
-
message = "Unknown error from Ping.fm"
|
233
|
+
message = "Unknown error from Ping.fm."
|
218
234
|
end
|
219
235
|
|
220
|
-
|
221
|
-
|
236
|
+
return {'status' => 'FAIL', 'message' => message}
|
237
|
+
end
|
222
238
|
|
223
239
|
end
|
224
|
-
|
225
240
|
end
|