wordnik 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,32 +1,33 @@
1
- # To jog the memory: Resource > Endpoint > Operation > OperationParameter
2
- require 'active_model'
1
+ module Wordnik
3
2
 
4
- class Endpoint
3
+ class Endpoint
4
+ require 'active_model'
5
+ include ActiveModel::Validations
6
+ include ActiveModel::Conversion
7
+ extend ActiveModel::Naming
5
8
 
6
- include ActiveModel::Validations
7
- include ActiveModel::Conversion
8
- extend ActiveModel::Naming
9
+ attr_accessor :path, :description, :operations
9
10
 
10
- attr_accessor :path, :description, :operations
11
+ validates_presence_of :path, :description, :operations
11
12
 
12
- validates_presence_of :path, :description, :operations
13
-
14
- def initialize(attributes = {})
15
- attributes.each do |name, value|
16
- send("#{name.to_s.underscore.to_sym}=", value)
17
- end
13
+ def initialize(attributes = {})
14
+ attributes.each do |name, value|
15
+ send("#{name.to_s.underscore.to_sym}=", value)
16
+ end
18
17
 
19
- # Generate Operations instances from JSON
20
- if self.operations
21
- self.operations = self.operations.map do |operationData|
22
- Operation.new(operationData)
18
+ # Generate Operations instances from JSON
19
+ if self.operations
20
+ self.operations = self.operations.map do |operationData|
21
+ Operation.new(operationData)
22
+ end
23
23
  end
24
24
  end
25
- end
26
25
 
27
- # It's an ActiveModel thing..
28
- def persisted?
29
- false
30
- end
26
+ # It's an ActiveModel thing..
27
+ def persisted?
28
+ false
29
+ end
31
30
 
31
+ end
32
+
32
33
  end
@@ -1,42 +1,45 @@
1
- # To jog the memory: Resource > Endpoint > Operation > OperationParameter
2
-
3
- class Operation
4
- include ActiveModel::Validations
5
- include ActiveModel::Conversion
6
- extend ActiveModel::Naming
1
+ module Wordnik
7
2
 
8
- attr_accessor :http_method, :summary, :notes, :parameters, :response, :open
3
+ class Operation
4
+ require 'active_model'
5
+ include ActiveModel::Validations
6
+ include ActiveModel::Conversion
7
+ extend ActiveModel::Naming
9
8
 
10
- validates_presence_of :http_method, :summary, :notes, :parameters, :response, :open
9
+ attr_accessor :http_method, :summary, :notes, :parameters, :response, :open
11
10
 
12
- def initialize(attributes = {})
13
- attributes.each do |name, value|
14
- send("#{name.to_s.underscore.to_sym}=", value)
15
- end
11
+ validates_presence_of :http_method, :summary, :notes, :parameters, :response, :open
12
+
13
+ def initialize(attributes = {})
14
+ attributes.each do |name, value|
15
+ send("#{name.to_s.underscore.to_sym}=", value)
16
+ end
16
17
 
17
- self.http_method = self.http_method.to_s.downcase
18
+ self.http_method = self.http_method.to_s.downcase
18
19
 
19
- # Generate OperationParameter instances from JSON
20
- if self.parameters
21
- self.parameters = self.parameters.map do |parameterData|
22
- OperationParameter.new(parameterData)
20
+ # Generate OperationParameter instances from JSON
21
+ if self.parameters
22
+ self.parameters = self.parameters.map do |parameterData|
23
+ OperationParameter.new(parameterData)
24
+ end
23
25
  end
24
- end
25
26
 
26
- end
27
+ end
27
28
 
28
- def get?
29
- self.http_method.downcase == "get"
30
- end
29
+ def get?
30
+ self.http_method.downcase == "get"
31
+ end
31
32
 
32
- # Can this operation be run in the sandbox?
33
- def sandboxable?
34
- self.get?
35
- end
33
+ # Can this operation be run in the sandbox?
34
+ def sandboxable?
35
+ self.get?
36
+ end
36
37
 
