f1api 0.9.11 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,14 +26,15 @@ module FellowshipOneAPI
26
26
  def transform_response(response_body, path)
27
27
  json = JSON.parse(response_body)
28
28
  if json.keys.first == "results"
29
- json = json["results"]
30
- key = json.keys.find {|k| k[0] != '@'}
31
- key = key.first if key.is_a? Enumerator
32
- ret = []
33
- json[key].each do |person|
34
- ret << person
29
+ results = json["results"]["person"]
30
+ (json["results"].keys.find_all {|key| key[0] == '@' && key != '@array'}).each do |key|
31
+ results.each do |result|
32
+ result.merge!({key => json["results"][key]})
33
+ end
35
34
  end
36
- JSON.dump({"people" => ret})
35
+ JSON.dump(results)
36
+ elsif !json[json.keys.first][json.keys.first.singularize].nil?
37
+ JSON.dump(json[json.keys.first][json.keys.first.singularize])
37
38
  else
38
39
  JSON.dump(json[json.keys.first])
39
40
  end
@@ -48,9 +48,9 @@ module FellowshipOneAPI
48
48
  end
49
49
 
50
50
  if(args[:auth_against])
51
- load_consumer_config args[:auth_against]
51
+ load_consumer_config args[:auth_against], args[:site_url]
52
52
  else
53
- load_consumer_config
53
+ load_consumer_config :portal, args[:site_url]
54
54
  end
55
55
 
56
56
  if(args[:oauth_token] and args[:oauth_token_secret])
@@ -3,72 +3,84 @@ module FellowshipOneAPI # :nodoc:
3
3
  #
4
4
  # This class was written to take rails environment variables like +RAILS_ENV+ and +Rails.root+ into account
5
5
  class Configuration
6
- # Explictly defines where the configuration file is
7
- def self.file_path=(path)
8
- @config_yaml = nil
9
- @file_path = path
10
- end
6
+ class <<self
7
+ # Explictly defines where the configuration file is
8
+ def file_path=(path)
9
+ @config_yaml = nil
10
+ @file_path = path
11
+ end
11
12
 
12
- # Reload the configuration file
13
- def self.reload
14
- load_yaml
15
- end
13
+ # Reload the configuration file
14
+ def reload
15
+ load_yaml
16
+ end
16
17
 
17
- # Gets the specified key from the configuration file
18
- # [Example] FellowshipTechAPIClient.Configuration["consumer_key"] <i># "2"</i>
19
- #
20
- # FellowshipTechAPIClient.Configuration["consumer_secret"] <i># "12345678-9abc-def0-1234-567890abcdef"</i>
21
- def self.[](value)
22
- load_yaml if @config_yaml.nil?
23
- val = @config_yaml[self.environment][value]
24
- # if we have the string has "{church_code}" then we'll substitute it
25
- if val =~ /\{church_code\}/ and value != "church_code"
26
- return val.gsub("{church_code}", self["church_code"])
18
+ # Gets the specified key from the configuration file
19
+ # [Example] FellowshipTechAPIClient.Configuration["consumer_key"] <i># "2"</i>
20
+ #
21
+ # FellowshipTechAPIClient.Configuration["consumer_secret"] <i># "12345678-9abc-def0-1234-567890abcdef"</i>
22
+ def [](value)
23
+ load_yaml if @config_yaml.nil?
24
+ val = @config_yaml[self.environment][value]
25
+ # if we have the string has "{church_code}" then we'll substitute it
26
+ if val =~ /\{church_code\}/ and value != "church_code"
27
+ return val.gsub("{church_code}", self["church_code"])
28
+ end
29
+ return val
27
30
  end
28
- return val
29
- end
30
31
 
31
- # Gets the current environment
32
- def self.environment
33
- @environment ||= "development"
34
- @environment ||= ::Rails.env if defined? ::Rails
35
- @environment
36
- end
32
+ # Gets the current environment
33
+ def environment
34
+ @environment ||= "development"
35
+ @environment ||= ::Rails.env if defined? ::Rails
36
+ @environment
37
+ end
37
38
 
38
- # Set the current environment
39
- def self.environment=(env_value)
40
- @environment = env_value
41
- end
39
+ # Set the current environment
40
+ def environment=(env_value)
41
+ @environment = env_value
42
+ end
42
43
 
