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 CHANGED
@@ -1 +1,3 @@
1
+ .rvmrc
2
+ doc/api/
1
3
  pkg/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ # Dependencies set in .gemspec
4
+ gemspec
@@ -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)
@@ -1,85 +1,75 @@
1
- Ping.fm Ruby Client/Library
1
+ = Ping.fm Ruby Library
2
2
 
3
- == Authors
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
- == CODE:
10
-
11
- http://pingfm.rubyforge.org/
5
+ # Require the library and initialize it.
6
+ require 'pingfm'
7
+ pingfm = Pingfm::Client.new('user_app_key')
12
8
 
13
- == DESCRIPTION:
9
+ # Ensure proper API and User App keys.
10
+ pingfm.validate['status']
11
+ # => {'status' => 'OK'} # if success, otherwise 'FAIL'
14
12
 
15
- Ping.fm (http://ping.fm) is a simple service that makes updating your social networks a snap, and this it's Ruby library.
13
+ # Grab latest 5 posts.
14
+ pingfm.latest(5) # => {'messages' => [...]}
16
15
 
17
- == FEATURES/PROBLEMS:
16
+ # Posting to all services.
17
+ pingfm.post('The Dark Knight was amazing.')
18
+ # => {'status' => 'OK'} # if success, otherwise 'FAIL'
18
19
 
19
- * Installing the gem creates a 'pingfm' shell script to post from the shell.
20
- * Keys are stored in a YAML file in your home directory (under Linux/OSX).
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
- == SYNOPSIS:
24
+ Check the {documentation}[http://rdoc.info/projects/Oshuma/pingfm] for more details.
23
25
 
24
- Shell usage:
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 keys have not been stored, it will ask for them. These keys will be saved
32
- in a YAML file in your home directory and you won't be asked for them again.
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 keys here:
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
- # Require the library and initialize it.
42
- require 'pingfm'
43
- pingfm = Pingfm::Client.new('api_key', 'user_app_key')
41
+ == Authors
44
42
 
45
- # Ensure proper API and User App keys.
46
- pingfm.validate['status']
47
- # => 'OK' if success, otherwise 'FAIL'
43
+ * {Dale Campbell}[http://oshuma.github.com/]
44
+ * {Kevin Williams}[http://kevwil.com/]
45
+ * {Krunoslav Husak}[http://h00s.net]
48
46
 
49
- # Grab latest 5 posts.
50
- pingfm.latest(5) # => {'messages' => [...]}
47
+ == Code
51
48
 
52
- # Posting to all services.
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
- Check the source comments for more details.
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
- == REQUIREMENTS:
55
+ == Additional Features
64
56
 
65
- Ruby!
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
- == INSTALL:
60
+ == Requirements
68
61
 
69
- # From Rubyforge...
70
- $ sudo gem install pingfm
62
+ Ruby!
71
63
 
72
- *OR*
64
+ == Install
73
65
 
74
- # From Github...
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
- == LICENSE:
68
+ == License
79
69
 
80
70
  (The MIT License)
81
71
 
82
- Copyright (c) 2008 Krunoslav Husak, Dale Campbell, Kevin Williams
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
- # Look in the tasks/setup.rb file for the various options that can be
2
- # configured in this Rakefile. The .rake files in the tasks directory
3
- # are where the options are used.
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
4
3
 
5
- load 'tasks/setup.rb'
4
+ Bundler::GemHelper.install_tasks
6
5
 
7
- ensure_in_path 'lib'
8
- require 'pingfm'
6
+ task :default => :spec
9
7
 
10
- task :default => 'spec:run'
11
-
12
- PROJ.name = 'pingfm'
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
- # EOF
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
- # TODO: Move this into a YAML config?
5
- API_KEY = '5fcb8b7041d5c32c7e1e60dc076989ba'
3
+ require 'rubygems' if RUBY_VERSION =~ /1\.8/
4
+ require 'bundler/setup'
5
+ require 'slop'
6
6
 
7
- require File.expand_path(
8
- File.join(File.dirname(__FILE__), '..', 'lib', 'pingfm'))
7
+ opts = Slop.parse!(:help => true) do
8
+ banner "Usage: #{File.basename(__FILE__)} [options] <message>"
9
9
 
10
- keyloader = ::Pingfm::Keyloader.new
11
- unless keyloader.has_keys?
12
- keyloader.api_key = API_KEY
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
- s = pingfm.validate
23
- if s['status'] == 'OK'
24
- status = ARGV.join(' ')
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
- # Might be a good idea to throw an exception here, instead of just bailing.
27
- if status.nil? || status.empty?
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
- post_result = pingfm.post(status)
32
+ # Join ARGV just in case the user didn't quote the message.
33
+ message = ARGV.join(' ')
33
34
 
34
- if post_result['status'] == 'FAIL'
35
- puts post_result['message']
36
- else
37
- puts 'Message sent.'
38
- end
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
- # EOF
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
@@ -1,56 +1,8 @@
1
- # $Id$
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
 
3
- # Equivalent to a header guard in C/C++
4
- # Used to prevent the class/module from being loaded more than once
5
- unless defined? Pingfm
3
+ require 'pingfm/client'
4
+ require 'pingfm/config'
6
5
 
7
6
  module Pingfm
8
-
9
- # :stopdoc:
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
@@ -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
- # MUST NOT end with a trailing slash, as this string is interpolated like this:
8
- # "#{API_URL}/user.services"
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
- class Client
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
- def initialize(api_key, user_app_key)
14
- @api_key = api_key
15
- @user_app_key = user_app_key
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
- # if unsuccessful returns:
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
- # Returns a list of services the user has set up through Ping.fm
56
- # if successful returns:
57
- # {'status' => 'OK', 'services' => [{'id' => 'serviceid', 'name' => 'servicename', 'trigger' => 'servicetrigger', 'url' => 'serviceurl', 'icon' => 'serviceicon', 'methods' => 'status,blog'}, ...]}
58
- # if unsuccessful returns:
59
- # {'status' => 'FAIL', 'message' => 'message what went wrong'}
60
- def services
61
- response = get_response('user.services')
62
- if response.elements['rsp'].attributes['status'] == 'OK'
63
- services = status_ok()
64
- services['services'] = []
65
- response.elements.each('rsp/services/service') do |service|
66
- services['services'].push({'id' => service.attributes['id'],
67
- 'name' => service.attributes['name'],
68
- 'trigger' => service.elements['trigger'].text,
69
- 'url' => service.elements['url'].text,
70
- 'icon' => service.elements['icon'].text,
71
- 'methods' => service.elements['methods'].text})
72
- end
73
- return services
74
- else
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
- end
77
- end
78
-
79
- # Returns a user's custom triggers
80
- # if successful returns:
81
- # {'status' => 'OK', 'triggers' => [{'id' => 'triggerid', 'method' => 'triggermethod', 'services' => [{'id' => 'serviceid', 'name' => 'servicename'}, ...]}, ...]}
82
- # if unsuccessful returns:
83
- # {'status' => 'FAIL', 'message' => 'message what went wrong'}
84
- def triggers
85
- response = get_response('user.triggers')
86
- if response.elements['rsp'].attributes['status'] == 'OK'
87
- triggers = status_ok
88
- triggers['triggers'] = []
89
- response.elements.each('rsp/triggers/trigger') do |trigger|
90
- triggers['triggers'].push({'id' => trigger.attributes['id'], 'method' => trigger.attributes['method'], 'services' => []})
91
-
92
- trigger.elements.each('services/service') do |service|
93
- triggers['triggers'].last['services'].push({'id' => service.attributes['id'], 'name' => service.attributes['name']})
94
- end
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
- latest['messages'].last['body'] = message.elements['*/body'].text
133
- latest['messages'].last['services'] = []
134
- message.elements.each('services/service') do |service|
135
- latest['messages'].last['services'].push({'id' => service.attributes['id'], 'name' => service.attributes['name']})
136
- end
137
- end
138
- return latest
139
- else
140
- return status_fail(response)
141
- end
142
- end
143
-
144
- # Posts a message to the user's Ping.fm services
145
- # Arguments:
146
- # body = message body
147
- # Optional arguments:
148
- # title = title of the posted message, title is required for 'blog' post method
149
- # post_method = posting method; either 'default', 'blog', 'microblog' or 'status.'
150
- # service = a single service to post to
151
- # debug = set debug to 1 to avoid posting test data
152
- # if successful returns:
153
- # {'status' => 'OK'}
154
- # if unsuccessful returns:
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
- # <tt>type</tt>: The service type (ex. 'user.services'). Gets appended to <tt>API_URL</tt>.
194
- # <tt>parameters</tt>: Optional (depending on the <tt>type</tt>) parameters to be passed along
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' => @api_key, 'user_app_key' => @user_app_key)
198
- REXML::Document.new(http_request("#{API_URL}/#{type}", parameters))
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
- def http_request(url, parameters)
203
- response = Net::HTTP.post_form(URI.parse(url), parameters)
204
- return response.body
205
- end
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
- def status_ok
209
- return {'status' => 'OK'}
210
- end
224
+ def status_ok
225
+ return {'status' => 'OK'}
226
+ end
211
227
 
212
228
  # Failed response.
213
- def status_fail(response)
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
- return {'status' => 'FAIL', 'message' => message}
221
- end
236
+ return {'status' => 'FAIL', 'message' => message}
237
+ end
222
238
 
223
239
  end
224
-
225
240
  end