37
- # It's an ActiveModel thing..
38
- def persisted?
39
- false
40
- end
38
+ # It's an ActiveModel thing..
39
+ def persisted?
40
+ false
41
+ end
41
42
 
43
+ end
44
+
42
45
  end
@@ -1,36 +1,39 @@
1
- # To jog the memory: Resource > Endpoint > Operation > OperationParameter
2
-
3
- class OperationParameter
4
- include ActiveModel::Validations
5
- include ActiveModel::Conversion
6
- extend ActiveModel::Naming
1
+ module Wordnik
2
+
3
+ class OperationParameter
4
+ require 'active_model'
5
+ include ActiveModel::Validations
6
+ include ActiveModel::Conversion
7
+ extend ActiveModel::Naming
7
8
 
8
- attr_accessor :name, :description, :required, :param_type, :default_value, :allowable_values
9
+ attr_accessor :name, :description, :required, :param_type, :default_value, :allowable_values
9
10
 
10
- validates_presence_of :name, :description, :required, :param_type, :default_value, :allowable_values
11
+ validates_presence_of :name, :description, :required, :param_type, :default_value, :allowable_values
11
12
 
12
- def initialize(attributes = {})
13
- attributes.each do |name, value|
14
- send("#{name.to_s.underscore.to_sym}=", value)
13
+ def initialize(attributes = {})
14
+ attributes.each do |name, value|
15
+ send("#{name.to_s.underscore.to_sym}=", value)
16
+ end
15
17
  end
16
- end
17
18
 
18
- def human_name
19
- return "request body" if self.param_type == 'body'
20
- self.name
21
- end
19
+ def human_name
20
+ return "request body" if self.param_type == 'body'
21
+ self.name
22
+ end
22
23
 
23
- def has_allowable_array?
24
- self.allowable_values.present? && self.allowable_values.include?(",")
25
- end
24
+ def has_allowable_array?
25
+ self.allowable_values.present? && self.allowable_values.include?(",")
26
+ end
26
27
 
27
- def required?
28
- self.required
29
- end
28
+ def required?
29
+ self.required
30
+ end
30
31
 
31
- # It's an ActiveModel thing..
32
- def persisted?
33
- false
34
- end
32
+ # It's an ActiveModel thing..
33
+ def persisted?
34
+ false
35
+ end
35
36
 
37
+ end
38
+
36
39
  end
@@ -1,159 +1,163 @@
1
- class Request
2
- require 'uri'
3
- require 'addressable/uri'
4
- require 'typhoeus'
5
- include ActiveModel::Validations
6
- include ActiveModel::Conversion
7
- extend ActiveModel::Naming
1
+ module Wordnik
2
+
3
+ class Request
4
+ require 'uri'
5
+ require 'addressable/uri'
6
+ require 'typhoeus'
7
+ require 'active_model'
8
+ include ActiveModel::Validations
9
+ include ActiveModel::Conversion
10
+ extend ActiveModel::Naming
8
11
 
9
- attr_accessor :host, :port, :path, :format, :params, :body, :http_method, :headers
12
+ attr_accessor :host, :port, :path, :format, :params, :body, :http_method, :headers
10
13
 
11
- validates_presence_of :host, :path, :format, :http_method
14
+ validates_presence_of :host, :path, :format, :http_method
12
15
 
13
- def initialize(http_method, path, attributes={})
14
- attributes[:format] ||= "json"
15
- attributes[:host] ||= Wordnik.configuration.base_uri
16
- attributes[:params] ||= {}
16
+ def initialize(http_method, path, attributes={})
17
+ attributes[:format] ||= "json"
18
+ attributes[:host] ||= Wordnik.configuration.base_uri
19
+ attributes[:params] ||= {}
17
20
 
18
- # Set default headers, but allow them to be overridden
19
- default_headers = {
20
- 'Content-Type' => "application/#{attributes[:format].downcase}",
21
- }
22
- attributes[:headers] = default_headers.merge(attributes[:headers] || {})
21
+ # Set default headers, but allow them to be overridden
22
+ default_headers = {
23
+ 'Content-Type' => "application/#{attributes[:format].downcase}",
24
+ }
25
+ attributes[:headers] = default_headers.merge(attributes[:headers] || {})
23
26
 