43
- # Overridden method_missing to facilitate a more pleasing ruby-like syntax for accessing
44
- # configuration values
45
- def self.method_missing(name, *args, &block)
46
- return self[name.to_s] unless self[name.to_s].nil?
47
- super
48
- end
44
+ # Overridden method_missing to facilitate a more pleasing ruby-like syntax for accessing
45
+ # configuration values
46
+ def method_missing(name, *args, &block)
47
+ return self[name.to_s] unless self[name.to_s].nil?
48
+ super
49
+ end
50
+
51
+ # Replace the "{church_code}" string with a custom string
52
+ # Meant for 1st party implementations
53
+ def url_with_church_code(church_code)
54
+ val = @config_yaml[self.environment]["url"]
55
+ # if we have the string has "{church_code}" then we'll substitute it
56
+ if val =~ /\{church_code\}/ and value != "church_code"
57
+ return val.gsub("{church_code}", church_code)
58
+ end
59
+ end
49
60
 
50
- private
61
+ private
51
62
 
52
- # Loads the YAML file
53
- #
54
- # Starts by looking to see if file_path is defined then checks in current directory (.) and then your Rails.root and then the config
55
- # directory off of the base directory of the gem
56
- def self.load_yaml
57
- begin
58
- if not @file_path.nil?
59
- @config_yaml = YAML.load_file(@file_path)
60
- elsif File.exists? "./f1-oauth.yml"
61
- @config_yaml = YAML.load_file("./f1-oauth.yml")
62
- elsif defined? ::Rails
63
- @config_yaml = YAML.load_file("#{::Rails.root}/config/f1-oauth.yml")
64
- else
65
- path = File.dirname(__FILE__) + "/../../config/f1-oauth.yml"
66
- @config_yaml = YAML.load_file(path)
63
+ # Loads the YAML file
64
+ #
65
+ # Starts by looking to see if file_path is defined then checks in current directory (.) and then your Rails.root and then the config
66
+ # directory off of the base directory of the gem
67
+ def load_yaml
68
+ begin
69
+ if not @file_path.nil?
70
+ @config_yaml = YAML.load_file(@file_path)
71
+ elsif File.exists? "./f1-oauth.yml"
72
+ @config_yaml = YAML.load_file("./f1-oauth.yml")
73
+ elsif defined? ::Rails
74
+ @config_yaml = YAML.load_file("#{::Rails.root.to_s}/config/f1-oauth.yml")
75
+ else
76
+ path = File.dirname(__FILE__) + "/../../config/f1-oauth.yml"
77
+ @config_yaml = YAML.load_file(path)
78
+ end
79
+ true
80
+ rescue Exception => ex
81
+ puts "There was an error: #{ex.message}"
82
+ false
67
83
  end
68
- true
69
- rescue Exception => ex
70
- puts "There was an error: #{ex.message}"
71
- false
72
84
  end
73
85
  end
74
86
  end
@@ -19,7 +19,7 @@ module FellowshipOneAPI # :nodoc:
19
19
  alias :consumer_secret= :oauth_consumer_secret=
20
20
 
21
21
  # The OAuth access token object where all requests are made off of
22
- attr_reader :oauth_access_token
22
+ attr_accessor :oauth_access_token
23
23
  alias :access_token :oauth_access_token
24
24
 
25
25
  # The OAuth consumer object
@@ -30,7 +30,7 @@ module FellowshipOneAPI # :nodoc:
30
30
  attr_reader :authenticated_user_uri
31
31
 
32
32
  # Creates the OAuth consumer object
33
- def load_consumer_config(type = :portal)
33
+ def load_consumer_config(type = :portal, site_url = nil)
34
34
  case type
35
35
  when :portal
36
36
  authorize_path = FellowshipOneAPI::Configuration.portal_authorize_path
@@ -41,9 +41,10 @@ module FellowshipOneAPI # :nodoc:
41
41
  @oauth_consumer_key ||= FellowshipOneAPI::Configuration.consumer_key
42
42
  @oauth_consumer_secret ||= FellowshipOneAPI::Configuration.consumer_secret
43
43
 
