Class: Wordnik::Resource

Inherits:
Object show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/wordnik/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/wordnik/resource.rb', line 19

def initialize(attributes = {})
  attributes.each do |name, value|
    send("#{name.to_s.underscore.to_sym}=", value)
  end

  # Generate Endpoint instances from JSON
  if self.raw_data['apis']
    self.endpoints = self.raw_data['apis'].map do |endpointData|
      Endpoint.new(self, endpointData)
    end
  end 
  
  # Attach module containing metaprogramatticaly generated operations.
  module_filename = File.join(File.dirname(__FILE__), "./resource_modules/#{self.name}.rb")
  if File.exist? module_filename
    self.class.send(:require, "wordnik/resource_modules/#{self.name}")
    self.class.send(:include, "#{self.name.to_s.camelize}Methods".constantize)
  end
end

Instance Attribute Details

#endpointsObject

Returns the value of attribute endpoints.



11
12
13
# File 'lib/wordnik/resource.rb', line 11

def endpoints
  @endpoints
end

#modelsObject

Returns the value of attribute models.



11
12
13
# File 'lib/wordnik/resource.rb', line 11

def models
  @models
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/wordnik/resource.rb', line 11

def name
  @name
end

#raw_dataObject

Returns the value of attribute raw_data.



11
12
13
# File 'lib/wordnik/resource.rb', line 11

def raw_data
  @raw_data
end

Instance Method Details

#persisted?Boolean

It’s an ActiveModel thing..

Returns:

  • (Boolean)


106
107
108
# File 'lib/wordnik/resource.rb', line 106

def persisted?
  false
end

#write_convenience_methodsObject



39
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wordnik/resource.rb', line 39

def write_convenience_methods
  return unless endpoints.present?

  filename = "lib/wordnik/resource_modules/#{name}.rb"
  puts "  #{filename}"
  file = File.new(filename, "w")
  lines = []

  lines << "# HEY HACKER! THIS IS AN AUTO-GENERATED FILE."
  lines << "# So don't bother editing it. To see how it's built, take a look at the Rakefile\n"

  lines << "module #{name.to_s.camelize}Methods\n"

  endpoints.each do |endpoint|
    endpoint.operations.each do |operation|

      next if operation.nickname.blank?

      # Comment about the operation
      # (Turn newlines into commented newlines)
      lines << "  # #{operation.summary.gsub("\n", "\n  # ")}" if operation.summary.present?
      lines << "  # #{operation.notes.gsub("\n", "\n  # ")}" if operation.notes.present?
      lines << "  #" if operation.summary.present?

      # Start writing the method
      lines << "  def #{operation.nickname}(#{[operation.positional_parameter_names, '*args'].flatten.join(', ')})"

      # HTTP Method
      lines << "    http_method = :#{operation.http_method}"

      # Path
      lines << "    path = '#{endpoint.path.sub(".{format}", "")}'"
      operation.positional_parameter_names.each do |param|
        # `body` positional params don't get injected into the path will be dealt with separately
        next if param.to_s == 'body' 
        lines << "    path.sub!('\{#{param}\}', #{param}.to_s)"
      end

      lines << "\n    # Ruby turns all key-value arguments at the end into a single hash"
      lines << "    # e.g. Wordnik.word.get_examples('dingo', :limit => 10, :part_of_speech => 'verb')"
      lines << "    # becomes {:limit => 10, :part_of_speech => 'verb'}"
      lines << "    last_arg = args.pop if args.last.is_a?(Hash)"
      lines << "    last_arg = args.pop if args.last.is_a?(Array)"
      lines << "    last_arg ||= {}\n"

      lines << "    # Look for a kwarg called :request_only, whose presence indicates"
      lines << "    # that we want the request itself back, not the response body"
      lines << "    if last_arg.is_a?(Hash) && last_arg[:request_only].present?"
      lines << "      request_only = true"
      lines << "      last_arg.delete(:request_only)"
      lines << "    end\n"

      lines << "    params = last_arg"
      lines << "    body ||= {}"

      lines << "    request = Wordnik::Request.new(http_method, path, :params => params, :body => body)"
      lines << "    request_only ? request : request.response.body"
      lines << "  end\n"
    end      
  end

  lines << "end"
  file.write lines.join("\n")
  file.close
end