24
- self.http_method = http_method.to_sym
25
- self.path = path
26
- attributes.each do |name, value|
27
- send("#{name.to_s.underscore.to_sym}=", value)
27
+ self.http_method = http_method.to_sym
28
+ self.path = path
29
+ attributes.each do |name, value|
30
+ send("#{name.to_s.underscore.to_sym}=", value)
31
+ end
28
32
  end
29
- end
30
-
31
- # Construct a base URL
32
- def url
33
- u = Addressable::URI.new
34
- u.host = self.host.sub(/\/$/, '')
35
- u.port = self.port if self.port.present?
36
- u.path = self.interpreted_path
37
- u.scheme = "http" # For some reason this must be set _after_ host, otherwise Addressable gets upset
38
- u.to_s
39
- end
40
33
 
41
- # Iterate over the params hash, injecting any path values into the path string
42
- # e.g. /word.{format}/{word}/entries => /word.json/cat/entries
43
- def interpreted_path
44
- p = self.path
45
- self.params.each_pair do |key, value|
46
- p = p.gsub("{#{key}}", value.to_s)
34
+ # Construct a base URL
35
+ def url
36
+ u = Addressable::URI.new
37
+ u.host = self.host.sub(/\/$/, '')
38
+ u.port = self.port if self.port.present?
39
+ u.path = self.interpreted_path
40
+ u.scheme = "http" # For some reason this must be set _after_ host, otherwise Addressable gets upset
41
+ u.to_s
47
42
  end
43
+
44
+ # Iterate over the params hash, injecting any path values into the path string
45
+ # e.g. /word.{format}/{word}/entries => /word.json/cat/entries
46
+ def interpreted_path
47
+ p = self.path
48
+ self.params.each_pair do |key, value|
49
+ p = p.gsub("{#{key}}", value.to_s)
50
+ end
48
51
 
49
- # Stick a .{format} placeholder into the path if there isn't
50
- # one already or an actual format like json or xml
51
- # e.g. /words/blah => /words.{format}/blah
52
- unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
53
- p = p.sub(/^(\/?\w+)/, "\\1.#{format}")
54
- end
52
+ # Stick a .{format} placeholder into the path if there isn't
53
+ # one already or an actual format like json or xml
54
+ # e.g. /words/blah => /words.{format}/blah
55
+ unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
56
+ p = p.sub(/^(\/?\w+)/, "\\1.#{format}")
57
+ end
55
58
 
56
- p = p.sub("{format}", self.format)
57
- URI.encode(p)
58
- end
59
+ p = p.sub("{format}", self.format)
60
+ URI.encode(p)
61
+ end
59
62
 
60
- def interpreted_body
61
- return unless self.body.present?
62
- return self.body.to_json if self.body.is_a?(Hash)
63
- self.body
64
- end
63
+ def interpreted_body
64
+ return unless self.body.present?
65
+ return self.body.to_json if self.body.is_a?(Hash)
66
+ self.body
67
+ end
65
68
 
66
- # Iterate over all params,
67
- # .. removing the ones that are part of the path itself.
68
- # .. stringifying values so Addressable doesn't blow up.
69
- # .. obfuscating the API key if needed.
70
- def query_string_params(obfuscated=false)
71
- qsp = {}
72
- self.params.each_pair do |key, value|
73
- next if self.path.include? "{#{key}}"
74
- next if value.blank?
75
- value = "YOUR_API_KEY" if key.to_sym == :api_key && obfuscated
76
- qsp[key] = value.to_s
69
+ # Iterate over all params,
70
+ # .. removing the ones that are part of the path itself.
71
+ # .. stringifying values so Addressable doesn't blow up.
72
+ # .. obfuscating the API key if needed.
73
+ def query_string_params(obfuscated=false)
74
+ qsp = {}
75
+ self.params.each_pair do |key, value|
76
+ next if self.path.include? "{#{key}}"
77
+ next if value.blank?
78
+ value = "YOUR_API_KEY" if key.to_sym == :api_key && obfuscated
79
+ qsp[key] = value.to_s
80
+ end
81
+ qsp
77
82
  end
