wordnik 0.3.6 → 0.3.7

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/README.md CHANGED
@@ -22,8 +22,10 @@ Then from your project's RAILS_ROOT, run:
22
22
  Create config/initializers/wordnik.rb and drop this in:
23
23
 
24
24
  Wordnik.configure do |config|
25
- config.api_key = '12345abcde'
26
- config.response_format = :json # defaults to json, but xml is also supported
25
+ config.api_key = '12345abcde' # required
26
+ config.username = 'bozo' # optional, but needed for user-related functions
27
+ config.password = 'cl0wnt0wn' # optional, but needed for user-related functions
28
+ config.response_format = :json # defaults to json, but xml is also supported
27
29
  end
28
30
 
29
31
  ### Rails 2.x
@@ -73,6 +75,11 @@ Contributing
73
75
  * Commit and push until you are happy with your contribution
74
76
  * Make sure to add tests for the feature/bugfix. This is important so we don't break it in a future version unintentionally.
75
77
 
78
+ Wishlist
79
+ --------
80
+
81
+ * Allow boolean params to really party like booleans (instead of 'true')
82
+
76
83
  Props
77
84
  -----
78
85
 
@@ -23,12 +23,15 @@ module Wordnik
23
23
  #
24
24
  # @example
25
25
  # Wordnik.configure do |config|
26
- # config.api_key = '1234567890abcdef'
27
- # config.response_format = :json
26
+ # config.api_key = '1234567890abcdef' # required
27
+ # config.username = 'wordlover' # optional, but needed for user-related functions
28
+ # config.password = 'i<3words' # optional, but needed for user-related functions
29
+ # config.response_format = :json # optional, defaults to json
28
30
  # end
29
31
  def configure
30
32
  self.configuration ||= Configuration.new
31
33
  yield(configuration) if block_given?
34
+
32
35
  self.build_resources
33
36
  end
34
37
 
@@ -46,6 +49,26 @@ module Wordnik
46
49
  self.resources[name] = resource
47
50
  end
48
51
  end
52
+
53
+ def authenticated?
54
+ Wordnik.configuration.user_id.present? && Wordnik.configuration.auth_token.present?
55
+ end
56
+
57
+ def de_authenticate
58
+ Wordnik.configuration.user_id = nil
59
+ Wordnik.configuration.auth_token = nil
60
+ end
61
+
62
+ def authenticate
63
+ return if Wordnik.authenticated?
64
+ response_body = Wordnik.account.get_authenticate(Wordnik.configuration.username, :password => Wordnik.configuration.password)
65
+ if response_body.is_a?(Hash) && response_body['userId'].present? && response_body['token'].present?
66
+ Wordnik.configuration.user_id = response_body['userId']
67
+ Wordnik.configuration.auth_token = response_body['token']
68
+ else
69
+ raise response.inspect
70
+ end
71
+ end
49
72
 
50
73
  # The names of all the resources.
51
74
  # This is used by Wordnik.build_resources and the rake task that fetches remote API docs
@@ -54,10 +77,16 @@ module Wordnik
54
77
  %w(account corpus document partners system tag user users word words wordList wordLists wordoftheday)
55
78
  end
56
79
 
80
+ # An alias. For convenience.
81
+ #
57
82
  def word
58
83
  Wordnik.resources[:word]
59
84
  end
60
85
 
86
+ def account
87
+ Wordnik.resources[:account]
88
+ end
89
+
61
90
  end
62
91
 
63
92
  end
@@ -2,11 +2,14 @@ module Wordnik
2
2
 
3
3
  class Configuration
4
4
 
5
- # Wordnik API key
5
+ # Wordnik credentials
6
6
  attr_accessor :api_key
7
+ attr_accessor :username
8
+ attr_accessor :password
7
9
 
8
10
  # TODO: Steal all the auth stuff from the old gem!
9
- # attr_accessor :auth_token
11
+ attr_accessor :auth_token
12
+ attr_accessor :user_id
10
13
 
11
14
  # Response format can be :json (default) or :xml
12
15
  attr_accessor :response_format
@@ -1,3 +1,3 @@
1
1
  module Wordnik
2
- VERSION = "0.3.6"
2
+ VERSION = "0.3.7"
3
3
  end
@@ -4,6 +4,7 @@ require 'wordnik'
4
4
  require 'vcr'
5
5
  require 'typhoeus'
6
6
  require 'json'
7
+ require 'yaml'
7
8
 
