wordnik 0.3.2 → 0.3.4
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/Gemfile.lock +1 -1
 - data/lib/wordnik.rb +11 -3
 - data/lib/wordnik/request.rb +15 -4
 - data/lib/wordnik/version.rb +1 -1
 - data/spec/request_spec.rb +45 -18
 - data/spec/wordnik_spec.rb +7 -1
 - metadata +1 -1
 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/lib/wordnik.rb
    CHANGED
    
    | 
         @@ -1,4 +1,3 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            require 'wordnik/monkey_patches'
         
     | 
| 
       2 
1 
     | 
    
         
             
            require 'wordnik/endpoint'
         
     | 
| 
       3 
2 
     | 
    
         
             
            require 'wordnik/operation'
         
     | 
| 
       4 
3 
     | 
    
         
             
            require 'wordnik/operation_parameter'
         
     | 
| 
         @@ -6,6 +5,7 @@ require 'wordnik/request' 
     | 
|
| 
       6 
5 
     | 
    
         
             
            require 'wordnik/resource'
         
     | 
| 
       7 
6 
     | 
    
         
             
            require 'wordnik/response'
         
     | 
| 
       8 
7 
     | 
    
         
             
            require 'wordnik/configuration'
         
     | 
| 
      
 8 
     | 
    
         
            +
            require 'wordnik/version'
         
     | 
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
            module Wordnik
         
     | 
| 
       11 
11 
     | 
    
         | 
| 
         @@ -38,10 +38,18 @@ module Wordnik 
     | 
|
| 
       38 
38 
     | 
    
         
             
                  self.resources = {}
         
     | 
| 
       39 
39 
     | 
    
         
             
                  self.resource_names.map do |resource_name|
         
     | 
| 
       40 
40 
     | 
    
         
             
                    name = resource_name.underscore.to_sym # 'fooBar' => :foo_bar
         
     | 
| 
       41 
     | 
    
         
            -
                     
     | 
| 
      
 41 
     | 
    
         
            +
                    begin
         
     | 
| 
      
 42 
     | 
    
         
            +
                      # This is the path the installed gem will want
         
     | 
| 
      
 43 
     | 
    
         
            +
                      filename = File.join(ENV['GEM_HOME'], "gems", "wordnik-#{Wordnik::VERSION}", "api_docs/#{resource_name}.json")
         
     | 
| 
      
 44 
     | 
    
         
            +
                      raw_data = JSON.parse(File.read(filename))
         
     | 
| 
      
 45 
     | 
    
         
            +
                    rescue
         
     | 
| 
      
 46 
     | 
    
         
            +
                      # This is the path the not-installed gem in development will want
         
     | 
| 
      
 47 
     | 
    
         
            +
                      filename = File.join("api_docs/#{resource_name}.json")
         
     | 
| 
      
 48 
     | 
    
         
            +
                      raw_data = JSON.parse(File.read(filename))
         
     | 
| 
      
 49 
     | 
    
         
            +
                    end
         
     | 
| 
       42 
50 
     | 
    
         
             
                    resource = Resource.new(
         
     | 
| 
       43 
51 
     | 
    
         
             
                      :name => name,
         
     | 
| 
       44 
     | 
    
         
            -
                      :raw_data =>  
     | 
| 
      
 52 
     | 
    
         
            +
                      :raw_data => raw_data
         
     | 
| 
       45 
53 
     | 
    
         
             
                    )
         
     | 
| 
       46 
54 
     | 
    
         
             
                    self.resources[name] = resource
         
     | 
| 
       47 
55 
     | 
    
         
             
                  end
         
     | 
    
        data/lib/wordnik/request.rb
    CHANGED
    
    | 
         @@ -5,6 +5,7 @@ module Wordnik 
     | 
|
| 
       5 
5 
     | 
    
         
             
                require 'addressable/uri'
         
     | 
| 
       6 
6 
     | 
    
         
             
                require 'typhoeus'
         
     | 
| 
       7 
7 
     | 
    
         
             
                require 'active_model'
         
     | 