78
- qsp
79
- end
80
83
 
81
- # Construct a query string from the query-string-type params
82
- def query_string(options={})
84
+ # Construct a query string from the query-string-type params
85
+ def query_string(options={})
83
86
 
84
- # We don't want to end up with '?' as our query string
85
- # if there aren't really any params
86
- return "" if query_string_params.blank?
87
+ # We don't want to end up with '?' as our query string
88
+ # if there aren't really any params
89
+ return "" if query_string_params.blank?
87
90
 
88
- default_options = {:obfuscated => false}
89
- options = default_options.merge(options)
91
+ default_options = {:obfuscated => false}
92
+ options = default_options.merge(options)
90
93
 
91
- qs = Addressable::URI.new
92
- qs.query_values = self.query_string_params(options[:obfuscated])
93
- qs.to_s
94
- end
94
+ qs = Addressable::URI.new
95
+ qs.query_values = self.query_string_params(options[:obfuscated])
96
+ qs.to_s
97
+ end
95
98
 
96
- # Returns full request URL with query string included
97
- def url_with_query_string(options={})
98
- default_options = {:obfuscated => false}
99
- options = default_options.merge(options)
99
+ # Returns full request URL with query string included
100
+ def url_with_query_string(options={})
101
+ default_options = {:obfuscated => false}
102
+ options = default_options.merge(options)
100
103
 
101
- [url, query_string(options)].join('')
102
- end
104
+ [url, query_string(options)].join('')
105
+ end
103
106
 
104
- def make
105
- response = case self.http_method.to_sym
106
- when :get
107
- Typhoeus::Request.get(
108
- self.url_with_query_string,
109
- :headers => self.headers.stringify_keys
110
- )
107
+ def make
108
+ response = case self.http_method.to_sym
109
+ when :get
110
+ Typhoeus::Request.get(
111
+ self.url_with_query_string,
112
+ :headers => self.headers.stringify_keys
113
+ )
111
114
 
112
- when :post
113
- Typhoeus::Request.post(
114
- self.url_with_query_string,
115
- :body => self.interpreted_body,
116
- :headers => self.headers.stringify_keys
117
- )
115
+ when :post
116
+ Typhoeus::Request.post(
117
+ self.url_with_query_string,
118
+ :body => self.interpreted_body,
119
+ :headers => self.headers.stringify_keys
120
+ )
118
121
 
119
- when :put
120
- Typhoeus::Request.put(
121
- self.url_with_query_string,
122
- :body => self.interpreted_body,
123
- :headers => self.headers.stringify_keys
124
- )
122
+ when :put
123
+ Typhoeus::Request.put(
124
+ self.url_with_query_string,
125
+ :body => self.interpreted_body,
126
+ :headers => self.headers.stringify_keys
127
+ )
125
128
 
126
- when :delete
127
- Typhoeus::Request.delete(
128
- self.url_with_query_string,
129
- :body => self.interpreted_body,
130
- :headers => self.headers.stringify_keys
131
- )
132
- end
129
+ when :delete
130
+ Typhoeus::Request.delete(
131
+ self.url_with_query_string,
132
+ :body => self.interpreted_body,
133
+ :headers => self.headers.stringify_keys
134
+ )
135
+ end
133
136
 
134
- @response_obj = Response.new(response)
135
- end
137
+ @response_obj = Response.new(response)
138
+ end
136
139
 
137
- # If the request has been made, return the existing response
138
- # If not, make the request and return the response
139
- def response
140
- @response_obj || self.make
141
- end
140
+ # If the request has been made, return the existing response
141
+ # If not, make the request and return the response
142
+ def response
143
+ @response_obj || self.make
144
+ end
142
145
 
143
- def response_code_pretty
144
- return unless @response.present?
145
- @response.code.to_s
146
- end
146
+ def response_code_pretty
147
+ return unless @response.present?
148
+ @response.code.to_s
149
+ end
147
150
 
