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/load_balancer.rb,
lib/wordnik/operation_parameter.rb

Overview

To jog the memory: Resource > Endpoint > Operation > OperationParameter

Defined Under Namespace

Classes: Configuration, Endpoint, LoadBalancer, Operation, OperationParameter, Request, Resource, Response

Constant Summary collapse

VERSION =
"4.12"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

A Wordnik configuration object. Must act like a hash and return sensible values for all Wordnik configuration options. See Wordnik::Configuration.



25
26
27
# File 'lib/wordnik.rb', line 25

def configuration
  @configuration
end

.loggerObject

Returns the value of attribute logger.



29
30
31
# File 'lib/wordnik.rb', line 29

def logger
  @logger
end

.resourcesObject

Returns the value of attribute resources.



27
28
29
# File 'lib/wordnik.rb', line 27

def resources
  @resources
end

Class Method Details

.authenticateObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/wordnik.rb', line 136

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

Returns:

  • (Boolean)


123
124
125
# File 'lib/wordnik.rb', line 123

def authenticated?
  Wordnik.configuration.user_id.present? && Wordnik.configuration.auth_token.present?
end

.clear_configurationObject



132
133
134
# File 'lib/wordnik.rb', line 132

def clear_configuration
  Wordnik.configuration = Configuration.new
end

.configure(build = true) {|configuration| ... } ⇒ Object

Call this method to modify defaults in your initializers.

Examples:

Wordnik.configure do |config|
  config.api_key = '1234567890abcdef'     # required
  config.username = 'wordlover'           # optional, but needed for user-related functions
  config.password = 'i<3words'            # optional, but needed for user-related functions
  config.response_format = 'json'         # optional, defaults to json
end

Yields:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/wordnik.rb', line 41

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

  # do the same if multiple hosts are specified
  configuration.hosts = configuration.hosts.map{|host| host.sub(/https?:\/\//, '').split('/').first}

  # create a load balancer if no load balancer specified && multiple hosts are specified ...
  if !configuration.load_balancer && configuration.hosts.size > 0
    self.logger.debug "Creating a load balancer from #{configuration.hosts.join(', ')}"
    configuration.load_balancer = LoadBalancer.new(configuration.hosts)
  end

  # 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_shortcutsObject

Use some magic ruby dust to make nice method shortcuts. Wordnik.word => Wordnik.resources Wordnik.users => Wordnik.resources



114
115
116
117
118
119
120
121
# File 'lib/wordnik.rb', line 114

def create_resource_shortcuts
  self.configuration.resource_names.each do |resource_name|
    method_name = resource_name.underscore.to_sym
    meta_def method_name do
      Wordnik.resources[method_name]
    end
  end
end

.de_authenticateObject



127
128
129
130
# File 'lib/wordnik.rb', line 127

def de_authenticate
  Wordnik.configuration.user_id = nil
  Wordnik.configuration.auth_token = nil
end

.download_resource_descriptionsObject

Remove old JSON documentation and generated modules, then download fresh JSON files.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wordnik.rb', line 79

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_resourcesObject

Iterate over each disk-cached JSON resource documentation file, and

  1. Instantiate a Resource object

  2. Stuff the Resource in Wordnik.resources



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/wordnik.rb', line 97

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