Module: Wordnik
- Defined in:
- lib/wordnik/resource.rb,
lib/wordnik.rb,
lib/wordnik/request.rb,
lib/wordnik/version.rb,
lib/wordnik/endpoint.rb,
lib/wordnik/response.rb,
lib/wordnik/operation.rb,
lib/wordnik/configuration.rb,
lib/wordnik/operation_parameter.rb
Overview
To jog the memory: Resource > Endpoint > Operation > OperationParameter
Defined Under Namespace
Classes: Configuration, Endpoint, Operation, OperationParameter, Request, Resource, Response
Constant Summary collapse
- VERSION =
"4.07"
Class Attribute Summary collapse
-
.configuration ⇒ Object
A Wordnik configuration object.
-
.logger ⇒ Object
Returns the value of attribute logger.
-
.resources ⇒ Object
Returns the value of attribute resources.
Class Method Summary collapse
- .authenticate ⇒ Object
- .authenticated? ⇒ Boolean
-
.configure(build = true) {|configuration| ... } ⇒ Object
Call this method to modify defaults in your initializers.
-
.create_resource_shortcuts ⇒ Object
Use some magic ruby dust to make nice method shortcuts.
- .de_authenticate ⇒ Object
-
.download_resource_descriptions ⇒ Object
Remove old JSON documentation and generated modules, then download fresh JSON files.
-
.instantiate_resources ⇒ Object
Iterate over each disk-cached JSON resource documentation file, and 1.
Class Attribute Details
.configuration ⇒ Object
A Wordnik configuration object. Must act like a hash and return sensible values for all Wordnik configuration options. See Wordnik::Configuration.
24 25 26 |
# File 'lib/wordnik.rb', line 24 def configuration @configuration end |
.logger ⇒ Object
Returns the value of attribute logger.
28 29 30 |
# File 'lib/wordnik.rb', line 28 def logger @logger end |
.resources ⇒ Object
Returns the value of attribute resources.
26 27 28 |
# File 'lib/wordnik.rb', line 26 def resources @resources end |
Class Method Details
.authenticate ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/wordnik.rb', line 122 def authenticate return if Wordnik.authenticated? if Wordnik.configuration.username.blank? || Wordnik.configuration.password.blank? raise ClientError, "Username and password are required to authenticate." end request = Wordnik::Request.new( :get, "account/authenticate/{username}", :params => { :username => Wordnik.configuration.username, :password => Wordnik.configuration.password } ) response_body = request.response.body Wordnik.configuration.user_id = response_body['userId'] Wordnik.configuration.auth_token = response_body['token'] end |
.authenticated? ⇒ Boolean
113 114 115 |
# File 'lib/wordnik.rb', line 113 def authenticated? Wordnik.configuration.user_id.present? && Wordnik.configuration.auth_token.present? end |
.configure(build = true) {|configuration| ... } ⇒ Object
Call this method to modify defaults in your initializers.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/wordnik.rb', line 40 def configure(build=true) self.configuration ||= Configuration.new yield(configuration) if block_given? # Configure logger. Default to use Rails self.logger ||= configuration.logger || (defined?(Rails) ? Rails.logger : Logger.new(STDOUT)) # remove :// from scheme configuration.scheme.sub!(/:\/\//, '') # remove http(s):// and anything after a slash configuration.host.sub!(/https?:\/\//, '') configuration.host = configuration.host.split('/').first # Add leading and trailing slashes to base_path configuration.base_path = "/#{configuration.base_path}".gsub(/\/+/, '/') configuration.base_path = "" if configuration.base_path == "/" # The Rakefile needs to call Wordnik.configure, but can't # attach resources because they haven't been downloaded. if build self.instantiate_resources self.create_resource_shortcuts end end |
.create_resource_shortcuts ⇒ Object
Use some magic ruby dust to make nice method shortcuts. Wordnik.word => Wordnik.resources Wordnik.users => Wordnik.resources
104 105 106 107 108 109 110 111 |
# File 'lib/wordnik.rb', line 104 def create_resource_shortcuts self.configuration.resource_names.each do |resource_name| method_name = resource_name.underscore.to_sym method_name do Wordnik.resources[method_name] end end end |
.de_authenticate ⇒ Object
117 118 119 120 |
# File 'lib/wordnik.rb', line 117 def de_authenticate Wordnik.configuration.user_id = nil Wordnik.configuration.auth_token = nil end |
.download_resource_descriptions ⇒ Object
Remove old JSON documentation and generated modules, then download fresh JSON files.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/wordnik.rb', line 69 def download_resource_descriptions system "rm api_docs/*.json" system "rm lib/wordnik/resource_modules/*.rb" Wordnik::Request.new(:get, "resources.json").response.body['apis'].each do |api| resource_name = api['path'].split(".").first.gsub(/\//, '') description = api['description'] puts " #{resource_name} #{description}" request = Wordnik::Request.new(:get, "#{resource_name}.json") filename = "api_docs/#{resource_name}.json" File.open(filename, 'w') {|f| f.write(request.response.raw.body) } end end |
.instantiate_resources ⇒ Object
Iterate over each disk-cached JSON resource documentation file, and
-
Instantiate a Resource object
-
Stuff the Resource in Wordnik.resources
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/wordnik.rb', line 87 def instantiate_resources self.resources = {} self.configuration.resource_names.each do |resource_name| name = resource_name.underscore.to_sym # 'fooBar' => :foo_bar filename = File.join(File.dirname(__FILE__), "../api_docs/#{resource_name}.json") resource = Resource.new( :name => name, :raw_data => JSON.parse(File.read(filename)) ) self.resources[name] = resource end end |