148
- def response_headers_pretty
149
- return unless @response.present?
150
- # JSON.pretty_generate(@response.headers).gsub(/\n/, '<br/>').html_safe # <- This was for RestClient
151
- @response.headers.gsub(/\n/, '<br/>').html_safe # <- This is for Typhoeus
152
- end
151
+ def response_headers_pretty
152
+ return unless @response.present?
153
+ # JSON.pretty_generate(@response.headers).gsub(/\n/, '<br/>').html_safe # <- This was for RestClient
154
+ @response.headers.gsub(/\n/, '<br/>').html_safe # <- This is for Typhoeus
155
+ end
153
156
 
154
- # It's an ActiveModel thing..
155
- def persisted?
156
- false
157
- end
157
+ # It's an ActiveModel thing..
158
+ def persisted?
159
+ false
160
+ end
158
161
 
162
+ end
159
163
  end
@@ -1,50 +1,53 @@
1
1
  # To jog the memory: Resource > Endpoint > Operation > OperationParameter
2
2
 
3
- class Resource
4
-
5
- include ActiveModel::Validations
6
- include ActiveModel::Conversion
7
- extend ActiveModel::Naming
3
+ module Wordnik
4
+ class Resource
5
+ require 'active_model'
6
+ include ActiveModel::Validations
7
+ include ActiveModel::Conversion
8
+ extend ActiveModel::Naming
8
9
 
9
- attr_accessor :name, :raw_data, :endpoints, :models
10
+ attr_accessor :name, :raw_data, :endpoints, :models
10
11
 
11
- validates_presence_of :name, :raw_data, :endpoints, :models
12
+ validates_presence_of :name, :raw_data, :endpoints, :models
12
13
 
13
- def initialize(attributes = {})
14
- attributes.each do |name, value|
15
- send("#{name.to_s.underscore.to_sym}=", value)
16
- end
14
+ def initialize(attributes = {})
15
+ attributes.each do |name, value|
16
+ send("#{name.to_s.underscore.to_sym}=", value)
17
+ end
17
18
 
18
- # Generate Endpoint instances from JSON
19
- if self.raw_data['endPoints']
20
- self.endpoints = self.raw_data['endPoints'].map do |endpointData|
21
- Endpoint.new(endpointData)
19
+ # Generate Endpoint instances from JSON
20
+ if self.raw_data['endPoints']
21
+ self.endpoints = self.raw_data['endPoints'].map do |endpointData|
22
+ Endpoint.new(endpointData)
23
+ end
22
24
  end
23
25
  end
24
- end
25
26
 
26
- def operation_nickname_pairs
27
- return unless self.endpoints.present?
28
- pairs = {}
29
- self.endpoints.map do |endpoint|
30
- endpoint.operations.map do |operation|
31
- nickname_parts = []
32
- nickname_parts << operation.http_method
33
- nickname_parts << endpoint.path.gsub(/\{\w+\}/, "").gsub("/", "_").nix(' ').nix('.').underscore
34
- nickname = nickname_parts.
35
- join("_").
36
- gsub(/_+/, "_").
37
- gsub("_#{self.name.underscore}", "").
38
- gsub(/_$/, "")
39
- pairs[nickname] = "#{operation.http_method.upcase} #{endpoint.path}"
27
+ def operation_nickname_pairs
28
+ return unless self.endpoints.present?
29
+ pairs = {}
30
+ self.endpoints.map do |endpoint|
31
+ endpoint.operations.map do |operation|
32
+ nickname_parts = []
33
+ nickname_parts << operation.http_method
34
+ nickname_parts << endpoint.path.gsub(/\{\w+\}/, "").gsub("/", "_").nix(' ').nix('.').underscore
35
+ nickname = nickname_parts.
36
+ join("_").
37
+ gsub(/_+/, "_").
38
+ gsub("_#{self.name.underscore}", "").
39
+ gsub(/_$/, "")
40
+ pairs[nickname] = "#{operation.http_method.upcase} #{endpoint.path}"
41
+ end
40
42
  end
43
+ pairs
41
44
  end
42
- pairs
43
- end
44
45
 