8
9
  RSpec.configure do |config|
9
10
  # some (optional) config here
@@ -14,12 +15,31 @@ VCR.config do |config|
14
15
  config.stub_with :webmock # or :fakeweb
15
16
  end
16
17
 
17
- unless ENV['KEY']
18
- puts "\nOn Noes! You gotta include your API key when running the specs, like so:\n\nKEY=12345 rake spec\n\n"
18
+ def help
19
+ puts "\nOh noes! You gotta stuff your wordnik credentials in ~/.wordnik.yml like so:\n\n"
20
+ puts "api_key: '12345abcdefg'"
21
+ puts "username: 'fumanchu'"
22
+ puts "password: 'kalamazoo'\n\n"
19
23
  exit
20
24
  end
21
25
 
22
- Wordnik.configure do |config|
23
- config.api_key = ENV['KEY']
24
- config.base_uri = "beta.wordnik.com/v4"
25
- end
26
+ # Parse ~/.wordnik.yml for user credentials
27
+ begin
28
+ CREDENTIALS = YAML::load_file(File.join(ENV['HOME'], ".wordnik.yml")).symbolize_keys
29
+ rescue
30
+ help
31
+ end
32
+
33
+ help unless Object.const_defined? 'CREDENTIALS'
34
+ help unless [:api_key, :username, :password].all? {|key| CREDENTIALS[key].present? }
35
+
36
+ def configure_wordnik
37
+ Wordnik.configure do |config|
38
+ config.api_key = CREDENTIALS[:api_key]
39
+ config.username = CREDENTIALS[:username]
40
+ config.password = CREDENTIALS[:password]
41
+ config.base_uri = "beta.wordnik.com/v4"
42
+ end
43
+ end
44
+
45
+ configure_wordnik
@@ -3,13 +3,10 @@ require 'spec_helper'
3
3
  describe Wordnik do
4
4
 
5
5
  before(:each) do
6
- Wordnik.configure do |config|
7
- config.api_key = ENV['KEY']
8
- config.base_uri = "beta.wordnik.com/v4"
9
- end
6
+ configure_wordnik
10
7
  end
11
8
 
12
- context "instantiation" do
9
+ context "initialization" do
13
10
 
14
11
  context "resources" do
15
12
  it "instantiates resources from cached JSON" do
@@ -24,7 +21,39 @@ describe Wordnik do
24
21
  it "assigns resource keys that match the resource names" do
25
22
  Wordnik.resources[:word].name.should == :word
26
23
  end
27
-
24
+
25
+ end
26
+
27
+ context "authentication" do
28
+
29
+ before(:each) do
30
+ end
31
+
32
+ it "succeeds if a username and password are present in the configuration" do
33
+ Wordnik.authenticate
34
+ Wordnik.authenticated?.should == true
35
+ end
36
+
37
+ it "de_authenticates" do
38
+ Wordnik.authenticate
39
+ Wordnik.authenticated?.should == true
40
+ Wordnik.de_authenticate
41
+ Wordnik.authenticated?.should == false
42
+ end
43
+
44
+ it "fails if credentials are invalid" do
45
+ Wordnik.de_authenticate
46
+ Wordnik.configure do |config|
47
+ config.api_key = CREDENTIALS[:api_key]
48
+ config.username = CREDENTIALS[:username]
49
+ config.password = 'wrong!'
50
+ config.base_uri = "beta.wordnik.com/v4"
51
+ end
52
+ Wordnik.authenticated?.should == false
53
+ end
54
+
55
+ it "fails if username and/or password are absent"
56
+
28
57
  end
29
58
 
30
59
  end
@@ -65,7 +94,9 @@ describe Wordnik do
65
94
 
66
95
  context "wordlists" do
67
96
 
68
- it "creates a wordlist"
97
+ it "creates a wordlist" do
98
+
99
+ end
69
100
 
70
101
  it "adds words"
71
102
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: wordnik
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.6
5
+ version: 0.3.7
6
6
  platform: ruby
7
7
  authors:
8
8
  - Zeke Sikelianos
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-03-04 00:00:00 -08:00
14
+ date: 2011-03-15 00:00:00 -07:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -187,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
187
  requirements: []
188
188
 
189
189
  rubyforge_project: wordnik
190
- rubygems_version: 1.5.2
190
+ rubygems_version: 1.6.2
191
191
  signing_key:
192
192
  specification_version: 3
193
193
  summary: A ruby wrapper for the Wordnik API