Class: ICloud::Api
- Inherits:
-
Object
- Object
- ICloud::Api
- Defined in:
- lib/icloud.rb
Overview
Your code goes hereā¦
Instance Attribute Summary collapse
-
#auth ⇒ Object
Returns the value of attribute auth.
-
#instance ⇒ Object
Returns the value of attribute instance.
-
#login ⇒ Object
Returns the value of attribute login.
-
#password ⇒ Object
Returns the value of attribute password.
Instance Method Summary collapse
- #apps ⇒ Object
- #authenticate(remember_me = false) ⇒ Object
- #authenticate? ⇒ Boolean
- #client_build_number ⇒ Object
- #client_id ⇒ Object
- #contacts ⇒ Object
- #cookie ⇒ Object
- #cookie=(v) ⇒ Object
- #dequote(str) ⇒ Object
- #ds_info ⇒ Object
-
#initialize(login, password) ⇒ Api
constructor
A new instance of Api.
- #validate ⇒ Object
- #version ⇒ Object
- #webservices ⇒ Object
Constructor Details
#initialize(login, password) ⇒ Api
Returns a new instance of Api.
30 31 32 33 34 35 |
# File 'lib/icloud.rb', line 30 def initialize(login, password) @login = login @password = password @auth = nil @instance = nil end |
Instance Attribute Details
#auth ⇒ Object
Returns the value of attribute auth.
28 29 30 |
# File 'lib/icloud.rb', line 28 def auth @auth end |
#instance ⇒ Object
Returns the value of attribute instance.
28 29 30 |
# File 'lib/icloud.rb', line 28 def instance @instance end |
#login ⇒ Object
Returns the value of attribute login.
28 29 30 |
# File 'lib/icloud.rb', line 28 def login @login end |
#password ⇒ Object
Returns the value of attribute password.
28 29 30 |
# File 'lib/icloud.rb', line 28 def password @password end |
Instance Method Details
#apps ⇒ Object
58 59 60 |
# File 'lib/icloud.rb', line 58 def apps Oj.load((@auth && @auth.body)||"{}")['apps'] end |
#authenticate(remember_me = false) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/icloud.rb', line 179 def authenticate(remember_me = false) @auth || begin validate conn = Faraday.new(:url => ICloud::URLS[:authenticate]) checksum = Digest::SHA1.hexdigest([login, instance].join).upcase auth_params = { "apple_id" => login, "password" => password, "id" => checksum, "extended_login" => remember_me } @auth = conn.post do |req| req.url '/setup/ws/1/login' req.headers["Origin"] = "https://www.icloud.com" req.headers["Referer"] = "https://www.icloud.com" req.headers["Cookie"] = req.params = { "clientBuildNumber" => client_build_number, "clientId" => client_id} req.body = Oj.dump(auth_params) end self. = @auth.env[:response_headers]["set-cookie"] self.instance = Oj.load(@auth.body)['instance'] end @auth end |
#authenticate? ⇒ Boolean
175 176 177 |
# File 'lib/icloud.rb', line 175 def authenticate? !!@auth end |
#client_build_number ⇒ Object
37 38 39 |
# File 'lib/icloud.rb', line 37 def client_build_number version["buildNumber"] end |
#client_id ⇒ Object
40 41 42 |
# File 'lib/icloud.rb', line 40 def client_id @uuid ||= UUID.new.generate.upcase end |
#contacts ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/icloud.rb', line 210 def contacts authenticate unless authenticate? @contacts || begin conn = Faraday.new(:url => webservices["contacts"]["url"] ) checksum = Digest::SHA1.hexdigest([login, instance].join).upcase resp = conn.get do |req| req.url '/co/startup' req.headers["Origin"] = "https://www.icloud.com" req.headers["Referer"] = "https://www.icloud.com" req.headers["Cookie"] = req.params = { "clientBuildNumber" => client_build_number, "clientId" => client_id, "dsid" => ds_info['dsid'], "clientVersion" => '2.1', "locale" => 'en_US', "order" => 'last,first', "id" => checksum } end @contacts = Oj.load(resp.body) end @contacts end |
#cookie ⇒ Object
140 141 142 |
# File 'lib/icloud.rb', line 140 def @cookie.to_s end |
#cookie=(v) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 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 131 132 133 134 135 136 137 138 |
# File 'lib/icloud.rb', line 68 def (v) @cookie = v.to_s.split(/,(?=[^;,]*=)|,$/).map { |c| = c.split(/;+/) first_elem = .shift first_elem.strip! key, value = first_elem.split(/\=/, 2) = { 'name' =>key, 'value' => value.dup } .each do |pair| pair.strip! key, value = pair.split(/=/, 2) next unless key value = dequote(value.strip) if value case key.downcase when 'domain' next unless value && !value.empty? begin ['domain'] = value ['for_domain'] = true rescue puts ("Couldn't parse domain: #{value}") end when 'path' next unless value && !value.empty? ['path'] = value when 'expires' next unless value && !value.empty? begin ['expires'] = Time::parse(value) rescue puts "Couldn't parse expires: #{value}" end when 'max-age' next unless value && !value.empty? begin ['max_age'] = Integer(value) rescue puts "Couldn't parse max age '#{value}'" end when 'comment' next unless value ['comment'] = value when 'version' next unless value begin ['version'] = Integer(value) rescue puts "Couldn't parse version '#{value}'" ['version'] = nil end when 'secure' ['secure'] = true end end ['secure'] ||= false # RFC 6265 4.1.2.2 ['expires'] = Time.now + ['max_age'] if ['max_age'] ['session'] = !['expires'] # Move this in to the cookie jar }.map{|j| "#{j['name']}=#{j['value']}"}.join(';') end |
#dequote(str) ⇒ Object
44 45 46 47 48 |
# File 'lib/icloud.rb', line 44 def dequote(str) ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup ret.gsub!(/\\(.)/, "\\1") ret end |
#ds_info ⇒ Object
54 55 56 |
# File 'lib/icloud.rb', line 54 def ds_info Oj.load((@auth && @auth.body)||"{}")['dsInfo'] end |
#validate ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/icloud.rb', line 158 def validate conn = Faraday.new(:url => URLS[:validate]) resp = conn.post do |req| req.url '/setup/ws/1/validate' req.headers["Origin"] = "https://www.icloud.com" req.headers["Referer"] = "https://www.icloud.com" req.headers["Cookie"] = req.params = { "clientBuildNumber" => client_build_number, "clientId" => client_id} end self. = resp.env[:response_headers]["set-cookie"] if resp.env[:response_headers].has_key?("set-cookie") self.instance = Oj.load(resp.body)['instance'] resp end |
#version ⇒ Object
153 154 155 156 |
# File 'lib/icloud.rb', line 153 def version @version_resp ||= Faraday.get(URLS[:version]) Oj.load(@version_resp.body) end |
#webservices ⇒ Object
50 51 52 |
# File 'lib/icloud.rb', line 50 def webservices Oj.load((@auth && @auth.body)||"{}")['webservices'] end |