45
- # It's an ActiveModel thing..
46
- def persisted?
47
- false
48
- end
46
+ # It's an ActiveModel thing..
47
+ def persisted?
48
+ false
49
+ end
49
50
 
51
+ end
52
+
50
53
  end
@@ -1,69 +1,73 @@
1
- class Response
2
- include ActiveModel::Validations
3
- include ActiveModel::Conversion
4
- extend ActiveModel::Naming
1
+ module Wordnik
2
+
3
+ class Response
4
+ require 'active_model'
5
+ include ActiveModel::Validations
6
+ include ActiveModel::Conversion
7
+ extend ActiveModel::Naming
5
8
 
6
- attr_accessor :raw
9
+ attr_accessor :raw
7
10
 
8
- validates_presence_of :raw
11
+ validates_presence_of :raw
9
12
 
10
- def initialize(raw)
11
- self.raw = raw
12
- end
13
+ def initialize(raw)
14
+ self.raw = raw
15
+ end
13
16
 
14
- def code
15
- raw.code
16
- end
17
+ def code
18
+ raw.code
19
+ end
17
20
 
18
- # If body is JSON, parse it
19
- # TODO: If body is XML, parse it
20
- # Otherwise return raw string
21
- def body
22
- JSON.parse(raw.body)
23
- rescue
24
- raw.body
25
- end
21
+ # If body is JSON, parse it
22
+ # TODO: If body is XML, parse it
23
+ # Otherwise return raw string
24
+ def body
25
+ JSON.parse(raw.body)
26
+ rescue
27
+ raw.body
28
+ end
26
29
 
27
- def headers
28
- h = {}
29
- raw.headers_hash.each {|k,v| h[k] = v }
30
- h
31
- end
30
+ def headers
31
+ h = {}
32
+ raw.headers_hash.each {|k,v| h[k] = v }
33
+ h
34
+ end
32
35
 
33
- # Extract the response format from the header hash
34
- # e.g. {'Content-Type' => 'application/json'}
35
- def format
36
- headers['Content-Type'].split("/").last.to_sym
37
- end
36
+ # Extract the response format from the header hash
37
+ # e.g. {'Content-Type' => 'application/json'}
38
+ def format
39
+ headers['Content-Type'].split("/").last.to_sym
40
+ end
38
41
 
39
- def json?
40
- format == :json
41
- end
42
+ def json?
43
+ format == :json
44
+ end
42
45
 
43
- def xml?
44
- format == :xml
45
- end
46
+ def xml?
47
+ format == :xml
48
+ end
46
49
 
47
- def pretty_body
48
- return unless body.present?
49
- case format
50
- when :json
51
- JSON.pretty_generate(body).gsub(/\n/, '<br/>').html_safe
52
- when :xml
53
- xsl = Nokogiri::XSLT(File.open(Rails.root.join("config", "pretty_print.xsl")))
54
- xml = Nokogiri(body)
55
- coder = HTMLEntities.new
56
- coder.encode(xsl.apply_to(xml).to_s)
50
+ def pretty_body
51
+ return unless body.present?
52
+ case format
53
+ when :json
54
+ JSON.pretty_generate(body).gsub(/\n/, '<br/>').html_safe
55
+ when :xml
56
+ xsl = Nokogiri::XSLT(File.open(Rails.root.join("config", "pretty_print.xsl")))
57
+ xml = Nokogiri(body)
58
+ coder = HTMLEntities.new
59
+ coder.encode(xsl.apply_to(xml).to_s)
60
+ end
57
61
  end
58
- end
59
62
 
60
- def pretty_headers
61
- JSON.pretty_generate(headers).gsub(/\n/, '<br/>').html_safe
62
- end
63
+ def pretty_headers
64
+ JSON.pretty_generate(headers).gsub(/\n/, '<br/>').html_safe
65
+ end
63
66
 
64
- # It's an ActiveModel thing..
65
- def persisted?
66
- false
67
- end
67
+ # It's an ActiveModel thing..
68
+ def persisted?
69
+ false
70
+ end
68
71
 
72
+ end
69
73
  end
@@ -1,3 +1,3 @@
1
1
  module Wordnik
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Zeke Sikelianos