Class: MapSqueakSession
Overview
MapSqueakSession - an class that encapsulates a session with the mapsqueak server.
Instance Attribute Summary collapse
-
#facebook_token ⇒ Object
Returns the value of attribute facebook_token.
-
#host ⇒ Object
Returns the value of attribute host.
-
#session_cookie ⇒ Object
readonly
Returns the value of attribute session_cookie.
Instance Method Summary collapse
- #create_user(name, email, password) ⇒ Object
- #edit_squeak(squeak, update_hash) ⇒ Object
-
#get_my_squeaks(format = :xml) ⇒ Object
return all of my squeaks in a specified format, either :xml or :json.
-
#get_squeaks(center_latitude, center_longitude, max = 100, format = :xml) ⇒ Object
get a list of no more than max squeaks closest to the given center_latitude/centerlongitude The format must either be :json or :xml.
-
#initialize(host = 'http://mapsqueak.heroku.com', username = nil, password = nil) ⇒ MapSqueakSession
constructor
A new instance of MapSqueakSession.
-
#sign_in(email, password, format = :xml) ⇒ Object
sign in by email/password.
-
#sign_out ⇒ Object
sign the current user out.
-
#squeak(squeak, send_format = :xml, facebook_params = {}, verbose = false) ⇒ Object
post a new squeak.
- #squeak_str_to_objects(squeak_string, format) ⇒ Object
Methods included from XmlHelpers
Constructor Details
#initialize(host = 'http://mapsqueak.heroku.com', username = nil, password = nil) ⇒ MapSqueakSession
Returns a new instance of MapSqueakSession.
27 28 29 30 31 |
# File 'lib/mapsqueak.rb', line 27 def initialize(host = 'http://mapsqueak.heroku.com', username=nil,password=nil) @host = host @cookie_file = '[email protected]' signin(username,password) unless username.nil? end |
Instance Attribute Details
#facebook_token ⇒ Object
Returns the value of attribute facebook_token.
24 25 26 |
# File 'lib/mapsqueak.rb', line 24 def facebook_token @facebook_token end |
#host ⇒ Object
Returns the value of attribute host.
24 25 26 |
# File 'lib/mapsqueak.rb', line 24 def host @host end |
#session_cookie ⇒ Object (readonly)
Returns the value of attribute session_cookie.
25 26 27 |
# File 'lib/mapsqueak.rb', line 25 def @session_cookie end |
Instance Method Details
#create_user(name, email, password) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/mapsqueak.rb', line 33 def create_user(name,email,password) # :name, :email, :password, :password_confirmation curl_str = "curl -F name=\'#{name}\' -F email=#{email} -F password=\'#{password}\' -F password_confirmation=\'#{password}\' #{self.host}/users" $stderr.puts curl_str `#{curl_str}` end |
#edit_squeak(squeak, update_hash) ⇒ Object
159 160 161 162 |
# File 'lib/mapsqueak.rb', line 159 def edit_squeak(squeak, update_hash) squeak.merge!(update_hash) "curl --request PUT --data \'#{squeak.to_json}\' #{self.host}/squeaks/#{squeak.id}.json -H \"Content-Type: application/json\"" end |
#get_my_squeaks(format = :xml) ⇒ Object
return all of my squeaks in a specified format, either :xml or :json
148 149 150 151 152 153 154 155 156 |
# File 'lib/mapsqueak.rb', line 148 def get_my_squeaks(format=:xml) unless [:json, :xml].include?(format) $stderr.puts "Error: must be in json or xml" end # TODO: add a hash based on the parameters requested and use session token # T squeak_string = `curl #{self.host}/squeaks/mine.#{format.to_s} --cookie #{@cookie_file}` return squeak_str_to_objects(squeak_string,format) end |
#get_squeaks(center_latitude, center_longitude, max = 100, format = :xml) ⇒ Object
get a list of no more than max squeaks closest to the given center_latitude/centerlongitude The format must either be :json or :xml. TODO: create a list of ClientSqueak objects, or make a get_squeak_objects function
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mapsqueak.rb', line 135 def get_squeaks(center_latitude,center_longitude,max = 100,format=:xml) # curl "http://192.168.0.2:3000/squeaks.xml?num_squeaks=3¢er_latitude=50.0¢er_longitude=-1.8" unless [:json, :xml].include?(format) $stderr.puts "Error: must be in json or xml" end squeak_string = `curl \'#{self.host}/squeaks.#{format.to_s}?num_squeaks=#{max}¢er_latitude=#{center_latitude}¢er_longitude=#{center_longitude}\'` puts squeak_string return squeak_str_to_objects(squeak_string,format) end |
#sign_in(email, password, format = :xml) ⇒ Object
sign in by email/password. note that a remember_token will be saved in a cookie file
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mapsqueak.rb', line 42 def sign_in(email,password,format=:xml) case format when :xml signin_xml = Document.new signin_xml.add_element('session') signin_xml.root << xml_element('email',email) # allow nil passwords right now signin_xml.root << xml_element('password',password.to_s) puts signin_xml.to_s @cookie_file = "#{email}.cookies" curl_str = "curl --data \'#{signin_xml.to_s}\' #{self.host}/sessions.xml -H \"Content-Type: application/xml\" --cookie-jar #{@cookie_file}" result = `#{curl_str}` puts result doc = Document.new(result) @user_id = XPath.first(doc,"hash/user-id").text when :post curl_str = "curl -F email=#{email} -F password=\'#{password}\' #{self.host}/sessions --cookie-jar #{@cookie_file}" `#{curl_str}` end end |
#sign_out ⇒ Object
sign the current user out. note that the cookie file will be deleted.
69 70 71 72 73 74 |
# File 'lib/mapsqueak.rb', line 69 def sign_out curl_str = "curl #{self.host}/sessions --request DELETE --cookie #{@cookie_file}" puts curl_str `#{curl_str}` File.unlink(@cookie_file) end |
#squeak(squeak, send_format = :xml, facebook_params = {}, verbose = false) ⇒ Object
post a new squeak. the squeak can either be whatever the ClientSqueak constructor accepts - a String of json or xml, or a hash, or it can be a ClientSqueak object. Talk about flexibility! the send_format must be :xml or :json facebook params is a Hash with either :facebook_test_user => true, or :access_token=> token
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/mapsqueak.rb', line 80 def squeak(squeak,send_format = :xml, facebook_params = {}, verbose=false) temp_squeak= nil case squeak when ClientSqueak temp_squeak= squeak when Hash temp_squeak= ClientSqueak.new(squeak) end unless [:json, :xml].include?(send_format) $stderr.puts "Error: send_format must be in json or xml" end format_str = send_format.to_s data = temp_squeak.send("to_#{format_str}") curl_str = "curl --data \'#{data}\' #{self.host}/squeaks.#{format_str} -H \"Content-Type: application/#{format_str}\" --cookie #{@cookie_file}" puts curl_str if verbose # execute the curl command res = `#{curl_str}` puts res if verbose actual_squeak = ClientSqueak.new(res) if facebook_params.include?(:access_token) user = Koala::Facebook::API.new(facebook_params[:access_token]) elsif(facebook_params.include?(:facebook_test_user)) test_users = Koala::Facebook::TestUsers.new(:app_id => '107582139349630', :secret => "ca16bbd5834ab7d4b012ec5e84a0d003") user_info = test_users.create(true, "offline_access,read_stream,manage_pages,publish_stream") login_url = user_info['login_url'] user = Koala::Facebook::API.new(user_info['access_token']) end unless user.nil? puts "Using the following facebook user: #{user.inspect}" picture_url = "http://maps.googleapis.com/maps/api/staticmap?center=#{update[:latitude]},#{update[:longitude]}&zoom=13&size=200x200&maptype=roadmap&markers=color:blue%7Clabel:M%7C#{update[:latitude]},#{update[:longitude]}&sensor=true" puts "Google image url: #{picture_url}" # Use google's static map api to get an image for the squeak id = user.put_wall_post("MapSqueak update at #{Time.now.strftime('')}",{:name => 'squeak name', :link => "#{opts.host}/squeaks/#{confirmation_return_hash['squeak']['id']}", :caption => opts.text, :description => "the description of the squeak, TBD", :picture => picture_url}) puts "Updated facebook with id: #{id}" puts "Visit #{login_url} to see it ..." unless login_url.nil? end # return the Squeak return actual_squeak end |
#squeak_str_to_objects(squeak_string, format) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/mapsqueak.rb', line 165 def squeak_str_to_objects(squeak_string,format) squeaks = [] case format when :xml doc = Document.new(squeak_string) doc.elements.each('squeaks/squeak') {|el| squeaks << ClientSqueak.new(el.to_s)} when :json obj = JSON.parse(squeak_string) # Note that gmaps4rails makes the json have a 'description' as opposed to 'text' obj.each {|s| puts s; squeaks << ClientSqueak.new(s) } end return squeaks end |