| 
      
 8 
     | 
    
         
            +
                require "wordnik/version"
         
     | 
| 
       8 
9 
     | 
    
         
             
                include ActiveModel::Validations
         
     | 
| 
       9 
10 
     | 
    
         
             
                include ActiveModel::Conversion
         
     | 
| 
       10 
11 
     | 
    
         
             
                extend ActiveModel::Naming
         
     | 
| 
         @@ -24,11 +25,21 @@ module Wordnik 
     | 
|
| 
       24 
25 
     | 
    
         
             
                    'Content-Type' => "application/#{attributes[:format].downcase}",
         
     | 
| 
       25 
26 
     | 
    
         
             
                    :api_key => Wordnik.configuration.api_key
         
     | 
| 
       26 
27 
     | 
    
         
             
                  }
         
     | 
| 
       27 
     | 
    
         
            -
                  attributes[:headers] = default_headers.merge(attributes[:headers] || {})
         
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
       29 
     | 
    
         
            -
                  # If a blank/nil api_key was passed in, remove it from the headers
         
     | 
| 
       30 
     | 
    
         
            -
                  attributes[:headers].delete(:api_key) if attributes[:headers][:api_key].blank?
         
     | 
| 
       31 
28 
     | 
    
         | 
| 
      
 29 
     | 
    
         
            +
                  # If a nil/blank api_key was passed in, remove it from the headers, even if the override is nil/blank
         
     | 
| 
      
 30 
     | 
    
         
            +
                  if attributes[:headers].present? && attributes[:headers].has_key?(:api_key)
         
     | 
| 
      
 31 
     | 
    
         
            +
                    default_headers.delete(:api_key)
         
     | 
| 
      
 32 
     | 
    
         
            +
                  end
         
     | 
| 
      
 33 
     | 
    
         
            +
                  
         
     | 
| 
      
 34 
     | 
    
         
            +
                  # If nil/blank api_key was passed in in the params, it overrides both the default
         
     | 
| 
      
 35 
     | 
    
         
            +
                  # headers and the argument headers
         
     | 
| 
      
 36 
     | 
    
         
            +
                  if attributes[:params].present? && attributes[:params].has_key?(:api_key)
         
     | 
| 
      
 37 
     | 
    
         
            +
                    default_headers.delete(:api_key)
         
     | 
| 
      
 38 
     | 
    
         
            +
                    attributes[:headers].delete(:api_key) if attributes[:headers].present?
         
     | 
| 
      
 39 
     | 
    
         
            +
                  end
         
     | 
| 
      
 40 
     | 
    
         
            +
                  
         
     | 
| 
      
 41 
     | 
    
         
            +
                  attributes[:headers] = default_headers.merge(attributes[:headers] || {})
         
     | 
| 
      
 42 
     | 
    
         
            +
                        
         
     | 
| 
       32 
43 
     | 
    
         
             
                  self.http_method = http_method.to_sym
         
     | 
| 
       33 
44 
     | 
    
         
             
                  self.path = path
         
     | 
| 
       34 
45 
     | 
    
         
             
                  attributes.each do |name, value|
         
     | 
    
        data/lib/wordnik/version.rb
    CHANGED
    
    
    
        data/spec/request_spec.rb
    CHANGED
    
    | 
         @@ -16,7 +16,7 @@ describe Wordnik::Request do 
     | 
|
| 
       16 
16 
     | 
    
         
             
                  @request.format.should == "json"
         
     | 
| 
       17 
17 
     | 
    
         
             
                end
         
     | 
| 
       18 
18 
     | 
    
         | 
| 
       19 
     | 
    
         
            -
                it " 
     | 
| 
      
 19 
     | 
    
         
            +
                it "gets default host from Wordnik.configuration" do
         
     | 
| 
       20 
20 
     | 
    
         
             
                  @request.host.should == Wordnik.configuration.base_uri
         
     | 
| 
       21 
21 
     | 
    
         
             
                end
         
     | 
| 
       22 
22 
     | 
    
         | 