44
- @oauth_consumer = ::OAuth::Consumer.new(@oauth_consumer_key,
44
+ url = site_url.nil? ? FellowshipOneAPI::Configuration.site_url : site_url
45
+ @oauth_consumer = ::OAuth::Consumer.new(@oauth_consumer_key,
45
46
  @oauth_consumer_secret,
46
- {:site => FellowshipOneAPI::Configuration.site_url,
47
+ {:site => url,
47
48
  :request_token_path => FellowshipOneAPI::Configuration.request_token_path,
48
49
  :access_token_path => FellowshipOneAPI::Configuration.access_token_path,
49
50
  :authorize_path => authorize_path })
@@ -3,34 +3,39 @@ module FellowshipOneAPI # :nodoc:
3
3
  # Implements the Credentials method of authentication. You must manage the credentials.
4
4
  module CredentialsAuthentication
5
5
  include OAuth
6
- # Authorizes a user
6
+ # Authenticates a user and throws and error if unable
7
7
  # +username+:: The username of the user
8
8
  # +password+:: The password of the user
9
9
  # +type+:: Can be :portal or :weblink based on which credentials you want to authenticate against
10
10
  # Returns the URI for the authenticated user
11
11
  def authenticate!(username, password, type = :portal)
12
- load_consumer_config(type) if @oauth_consumer.nil?
13
-
14
- cred = URI.encode(Base64.encode64("#{username} #{password}"))
15
-
16
- case type
17
- when :portal
18
- auth_url = FellowshipOneAPI::Configuration.portal_credential_token_path
19
- when :weblink
20
- auth_url = FellowshipOneAPI::Configuration.weblink_credential_token_path
21
- end
22
-
23
- response = @oauth_consumer.request(:post, auth_url, nil, {}, "ec=#{cred}", {'Content-Type' => 'application/x-www-form-urlencoded'})
24
-
12
+ response = get_api_response(username, password, type)
13
+
25
14
  handle_response_code(response)
26
-
27
15
  # Gettting the URI of the authenticated user
28
16
  @authenticated_user_uri = response["Content-Location"]
29
17
  end
30
18
  alias :authorize! :authenticate!
31
19
 
20
+ # Authenticates a user and returns true if successful
21
+ # +username+:: The username of the user
22
+ # +password+:: The password of the user
23
+ # +type+:: Can be :portal or :weblink based on which credentials you want to authenticate against
24
+ # Returns _true_ if was able to authenticate, _false_ if not
25
+ def authenticate(username, password, type = :portal)
26
+ response = get_api_response(username, password, type)
27
+
28
+ if(response.code.to_i == 400)
29
+ return false
30
+ end
31
+ handle_response_code(response)
32
+ @authenticated_user_uri = response["Content-Location"]
33
+ return true
34
+ end
35
+ alias :authorize :authenticate
36
+
32
37
  private
33
- def handle_response_code(response)
38
+ def handle_response_code(response)
34
39
  case response.code.to_i
35
40
  when (200..299)
36
41
  @oauth_access_token = ::OAuth::AccessToken.from_hash(@oauth_consumer, parse_access_token(response.body))
@@ -44,7 +49,22 @@ module FellowshipOneAPI # :nodoc:
44
49
  response.error!
45
50
  end
46
51
  end
47
-
52
+
53
+ def get_api_response(username, password, type)
54
+ load_consumer_config(type) if @oauth_consumer.nil?
55
+
56
+ cred = URI.encode(Base64.encode64("#{username} #{password}"))
57
+
58
+ case type
59
+ when :portal
60
+ auth_url = FellowshipOneAPI::Configuration.portal_credential_token_path
61
+ when :weblink
62
+ auth_url = FellowshipOneAPI::Configuration.weblink_credential_token_path
63
+ end
64
+
65
+ @oauth_consumer.request(:post, auth_url, nil, {}, "ec=#{cred}", {'Content-Type' => 'application/x-www-form-urlencoded'})
66
+ end
67
+
48
68
  # Parse returned OAuth access token key/secret pair
49
69
  def parse_access_token(response)
50
70
  oauth_hash = {}
@@ -1,13 +1,15 @@
1
1
  class HttpFixture
2
+ attr_accessor :code
3
+
4
+ def initialize(code = "200")
5
+ self.code = code
6
+ end
7
+
2
8
  def body
3
9
  "oauth_token=access&oauth_token_secret=token"
4
10
  end
5
-
11
+
6
12
  def [](value)
7
13
  {'Content-Location' => "#{FellowshipOneAPI::Configuration.site_url}/V1/People/123456"}[value]
8
14
  end
9
-
10
- def code
11
- "200"
12
- end
13
- end
15
+ end
@@ -10,18 +10,18 @@ class CredentialsTest < Test::Unit::TestCase
10
10
  def setup
11
11
  Configuration.environment = "test"
12
12
  Configuration.file_path = "#{File.dirname(__FILE__)}/../../config/f1-oauth.yml"
13
-
13
+
14
14
  @test_username = "testuser"
15
15
  @test_password = "testpass"
16
-
16
+
17
17
  @cred_test = CredentialsAuthenticationTest.new
18
18
  @cred_test.load_consumer_config
19
19
  @mocked_access_token = AccessTokenFixture.get(@cred_test.oauth_consumer)
20
20
  @mocked_request_token = RequestTokenFixture.get(@cred_test.oauth_consumer)
21
21
  cred = URI.encode(Base64.encode64("#{@test_username} #{@test_password}"))
22
-
22
+
23
23
  @mocked_user_uri = "#{Configuration.site_url}/V1/People/123456"
24
-
24
+
25
25
  @cred_test.oauth_consumer.stubs(:get_request_token).returns(@mocked_request_token)
26
26
 
27
27
  @cred_test.oauth_consumer.stubs(:request).with(:post, ::FellowshipOneAPI::Configuration.portal_credential_token_path,
@@ -57,4 +57,28 @@ class CredentialsTest < Test::Unit::TestCase
57
57
 
58
58
  assert_equal(@mocked_user_uri, @cred_test.authenticated_user_uri)
59
59
  end
60
+
61
+ def test_bool_authenticate_with_valid_creds
62
+ val = @cred_test.authenticate @test_username, @test_password
63
+
64
+ assert(val)
65
+ end
66
+
67
+ def test_bool_authenticate_with_invalid_creds
68
+ @cred_test.oauth_consumer.unstub(:request)
69
+ @cred_test.oauth_consumer.unstub(:get_request_token)
70
+ cred = URI.encode(Base64.encode64("#{@test_username} badpassword"))
71
+
72
+ @cred_test.oauth_consumer.expects(:request).with(:post, ::FellowshipOneAPI::Configuration.portal_credential_token_path,
73
+ nil, {}, "ec=#{cred}", {'Content-Type' => 'application/x-www-form-urlencoded'}).returns(
74
+ HttpFixture.new("400")).at_least_once
75
+ val = @cred_test.authenticate @test_username, "badpassword"
76
+ assert val == false
77
+ end
78
+
79
+ def test_site_url
80
+ @cred_test.load_consumer_config :portal, "http://foo.com"
81
+
82
+ assert_equal "http://foo.com", @cred_test.oauth_consumer.site
83
+ end
60
84
  end
@@ -72,5 +72,4 @@ class OAuthTest < Test::Unit::TestCase
72
72
  @oauth_test.authorize!
73
73
  assert_equal("#{Configuration.site_url}#{Configuration.weblink_authorize_path}", @oauth_test.oauth_consumer.authorize_url)
74
74
  end
75
-
76
75
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: f1api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.11
4
+ version: 0.10.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-05-06 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth
16
- requirement: &2156900320 !ruby/object:Gem::Requirement
16
+ requirement: &2157161600 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.4.4
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2156900320
24
+ version_requirements: *2157161600
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activeresource
27
- requirement: &2156899900 !ruby/object:Gem::Requirement
27
+ requirement: &2157161160 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,21 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2156899900
36
- - !ruby/object:Gem::Dependency
37
- name: oauth
38
- requirement: &2156899280 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: 0.4.4
44
- type: :runtime
45
- prerelease: false
46
- version_requirements: *2156899280
35
+ version_requirements: *2157161160
47
36
  - !ruby/object:Gem::Dependency
48
37
  name: mocha
49
- requirement: &2156898800 !ruby/object:Gem::Requirement
38
+ requirement: &2157160520 !ruby/object:Gem::Requirement
50
39
  none: false
51
40
  requirements:
52
41
  - - ! '>='
@@ -54,10 +43,10 @@ dependencies:
54
43
  version: '0'
55
44
  type: :development
56
45
  prerelease: false
57
- version_requirements: *2156898800
46
+ version_requirements: *2157160520
58
47
  - !ruby/object:Gem::Dependency
59
48
  name: activeresource
60
- requirement: &2156898320 !ruby/object:Gem::Requirement
49
+ requirement: &2157160000 !ruby/object:Gem::Requirement
61
50
  none: false
62
51
  requirements:
63
52
  - - ! '>='
@@ -65,7 +54,7 @@ dependencies:
65
54
  version: '0'
66
55
  type: :runtime
67
56
  prerelease: false
68
- version_requirements: *2156898320
57
+ version_requirements: *2157160000
69
58
  description: ! 'Consumes the Fellowship One API in your apps using ActiveResource. Implements
70
59
  2nd party credentials-based authenticaion and full OAuth implementation. '
71
60
  email: jesse.dearing@activenetwork.com