carddav 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f2c463be27a433b0595a26114f0e1955b1bdcb6f
4
+ data.tar.gz: f2f0f2e518319bf2b0f5be2b1c70624c48b3659f
5
+ SHA512:
6
+ metadata.gz: e9a8e9814dc1aeacab75bcfb8612afb35a37b82d1a501be47d654c9b769bcb99ae955f3ea64fa24b9758e1ca68c0911f68677d9be882a7959305f9197393eb02
7
+ data.tar.gz: 26bab95b8342b671f76df28c8762d46845aa764eb6d512780fd80cd0bd5365034139aba3e6b8269b598d57289f5d5d1f21c9e7d2df74543fc3fd18137485d6d2
data/.byebug_history ADDED
@@ -0,0 +1,97 @@
1
+ continue
2
+ t.first.parsed
3
+ t.first.name
4
+ t.size
5
+ t = res.xpath('//address-data/text()').map { |node| Carddav::Card.new node }
6
+ res.xpath('//address-data/text()')
7
+ res.css('address-data/text()')
8
+ res.xpath('address-data/text()')
9
+ res
10
+ cont
11
+ req.status
12
+ req
13
+ cont
14
+ url.new el.first.text
15
+ el.first.text
16
+ cont
17
+ continue
18
+ res.xpath('//response/href[following-sibling::propstat/prop/resourcetype/addressbook]')
19
+ res.xpath('//response/href[following-sibling::propstat/prop/resourcetype/addressbook]').size
20
+ res.xpath('//response/href[following-sibling::propstat/prop/resourcetype]').size
21
+ res.xpath('//response/href[following-sibling::propstat]').size
22
+ res.xpath('//response/href[following-sibling::addressbook]').size
23
+ res.xpath('//response/href[following-sibling:://addressbook]').size
24
+ res.xpath('//response/href[following-sibling::addressbook]').size
25
+ res.xpath('//response/href').size
26
+ res.xpath('//response//href').size
27
+ res.xpath('//response//resourcetype//addressbook').size
28
+ res.xpath('//response[boolean(//resourcetype//addressbook)]//href').size
29
+ res.xpath('//response[boolean(//resourcetype//addressbook]//href)').size
30
+ res.xpath('//response[//resourcetype//addressbook]//href').size
31
+ res.xpath('//response[//resourcetype//addressbook]//href')
32
+ res.xpath('//response[//resourcetype//addressbook]//href').first
33
+ el.first.text
34
+ el
35
+ cont
36
+ continue
37
+ res.xpath('//response[//resourcetype//addressbook]//href').first
38
+ res.xpath('//response[//resourcetype//addressbook]//href').class
39
+ res.xpath('//response[//resourcetype//addressbook]//href/text()').size
40
+ res.xpath('//response[//resourcetype//addressbook]//href//text()').size
41
+ res.xpath('//response[//resourcetype//addressbook]//href').size
42
+ res.xpath('//response[//resourcetype//addressbook]//href').
43
+ res.xpath '//response[//resourcetype//addressbook]//href'
44
+ puts res.to_xml
45
+ el.xpath('text()').text
46
+ el.xpath('text()')
47
+ el.xpath(text())
48
+ el
49
+ el.text
50
+ url
51
+ uel
52
+ cont
53
+ continue
54
+ url.new el.text
55
+ el.text
56
+ url
57
+ el
58
+ cont
59
+ el.text
60
+ continue
61
+ url.to_s
62
+ url
63
+ cont
64
+ continue
65
+ puts res
66
+ url
67
+ cont
68
+ continue
69
+ res.remove_namespaces!.css('addressbook-home-set').text
70
+ res.remove_namespaces!.css('addressbook-home-set href').text
71
+ res.remove_namespaces!.css 'addressbook-home-set href'
72
+ res.remove_namespaces!.css 'addressbook-home-set'
73
+ res
74
+ res.xpath 'addressbook-home-set'
75
+ res.css 'addressbook-home-set'
76
+ puts res
77
+ res
78
+ cont
79
+ continue
80
+ url
81
+ puts res
82
+ res.css('addressbook-home-set')
83
+ res.css('addressbook-home-set').first.to_xml
84
+ res.css('addressbook-home-set').first.text
85
+ res.css('addressbook-home-set').first
86
+ res.css('addressbook-home-set').size
87
+ res.css('addressbook-home-set').text
88
+ res.css('addressbook-home-set').children
89
+ res.css('addressbook-home-set').child
90
+ res.css 'addressbook-home-set'
91
+ res.css 'addressbook-home-set > href'
92
+ res.css 'addressbook-home-set href'
93
+ res.css 'addressbook-home-set'
94
+ res
95
+ node_name
96
+ cont
97
+ node_name
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ cache: bundler
3
+ sudo: false
4
+ rvm:
5
+ - 2.2.4
6
+ before_install: gem install bundler -v 1.11.2
7
+ notifications:
8
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'pry'
6
+ gem 'guard-rspec'
7
+ gem 'byebug'
data/Guardfile ADDED
@@ -0,0 +1,14 @@
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
+ require 'guard/rspec/dsl'
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+
5
+ # RSpec files
6
+ rspec = dsl.rspec
7
+ watch(rspec.spec_helper) { rspec.spec_dir }
8
+ watch(rspec.spec_support) { rspec.spec_dir }
9
+ watch(rspec.spec_files)
10
+
11
+ # Ruby files
12
+ ruby = dsl.ruby
13
+ dsl.watch_spec_files_for(ruby.lib_files)
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Tim Maslyuchenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Carddav
2
+
3
+ [![Build Status](https://travis-ci.org/timsly/carddav.svg?branch=master)](https://travis-ci.org/timsly/carddav)
4
+
5
+ Ruby implementation for CardDAV protocol.
6
+
7
+ It supports only getting cards right now.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'carddav'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install carddav
24
+
25
+ ## Usage
26
+
27
+ Gem was tested with two providers: iCloud and gmx.
28
+ To get contacts from them you need to do following:
29
+
30
+ ```ruby
31
+ service = Carddav.service :gmx, 'email@gmx.net', 'password'
32
+ service.cards
33
+ ```
34
+
35
+ or
36
+
37
+ ```ruby
38
+ service = Carddav.service :apple, 'email@icloud.com', 'password'
39
+ service.cards
40
+ ```
41
+
42
+ Under the hood it uses `Carddav::Client`. It uses standart approach of getting data
43
+ from CardDAV servers.
44
+
45
+ ```ruby
46
+ client = Carddav.client.new 'http://my-carddav-server.org', 'email@mail.net', 'password'
47
+ client.cards
48
+ ```
49
+
50
+ ## Discovery process
51
+
52
+ `Carddav::Client` discovers urls steps by step and than finally tried to get data from `addressbook_url`
53
+ and than parse vcards from response.
54
+
55
+ Here are all steps:
56
+
57
+ * getting current_user_principal url
58
+ * getting addressbook_home_set url
59
+ * getting addressbook url
60
+ * getting cards
61
+
62
+ Sometimes some of the url are static so it can be passed to the client.
63
+ This was we bypass some steps in the discovery process
64
+
65
+ ```ruby
66
+ client = Carddav.client.new 'http://my-carddav-server.org', 'email@mail.net', 'password'
67
+ client.addressbook_url = '/addressbook-url'
68
+ client.cards # will use http://my-carddav-server.org/addressbook-url and get cards from there.
69
+ ```
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
74
+
75
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/timsly/carddav.
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new :test
5
+
6
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'carddav'
5
+
6
+ require 'pry'
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/carddav.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carddav/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'carddav'
8
+ spec.version = Carddav::VERSION
9
+ spec.authors = ['Tim Maslyuchenko']
10
+ spec.email = ['insside@gmail.com']
11
+
12
+ spec.summary = 'CardDAV ruby implementation'
13
+ spec.description = 'CardDAV ruby implementation'
14
+ spec.homepage = 'https://github.com/timsly/carddav'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'curb'
23
+ spec.add_dependency 'nokogiri'
24
+ spec.add_dependency 'vcardigan'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.11'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.4'
29
+ spec.add_development_dependency 'webmock'
30
+ end
data/lib/carddav.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'curb'
2
+ require 'nokogiri'
3
+ require 'vcardigan'
4
+
5
+ require 'carddav/version'
6
+ require 'carddav/client'
7
+ require 'carddav/url'
8
+ require 'carddav/card'
9
+
10
+ module Carddav
11
+ SERVICES = {
12
+ apple: 'https://contacts.icloud.com',
13
+ gmx: { host: 'https://carddav.gmx.net', urls: {
14
+ current_user_principal: '/CardDavProxy/carddav/user-principal-uri'
15
+ }}
16
+ }
17
+
18
+ class InvalidResponse < Exception
19
+ attr_reader :response
20
+
21
+ def initialize(msg = nil, response = nil)
22
+ super msg
23
+ @response = response
24
+ end
25
+ end
26
+
27
+ class UnknownService < Exception
28
+ end
29
+
30
+ def self.service(name, username, password)
31
+ service = SERVICES[name.to_sym] || raise(UnknownService)
32
+ service = { host: service } unless service.is_a? Hash
33
+ client = Client.new service[:host], username, password
34
+ Array(service[:urls]).each do |key, value|
35
+ client.public_send "#{key}_url=", value
36
+ end
37
+ client
38
+ end
39
+ end
@@ -0,0 +1,31 @@
1
+ class Carddav::Card
2
+ attr_reader :raw, :parsed
3
+
4
+ def self.normalize(data)
5
+ data.to_s.gsub '&#13', ''
6
+ end
7
+
8
+ def initialize(data)
9
+ @raw = data.to_s
10
+ @parsed = VCardigan.parse self.class.normalize(data)
11
+ end
12
+
13
+ def name
14
+ vcard_field :fn
15
+ end
16
+
17
+ def email
18
+ vcard_field :email
19
+ end
20
+
21
+ def image
22
+ vcard_field :photo
23
+ end
24
+
25
+ private
26
+
27
+ def vcard_field(name)
28
+ field = parsed.send name.to_s
29
+ field && field.first && field.first.values.join
30
+ end
31
+ end
@@ -0,0 +1,84 @@
1
+ class Carddav::Client
2
+ NAMESPACES = { 'xmlns:D' => 'DAV:', 'xmlns:C' => 'urn:ietf:params:xml:ns:carddav' }
3
+
4
+ attr_reader :host, :username, :password, :current_user_principal_url,
5
+ :addressbook_home_set_url, :addressbook_url
6
+
7
+ def initialize(host, username, password)
8
+ @host = Carddav::Url.new host
9
+ @username = username
10
+ @password = password
11
+ end
12
+
13
+ def current_user_principal_url=(url)
14
+ @current_user_principal_url = host.new url
15
+ end
16
+
17
+ def addressbook_home_set_url=(url)
18
+ @addressbook_home_set_url = host.new url
19
+ end
20
+
21
+ def addressbook_url=(url)
22
+ @addressbook_url = host.new url
23
+ end
24
+
25
+ def current_user_principal_url
26
+ @current_user_principal_url ||= find_href host, 'D:current-user-principal'
27
+ end
28
+
29
+ def addressbook_home_set_url
30
+ @addressbook_home_set_url ||= find_href current_user_principal_url, 'C:addressbook-home-set'
31
+ end
32
+
33
+ def addressbook_url
34
+ @addressbook_url ||= find_href addressbook_home_set_url, 'D:resourcetype', 1 do |xml|
35
+ xml.xpath '//response/href[following-sibling::propstat/prop/resourcetype/addressbook]'
36
+ end
37
+ end
38
+
39
+ def cards
40
+ res = request addressbook_url, method: 'REPORT', depth: 1 do
41
+ Nokogiri::XML::Builder.new do |xml|
42
+ xml['C'].send 'addressbook-query', NAMESPACES do
43
+ xml['D'].prop do
44
+ xml['D'].getetag
45
+ xml['C'].send 'address-data'
46
+ end
47
+ end
48
+ end.to_xml
49
+ end
50
+ res.xpath('//address-data/text()').map { |node| Carddav::Card.new node }
51
+ end
52
+
53
+ private
54
+
55
+ def request(url, method: 'PROPFIND', depth: 0)
56
+ req = Curl::Easy.perform url.to_s do |c|
57
+ c.headers['Content-Type'] = 'application/xml'
58
+ c.headers['Depth'] = depth
59
+ c.userpwd = "#{username}:#{password}"
60
+ c.set :customrequest, method
61
+ c.set :postfields, yield if block_given?
62
+ end
63
+ if (status = /^(\d+)/.match(req.status)[0].to_i) && (status < 200 || status >= 300)
64
+ raise Carddav::InvalidResponse, "Invalid response from #{url}", req.body_str
65
+ end
66
+ Nokogiri::XML(req.body_str).remove_namespaces!
67
+ end
68
+
69
+ def find_href(url, node, depth = 0)
70
+ namespece, node_name = node.split ':'
71
+ res = request url, depth: depth do
72
+ Nokogiri::XML::Builder.new do |xml|
73
+ xml['D'].propfind NAMESPACES do
74
+ xml['D'].prop do
75
+ xml[namespece].send node_name
76
+ end
77
+ end
78
+ end.to_xml
79
+ end
80
+ el = block_given? ? yield(res) : res.css("#{node_name} href")
81
+ raise Carddav::InvalidResponse, "Cannot find #{node_name} node", res.to_xml if el.empty?
82
+ url.new el.first.text
83
+ end
84
+ end
@@ -0,0 +1,16 @@
1
+ class Carddav::Url
2
+ def initialize(uri)
3
+ @uri = URI uri
4
+ end
5
+
6
+ def new(path)
7
+ return self.class.new path if /^https?:\/\// =~ path
8
+ uri = @uri.dup
9
+ uri.path = path.start_with?('/') ? path : "/#{path}"
10
+ self.class.new uri
11
+ end
12
+
13
+ def to_s
14
+ @uri.to_s
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Carddav
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carddav
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Maslyuchenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: curb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: vcardigan
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: CardDAV ruby implementation
112
+ email:
113
+ - insside@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".byebug_history"
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - Guardfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - carddav.gemspec
130
+ - lib/carddav.rb
131
+ - lib/carddav/card.rb
132
+ - lib/carddav/client.rb
133
+ - lib/carddav/url.rb
134
+ - lib/carddav/version.rb
135
+ homepage: https://github.com/timsly/carddav
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.5.2
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: CardDAV ruby implementation
159
+ test_files: []