| 
         @@ -112,23 +112,6 @@ describe Wordnik::Request do 
     | 
|
| 
       112 
112 
     | 
    
         
             
                  @request.query_string.should == "?foo=criminy&word=dog"
         
     | 
| 
       113 
113 
     | 
    
         
             
                end
         
     | 
| 
       114 
114 
     | 
    
         | 
| 
       115 
     | 
    
         
            -
                it "obfuscates the API key when needed" do
         
     | 
| 
       116 
     | 
    
         
            -
                  @request = Wordnik::Request.new(:get, "words/fancy", @default_params.merge({
         
     | 
| 
       117 
     | 
    
         
            -
                    :params => {
         
     | 
| 
       118 
     | 
    
         
            -
                      :word => "dog",
         
     | 
| 
       119 
     | 
    
         
            -
                      :api_key => "123456"
         
     | 
| 
       120 
     | 
    
         
            -
                    }
         
     | 
| 
       121 
     | 
    
         
            -
                  }))
         
     | 
| 
       122 
     | 
    
         
            -
                  @request.query_string_params.should == {:word => "dog", :api_key => "123456"}
         
     | 
| 
       123 
     | 
    
         
            -
                  @request.query_string_params(true).should == {:word => "dog", :api_key => "YOUR_API_KEY"}
         
     | 
| 
       124 
     | 
    
         
            -
             
     | 
| 
       125 
     | 
    
         
            -
                  @request.query_string.should == "?api_key=123456&word=dog"
         
     | 
| 
       126 
     | 
    
         
            -
                  @request.query_string(:obfuscated => true).should == "?api_key=YOUR_API_KEY&word=dog"
         
     | 
| 
       127 
     | 
    
         
            -
             
     | 
| 
       128 
     | 
    
         
            -
                  @request.url_with_query_string.should =~ /123456/
         
     | 
| 
       129 
     | 
    
         
            -
                  @request.url_with_query_string(:obfuscated => true).should =~ /YOUR\_API\_KEY/
         
     | 
| 
       130 
     | 
    
         
            -
                end
         
     | 
| 
       131 
     | 
    
         
            -
             
     | 
| 
       132 
115 
     | 
    
         
             
                it "URI encodes the path" do
         
     | 
| 
       133 
