familysearch 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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p362@familysearch"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.17.7 ()" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
35
+
36
+ # If you use bundler, this might be useful to you:
37
+ # if [[ -s Gemfile ]] && {
38
+ # ! builtin command -v bundle >/dev/null ||
39
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
40
+ # }
41
+ # then
42
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ # gem install bundler
44
+ # fi
45
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ # then
47
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
48
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ source "https://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem 'faraday', "~> 0.8.4"
6
+ gem 'faraday_middleware', "~> 0.9.0"
7
+ gem 'multi_json', "~> 1.5.0"
8
+ gem "hashie", "~> 2.0.2"
9
+ gem "rash", "~> 0.4.0"
10
+
11
+ # Add dependencies to develop your gem here.
12
+ # Include everything needed to run rake, tests, features, etc.
13
+ group :development, :test do
14
+ gem "rspec", "~> 2.13.0"
15
+ gem "shoulda", "~> 3.3.2"
16
+ gem "bundler", "~> 1.2.3"
17
+ gem "jeweler", "~> 1.8.4"
18
+ gem "vcr", "~> 2.4.0"
19
+ gem "webmock", "~> 1.10.0"
20
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Jimmy Zimmerman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # familysearch gem
2
+
3
+ ## Installation
4
+
5
+ You can install it from the commandline.
6
+
7
+ gem install familysearch
8
+
9
+ Or add it to a Gemfile for use with Bundler
10
+
11
+ gem "familysearch", "~> 0.1.0 "
12
+
13
+
14
+ ## Basic Usage
15
+
16
+ Here's how to use it
17
+
18
+ require 'rubygems'
19
+ require 'familysearch'
20
+
21
+ # Instantiate a Client object
22
+ client = FamilySearch::Client.new :environment => :sandbox, :key => 'your-dev-key-here'
23
+
24
+ # Load the Discovery resource
25
+ client.discover!
26
+
27
+ # For testing, you can use basic auth to get a session,
28
+ # Don't do this in your production web app. Use OAuth 2.0
29
+ client.basic_auth! 'api-user-1241', '1782'
30
+
31
+ me = client.get(client.discovery.links.current_user_person.href).body
32
+
33
+ More documentation coming soon...
34
+
35
+ ## Copyright
36
+
37
+ Copyright (c) 2013 Jimmy Zimmerman. See LICENSE.txt for
38
+ further details.
39
+
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "familysearch"
16
+ gem.homepage = "http://github.com/jimmyz/familysearch-rb"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{A gem for the FamilySearch Platform.}
19
+ gem.description = %Q{A gem for the FamilySearch Platform. Documentation for the FamilySearch Platform can be found at https://}
20
+ gem.email = "jimmy.zimmerman@gmail.com"
21
+ gem.authors = ["Jimmy Zimmerman"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core/rake_task'
29
+ RSpec::Core::RakeTask.new(:spec)
30
+ task :default => :spec
31
+
32
+ require 'rdoc/task'
33
+ Rake::RDocTask.new do |rdoc|
34
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
35
+
36
+ rdoc.rdoc_dir = 'rdoc'
37
+ rdoc.title = "familysearch #{version}"
38
+ rdoc.rdoc_files.include('README*')
39
+ rdoc.rdoc_files.include('lib/**/*.rb')
40
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,87 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "familysearch"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jimmy Zimmerman"]
12
+ s.date = "2013-03-18"
13
+ s.description = "A gem for the FamilySearch Platform. Documentation for the FamilySearch Platform can be found at https://"
14
+ s.email = "jimmy.zimmerman@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rvmrc",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "familysearch.gemspec",
28
+ "lib/familysearch.rb",
29
+ "lib/familysearch/client.rb",
30
+ "spec/client_spec.rb",
31
+ "spec/familysearch_spec.rb",
32
+ "spec/fixtures/vcr_cassettes/discovery.yml",
33
+ "spec/fixtures/vcr_cassettes/discovery_auth.yml",
34
+ "spec/spec_helper.rb",
35
+ "workspace/notes/APIDesign.txt",
36
+ "workspace/notes/discovery_links.txt",
37
+ "workspace/notes/goals.txt"
38
+ ]
39
+ s.homepage = "http://github.com/jimmyz/familysearch-rb"
40
+ s.licenses = ["MIT"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = "1.8.24"
43
+ s.summary = "A gem for the FamilySearch Platform."
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<faraday>, ["~> 0.8.4"])
50
+ s.add_runtime_dependency(%q<faraday_middleware>, ["~> 0.9.0"])
51
+ s.add_runtime_dependency(%q<multi_json>, ["~> 1.5.0"])
52
+ s.add_runtime_dependency(%q<hashie>, ["~> 2.0.2"])
53
+ s.add_runtime_dependency(%q<rash>, ["~> 0.4.0"])
54
+ s.add_development_dependency(%q<rspec>, ["~> 2.13.0"])
55
+ s.add_development_dependency(%q<shoulda>, ["~> 3.3.2"])
56
+ s.add_development_dependency(%q<bundler>, ["~> 1.2.3"])
57
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
58
+ s.add_development_dependency(%q<vcr>, ["~> 2.4.0"])
59
+ s.add_development_dependency(%q<webmock>, ["~> 1.10.0"])
60
+ else
61
+ s.add_dependency(%q<faraday>, ["~> 0.8.4"])
62
+ s.add_dependency(%q<faraday_middleware>, ["~> 0.9.0"])
63
+ s.add_dependency(%q<multi_json>, ["~> 1.5.0"])
64
+ s.add_dependency(%q<hashie>, ["~> 2.0.2"])
65
+ s.add_dependency(%q<rash>, ["~> 0.4.0"])
66
+ s.add_dependency(%q<rspec>, ["~> 2.13.0"])
67
+ s.add_dependency(%q<shoulda>, ["~> 3.3.2"])
68
+ s.add_dependency(%q<bundler>, ["~> 1.2.3"])
69
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
70
+ s.add_dependency(%q<vcr>, ["~> 2.4.0"])
71
+ s.add_dependency(%q<webmock>, ["~> 1.10.0"])
72
+ end
73
+ else
74
+ s.add_dependency(%q<faraday>, ["~> 0.8.4"])
75
+ s.add_dependency(%q<faraday_middleware>, ["~> 0.9.0"])
76
+ s.add_dependency(%q<multi_json>, ["~> 1.5.0"])
77
+ s.add_dependency(%q<hashie>, ["~> 2.0.2"])
78
+ s.add_dependency(%q<rash>, ["~> 0.4.0"])
79
+ s.add_dependency(%q<rspec>, ["~> 2.13.0"])
80
+ s.add_dependency(%q<shoulda>, ["~> 3.3.2"])
81
+ s.add_dependency(%q<bundler>, ["~> 1.2.3"])
82
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
83
+ s.add_dependency(%q<vcr>, ["~> 2.4.0"])
84
+ s.add_dependency(%q<webmock>, ["~> 1.10.0"])
85
+ end
86
+ end
87
+
@@ -0,0 +1 @@
1
+ require 'familysearch/client'
@@ -0,0 +1,65 @@
1
+ require 'hashie'
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+ require 'forwardable'
5
+
6
+ module FamilySearch
7
+ class Client
8
+ ENV_CONF = {
9
+ :production => {
10
+ :base_url => 'https://familysearch.org',
11
+ :discovery_path => '/.well-known/app-meta'
12
+ },
13
+ :staging => {
14
+ :base_url => 'https://stage.familysearch.org',
15
+ :discovery_path => '/.well-known/app-meta'
16
+ },
17
+ :sandbox => {
18
+ :base_url => 'https://sandbox.familysearch.org',
19
+ :discovery_path => '/.well-known/app-meta'
20
+ }
21
+ }
22
+
23
+ attr_accessor :access_token, :logger, :key, :environment, :discovery, :agent
24
+ attr_reader :base_url
25
+
26
+ extend Forwardable
27
+ def_delegators :@agent, :get, :put, :post, :delete, :head, :options
28
+
29
+ def initialize(options = {})
30
+ @access_token = options[:access_token] # if options[:access_token]
31
+ @logger = options[:logger]
32
+ @key = options[:key]
33
+ @environment = options[:environment] || :sandbox
34
+ @base_url = options[:base_url] || ENV_CONF[@environment][:base_url]
35
+ @agent = Faraday.new(@base_url) do |faraday|
36
+ faraday.response :logger, options[:logger] if options[:logger]
37
+ faraday.response :rashify
38
+ faraday.response :json
39
+ faraday.response :follow_redirects, :limit => 3, :standards_compliant => true
40
+ faraday.headers['Accept'] = 'application/x-fs-v1+json'
41
+ faraday.authorization('Bearer',@access_token) if @access_token
42
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
43
+ end
44
+ end
45
+
46
+ def discover!
47
+ @discovery ||= get_discovery
48
+ end
49
+
50
+ def basic_auth!(username,password,key=nil)
51
+ self.discover!
52
+ @key ||= key if key
53
+ @agent.basic_auth username, password
54
+ response = @agent.get @discovery.links.fs_identity_v2_login.href, :dataFormat => 'application/json', :key => @key
55
+ @access_token = response.body.session.id
56
+ @agent.authorization('Bearer',@access_token)
57
+ end
58
+
59
+ private
60
+ def get_discovery
61
+ result = @agent.get(ENV_CONF[@environment][:discovery_path])
62
+ result.body
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,132 @@
1
+ require 'spec_helper'
2
+ require 'familysearch/client'
3
+
4
+ describe FamilySearch::Client do
5
+
6
+ describe "instantiating" do
7
+
8
+ it "should accept a hash of config options" do
9
+ client = FamilySearch::Client.new {}
10
+ end
11
+
12
+ it "should accept a logger option" do
13
+ require 'logger'
14
+ logger = Logger.new STDOUT
15
+ client = FamilySearch::Client.new :logger => logger
16
+ client.logger.should == logger
17
+ end
18
+
19
+ it "should accept an access_token" do
20
+ token = 'access-token'
21
+ client = FamilySearch::Client.new :access_token => token
22
+ client.access_token.should == token
23
+ end
24
+
25
+ it "should only set the access_token if passed in the options" do
26
+ client = FamilySearch::Client.new
27
+ client.access_token.should be(nil)
28
+ client.agent.headers['Authorization'].should be(nil)
29
+ end
30
+
31
+ it "should accept a developer key" do
32
+ key = 'Jimmyz-cool-dev-key'
33
+ client = FamilySearch::Client.new :key => key
34
+ client.key.should == key
35
+ end
36
+
37
+ it "should accept an environment variable (:production)" do
38
+ client = FamilySearch::Client.new :environment => :production
39
+ client.environment.should == :production
40
+ client.base_url.should == 'https://familysearch.org'
41
+ end
42
+
43
+ it "should accept an environment variable (:staging)" do
44
+ client = FamilySearch::Client.new :environment => :staging
45
+ client.environment.should == :staging
46
+ client.base_url.should == 'https://stage.familysearch.org'
47
+ end
48
+
49
+ it "should accept an environment variable (:sandbox)" do
50
+ client = FamilySearch::Client.new :environment => :sandbox
51
+ client.environment.should == :sandbox
52
+ client.base_url.should == 'https://sandbox.familysearch.org'
53
+ end
54
+
55
+ it "should default to sandbox environment" do
56
+ client = FamilySearch::Client.new
57
+ client.environment.should == :sandbox
58
+ client.base_url.should == 'https://sandbox.familysearch.org'
59
+ end
60
+
61
+ it "should allow the overriding of the base_url" do
62
+ client = FamilySearch::Client.new :base_url => 'http://localhost:8080'
63
+ client.base_url.should == 'http://localhost:8080'
64
+ end
65
+
66
+ it "should instantiate a Faraday instance accessible via .agent" do
67
+ client = FamilySearch::Client.new
68
+ client.agent.is_a? Faraday
69
+ end
70
+ end
71
+
72
+ describe "get" do
73
+ def client
74
+ @client = FamilySearch::Client.new
75
+ end
76
+
77
+ it "should accept a path and return an object" do
78
+ VCR.use_cassette('discovery') do
79
+ obj = client.get 'https://sandbox.familysearch.org/.well-known/app-meta'
80
+ obj.is_a? Object
81
+ end
82
+ end
83
+
84
+ end
85
+
86
+ describe "discover!" do
87
+ def client
88
+ @client ||= FamilySearch::Client.new
89
+ end
90
+
91
+ it "should make a request " do
92
+ VCR.use_cassette('discovery') do
93
+ client.discover!
94
+ client.discovery.links.fs_identity_v2_login.href.should == 'https://sandbox.familysearch.org/identity/v2/login'
95
+ client.discovery['links']['fs-identity-v2-login']['href'].should == 'https://sandbox.familysearch.org/identity/v2/login'
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "basic_auth!" do
101
+ def client()
102
+ @client ||= FamilySearch::Client.new(:key => 'WCQY-7J1Q-GKVV-7DNM-SQ5M-9Q5H-JX3H-CMJK' )
103
+ end
104
+
105
+ it "should call discover! if it hasn't already" do
106
+ VCR.use_cassette('discovery_auth') do
107
+ client.should_receive(:discover!).and_call_original
108
+ client.basic_auth! 'api-user-1241', '1782'
109
+ end
110
+ end
111
+
112
+ it "should accept a username and password" do
113
+ VCR.use_cassette('discovery_auth') do
114
+ client.basic_auth! 'api-user-1241', '1782'
115
+ end
116
+ end
117
+
118
+ it "should make a call to the fs-identity-v2-login with credentials and set the access_token" do
119
+ VCR.use_cassette('discovery_auth') do
120
+ client.basic_auth! 'api-user-1241', '1782'
121
+ client.access_token.should == 'USYS8B6B487A084AA3B3C027451E23D20D5E_nbci-045-034.d.usys.fsglobal.net'
122
+ end
123
+ end
124
+
125
+ it "should set the agent's authorization to Bearer with the access token" do
126
+ VCR.use_cassette('discovery_auth') do
127
+ client.agent.should_receive(:authorization).with('Bearer','USYS8B6B487A084AA3B3C027451E23D20D5E_nbci-045-034.d.usys.fsglobal.net').and_call_original
128
+ client.basic_auth! 'api-user-1241', '1782'
129
+ end
130
+ end
131
+ end
132
+ end