116 
     | 
    
         
             
                  @request = Wordnik::Request.new(:get, "word.{format}/{word}/definitions", @default_params.merge({
         
     | 
| 
       134 
117 
     | 
    
         
             
                    :params => {
         
     | 
| 
         @@ -151,6 +134,50 @@ describe Wordnik::Request do 
     | 
|
| 
       151 
134 
     | 
    
         
             
                end
         
     | 
| 
       152 
135 
     | 
    
         | 
| 
       153 
136 
     | 
    
         
             
                it "camelCases parameters"
         
     | 
| 
      
 137 
     | 
    
         
            +
                
         
     | 
| 
      
 138 
     | 
    
         
            +
              end
         
     | 
| 
      
 139 
     | 
    
         
            +
              
         
     | 
| 
      
 140 
     | 
    
         
            +
              describe "API key" do
         
     | 
| 
      
 141 
     | 
    
         
            +
                
         
     | 
| 
      
 142 
     | 
    
         
            +
                it "is inferred from the Wordnik base configuration by default" do
         
     | 
| 
      
 143 
     | 
    
         
            +
                  Wordnik.configure {|c| c.api_key = "xyz" }
         
     | 
| 
      
 144 
     | 
    
         
            +
                  Wordnik::Request.new(:get, "word/json").headers[:api_key].should == "xyz"
         
     | 
| 
      
 145 
     | 
    
         
            +
                end
         
     | 
| 
      
 146 
     | 
    
         
            +
                
         
     | 
| 
      
 147 
     | 
    
         
            +
                it "gets obfuscated for public display" do
         
     | 
| 
      
 148 
     | 
    
         
            +
                  @request = Wordnik::Request.new(:get, "words/fancy", @default_params.merge({
         
     | 
| 
      
 149 
     | 
    
         
            +
                    :params => {
         
     | 
| 
      
 150 
     | 
    
         
            +
                      :word => "dog",
         
     | 
| 
      
 151 
     | 
    
         
            +
                      :api_key => "123456"
         
     | 
| 
      
 152 
     | 
    
         
            +
                    }
         
     | 
| 
      
 153 
     | 
    
         
            +
                  }))
         
     | 
| 
      
 154 
     | 
    
         
            +
                  @request.query_string_params.should == {:word => "dog", :api_key => "123456"}
         
     | 
| 
      
 155 
     | 
    
         
            +
                  @request.query_string_params(true).should == {:word => "dog", :api_key => "YOUR_API_KEY"}
         
     | 
| 
      
 156 
     | 
    
         
            +
             
     | 
| 
      
 157 
     | 
    
         
            +
                  @request.query_string.should == "?api_key=123456&word=dog"
         
     | 
| 
      
 158 
     | 
    
         
            +
                  @request.query_string(:obfuscated => true).should == "?api_key=YOUR_API_KEY&word=dog"
         
     | 
| 
      
 159 
     | 
    
         
            +
             
     | 
| 
      
 160 
     | 
    
         
            +
                  @request.url_with_query_string.should =~ /123456/
         
     | 
| 
      
 161 
     | 
    
         
            +
                  @request.url_with_query_string(:obfuscated => true).should =~ /YOUR\_API\_KEY/
         
     | 
| 
      
 162 
     | 
    
         
            +
                end
         
     | 
| 
      
 163 
     | 
    
         
            +
             
     | 
| 
      
 164 
     | 
    
         
            +
                it "allows a key in the params to override the configuration-level key, even if it's blank" do
         
     | 
| 
      
 165 
     | 
    
         
            +
                  Wordnik.configure {|c| c.api_key = "abc" }
         
     | 
| 
      
 166 
     | 
    
         
            +
                  @request_with_key = Wordnik::Request.new(:get, "word/json", :params => {:api_key => "jkl"})
         
     | 
| 
      
 167 
     | 
    
         
            +
                  @request_with_key.headers[:api_key].should be_nil
         
     | 
| 
      
 168 
     | 
    
         
            +
                  @request_with_key.params[:api_key].should == "jkl"
         
     | 
| 
      
 169 
     | 
    
         
            +
                  
         
     | 
| 
      
 170 
     | 
    
         
            +
                  @request_without_key = Wordnik::Request.new(:get, "word/json", :params => {:api_key => nil})
         
     | 
| 
      
 171 
     | 
    
         
            +
                  @request_without_key.headers[:api_key].should be_nil
         
     | 
| 
      
 172 
     | 
    
         
            +
                  @request_without_key.params[:api_key].should be_nil
         
     | 
| 
      
 173 
     | 
    
         
            +
                end
         
     | 
| 
      
 174 
     | 
    
         
            +
             
     | 
| 
      
 175 
     | 
    
         
            +
                it "allows a key in the headers to override the configuration-level key, even if it's blank" do
         
     | 
| 
      
 176 
     | 
    
         
            +
                  Wordnik.configure {|c| c.api_key = "hij" }
         
     | 
| 
      
 177 
     | 
    
         
            +
                  Wordnik::Request.new(:get, "word/json").headers[:api_key].should == "hij"
         
     | 
| 
      
 178 
     | 
    
         
            +
                  Wordnik::Request.new(:get, "word/json", :headers => {:api_key => "jkl"}).headers[:api_key].should == "jkl"
         
     | 
| 
      
 179 
     | 
    
         
            +
                  Wordnik::Request.new(:get, "word/json", :headers => {:api_key => nil}).headers[:api_key].should be_nil
         
     | 
| 
      
 180 
     | 
    
         
            +
                end
         
     | 
| 
       154 
181 
     | 
    
         | 
| 
       155 
182 
     | 
    
         
             
              end
         
     | 
| 
       156 
183 
     | 
    
         | 
    
        data/spec/wordnik_spec.rb
    CHANGED