al_papi 0.0.8 → 0.0.9

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.
@@ -0,0 +1,25 @@
1
+ module AlPapi
2
+
3
+ class Config
4
+
5
+ DEFAULT_HOST = 'http://api.authoritylabs.com'
6
+ DEFAULT_PORT = 80
7
+
8
+ attr_accessor :api_key
9
+ attr_reader :host, :port
10
+
11
+ ##
12
+ #
13
+ # == Options
14
+ #
15
+ # [auth_key] Your Partner API api key. Required to make any API requests
16
+
17
+ def initialize(options = {})
18
+ @api_key = options[:api_key]
19
+ @host = DEFAULT_HOST
20
+ @port = DEFAULT_PORT
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,11 @@
1
+ module AlPapi
2
+
3
+ class Engines
4
+
5
+ def self.all
6
+ %w(google bing yahoo)
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,54 @@
1
+ module AlPapi
2
+
3
+ class Http # :nodoc:all:
4
+
5
+ attr_accessor :errors, :response, :success, :config
6
+
7
+ def initialize(_config)
8
+ @config, @errors, @success = _config, [], false
9
+ end
10
+
11
+ def get(path, params = {})
12
+ request 'get', "#{path}?#{RestClient::Payload.generate(build_params(params))}"
13
+ end
14
+
15
+ def post(path, params = {})
16
+ request 'post', path, build_params(params)
17
+ end
18
+
19
+ def build_params(params = nil)
20
+ return nil if params.empty?
21
+ return nil unless params.is_a?(Hash)
22
+ params.merge(auth_token: @config.api_key, format: 'json')
23
+ end
24
+
25
+ def request(http_verb, path, params = nil)
26
+ url = "#{@config.host}#{path}"
27
+ args = http_verb == 'post' ? [http_verb, url, params] : [http_verb, url]
28
+
29
+ response = RestClient.send *args do |res, req, raw_res|
30
+ body = raw_res.body
31
+ code = raw_res.code.to_i
32
+
33
+ self.response = body
34
+ self.errors = []
35
+
36
+ case code
37
+ when 200
38
+ self.response = JSON.parse body
39
+ self.success = true
40
+ when 204
41
+ self.errors << RequestError.new('No Content', code, path, params)
42
+ when 401
43
+ self.errors << RequestError.new('Invalid Auth Token Provided', code, path, params)
44
+ else
45
+ self.errors << RequestError.new(body, code, path, params)
46
+ end
47
+
48
+ Response.new(self, code, path, params)
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,227 @@
1
+ module AlPapi
2
+
3
+ class Locales
4
+
5
+ GOOGLE_LOCALES = [
6
+ 'af-za',
7
+ 'ar-ae',
8
+ 'ar-bh',
9
+ 'ar-il',
10
+ 'ar-qa',
11
+ 'ar-sa',
12
+ 'ar-tn',
13
+ 'bg-bg',
14
+ 'ca-ct',
15
+ 'cs-cz',
16
+ 'da-dk',
17
+ 'de-at',
18
+ 'de-be',
19
+ 'de-ch',
20
+ 'de-de',
21
+ 'de-ro',
22
+ 'el-gr',
23
+ 'en-ae',
24
+ 'en-au',
25
+ 'en-be',
26
+ 'en-bh',
27
+ 'en-ca',
28
+ 'en-hk',
29
+ 'en-id',
30
+ 'en-ie',
31
+ 'en-il',
32
+ 'en-in',
33
+ 'en-mt',
34
+ 'en-my',
35
+ 'en-ng',
36
+ 'en-nz',
37
+ 'en-ph',
38
+ 'en-sa',
39
+ 'en-sg',
40
+ 'en-th',
41
+ 'en-tw',
42
+ 'en-ua',
43
+ 'en-uk',
44
+ 'en-us',
45
+ 'en-vn',
46
+ 'en-qa',
47
+ 'en-za',
48
+ 'es-ar',
49
+ 'es-cl',
50
+ 'es-co',
51
+ 'es-es',
52
+ 'es-mx',
53
+ 'es-pe',
54
+ 'es-pr',
55
+ 'es-us',
56
+ 'es-ve',
57
+ 'et-ee',
58
+ 'fi-fl',
59
+ 'fr-be',
60
+ 'fr-ca',
61
+ 'fr-ch',
62
+ 'fr-fr',
63
+ 'fr-tn',
64
+ 'fr-vn',
65
+ 'ga-ie',
66
+ 'ha-ng',
67
+ 'he-il',
68
+ 'hi-in',
69
+ 'hr-hr',
70
+ 'hu-hu',
71
+ 'hu-ro',
72
+ 'id-id',
73
+ 'ie-en',
74
+ 'ig-ng',
75
+ 'it-ch',
76
+ 'it-it',
77
+ 'ja-jp',
78
+ 'jw-id',
79
+ 'ko-kr',
80
+ 'lt-lt',
81
+ 'lt-lv',
82
+ 'lv-lv',
83
+ 'mi-nz',
84
+ 'ms-my',
85
+ 'ms-sg',
86
+ 'mt-mt',
87
+ 'nl-be',
88
+ 'nl-nl',
89
+ 'no-no',
90
+ 'pl-pl',
91
+ 'pt-br',
92
+ 'pt-pt',
93
+ 'rm-ch',
94
+ 'ro-ro',
95
+ 'ru-et',
96
+ 'ru-lv',
97
+ 'ru-ru',
98
+ 'ru-ua',
99
+ 'sl-si',
100
+ 'sk-sk',
101
+ 'sr-rs',
102
+ 'sv-fi',
103
+ 'sv-se',
104
+ 'ta-sg',
105
+ 'th-th',
106
+ 'tl-ph',
107
+ 'tr-tr',
108
+ 'uk-ua',
109
+ 'vi-vn',
110
+ 'xh-za',
111
+ 'yo-ng',
112
+ 'zh-cn-hk',
113
+ 'zh-hk-hk',
114
+ 'zh-cn-sg',
115
+ 'zh-cn-tw',
116
+ 'zh-cn-vn',
117
+ 'zh-tw-tw',
118
+ 'zu-za'
119
+ ]
120
+
121
+ YAHOO_LOCALES = [
122
+ 'da-dk',
123
+ 'de-at',
124
+ 'de-ch',
125
+ 'de-de',
126
+ 'el-gr',
127
+ 'en-au',
128
+ 'en-ca',
129
+ 'en-ie',
130
+ 'en-in',
131
+ 'en-my',
132
+ 'en-nz',
133
+ 'en-ph',
134
+ 'en-sg',
135
+ 'en-tw',
136
+ 'en-uk',
137
+ 'en-us',
138
+ 'es-ar',
139
+ 'es-cl',
140
+ 'es-co',
141
+ 'es-es',
142
+ 'es-mx',
143
+ 'es-pe',
144
+ 'es-us',
145
+ 'es-ve',
146
+ 'fi-fl',
147
+ 'fr-ca',
148
+ 'fr-ch',
149
+ 'fr-fr',
150
+ 'id-id',
151
+ 'it-ch',
152
+ 'it-it',
153
+ 'ja-jp',
154
+ 'ko-kr',
155
+ 'kr-kr',
156
+ 'nl-nl',
157
+ 'no-no',
158
+ 'pt-br',
159
+ 'ro-ro',
160
+ 'ru-ru',
161
+ 'sv-se',
162
+ 'th-th',
163
+ 'tr-tr',
164
+ 'vi-vn',
165
+ 'zh-cn',
166
+ 'zh-cn-tw',
167
+ 'zh-hk'
168
+ ]
169
+
170
+ BING_LOCALES = [
171
+ 'ar-sa',
172
+ 'de-at',
173
+ 'de-ch',
174
+ 'de-de',
175
+ 'da-dk',
176
+ 'en-au',
177
+ 'en-ca',
178
+ 'en-ch',
179
+ 'en-ie',
180
+ 'en-in',
181
+ 'en-nz',
182
+ 'en-sa',
183
+ 'en-sg',
184
+ 'en-uk',
185
+ 'en-us',
186
+ 'es-ar',
187
+ 'es-co',
188
+ 'es-cl',
189
+ 'es-es',
190
+ 'es-mx',
191
+ 'es-pe',
192
+ 'es-us',
193
+ 'es-ve',
194
+ 'fr-ca',
195
+ 'fr-ch',
196
+ 'fr-fr',
197
+ 'it-ch',
198
+ 'it-it',
199
+ 'ja-jp',
200
+ 'ms-my',
201
+ 'nl-be',
202
+ 'nl-nl',
203
+ 'no-no',
204
+ 'pt-br',
205
+ 'rm-ch',
206
+ 'ru-ru',
207
+ 'sv-se',
208
+ 'tl-ph',
209
+ 'vi-vn',
210
+ 'zh-cn',
211
+ 'zh-cn-tw',
212
+ 'zh-hk'
213
+ ]
214
+
215
+ ##
216
+ #
217
+ # [engine] Defaults to google. Returns Partner API supported locales.
218
+ # Allowed engines are google, bing, yahoo.
219
+
220
+ def self.supported(engine = 'google')
221
+ return [] unless AlPapi::Engines.all.include?(engine.downcase)
222
+ self.const_get("#{engine.upcase}_LOCALES")
223
+ end
224
+
225
+ end
226
+
227
+ end
@@ -0,0 +1,75 @@
1
+ module AlPapi
2
+
3
+ class Request
4
+
5
+ attr_reader :config
6
+
7
+ ##
8
+ #
9
+ # [config] * Hash { auth_key: 'your_auth_token' }
10
+ # * OR AlPapi::Config
11
+
12
+ def initialize(config)
13
+ @config = config.is_a?(AlPapi::Config) ? config : Config.new(config)
14
+ @success, @errors = false, []
15
+ end
16
+
17
+ def http # :nodoc:
18
+ Http.new(@config)
19
+ end
20
+
21
+ ##
22
+ # == Params
23
+ #
24
+ # [keyword] <b>Required</b> -
25
+ # Your keyword you want to get results for
26
+ # [engine] <em>Optional</em> -
27
+ # Defaults to google. Allowed engines are google, yahoo, bing.
28
+ # [locale] <em>Optional</em> -
29
+ # Defaults to en-us. See AlPapi::Locales for supported locales.
30
+ # [pages_from] <em>Optional</em> -
31
+ # Default is false. Google specific parameter to get results from
32
+ # the locale passed in when set to true.
33
+ # [callback] <em>Optional</em> -
34
+ # Default is set on your account through the website.
35
+ # Set specific callbacks here for each request. Callback a url that
36
+ # is sent a POST when results are returned.
37
+
38
+ def post(params = {}, priority = false)
39
+ path = priority ? '/keywords/priority' : '/keywords'
40
+ http.post path, params
41
+ end
42
+
43
+ ##
44
+ # == Params
45
+ #
46
+ # See post for required params
47
+
48
+ def priority_post(params = {})
49
+ post params, true
50
+ end
51
+
52
+ ##
53
+ # == Params
54
+ #
55
+ # Parameters should be the same as what was posted to the Partner API where applies.
56
+ #
57
+ # [keyword] <b>Required</b> -
58
+ # The keyword you are ready to get results for.
59
+ # [engine] <em>Optional</em> -
60
+ # Defaults to google. Allowed engines are google, yahoo, bing.
61
+ # [locale] <em>Optional</em> -
62
+ # Defaults to en-us. See AlPapi::Locales for supported locales.
63
+ # [rank_date] <em>Optional</em> -
64
+ # Default is set to today UTC time.
65
+ # Date should be in format of YYYY-MM-DD ie. 2011-12-28
66
+ # [data_format] <em>Optional</em> -
67
+ # Default is JSON. Options are HTML or JSON.
68
+
69
+ def get(params = {})
70
+ http.get '/keywords/get', params
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,13 @@
1
+ module AlPapi
2
+
3
+ class RequestError
4
+
5
+ attr_reader :message, :code, :path, :params
6
+
7
+ def initialize(_message, _code, _path, _params) # :nodoc:
8
+ @message, @code, @path, @params = _message, _code, _path, _params
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,22 @@
1
+ module AlPapi
2
+
3
+ class Response
4
+
5
+ attr_reader :success, :body, :errors, :code, :path, :params
6
+
7
+ def initialize(http, _code, _path, _params) # :nodoc:
8
+ @success, @body, @errors = http.success, http.response, http.errors
9
+ @code, @path, @params = _code, _path, _params
10
+ end
11
+
12
+ ##
13
+ #
14
+ # Convenience method to determine if request was successfull or not
15
+
16
+ def success?
17
+ return @success
18
+ end
19
+
20
+ end
21
+
22
+ end
data/lib/al_papi.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+ require 'net/http'
4
+
5
+ module AlPapi
6
+
7
+ end
8
+
9
+ require File.dirname(__FILE__) + '/al_papi/config'
10
+ require File.dirname(__FILE__) + '/al_papi/http'
11
+ require File.dirname(__FILE__) + '/al_papi/request'
12
+ require File.dirname(__FILE__) + '/al_papi/request_error'
13
+ require File.dirname(__FILE__) + '/al_papi/response'
14
+ require File.dirname(__FILE__) + '/al_papi/locales'
15
+ require File.dirname(__FILE__) + '/al_papi/engines'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: al_papi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-12-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
- requirement: &70345285697560 !ruby/object:Gem::Requirement
16
+ requirement: &70354144621020 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.6.7
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70345285697560
24
+ version_requirements: *70354144621020
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70345285697060 !ruby/object:Gem::Requirement
27
+ requirement: &70354144620500 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '2.0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70345285697060
35
+ version_requirements: *70354144620500
36
36
  description: Wraps AuthorityLabs Partner API calls in a gem.
37
37
  email: ''
38
38
  executables: []
@@ -40,6 +40,14 @@ extensions: []
40
40
  extra_rdoc_files:
41
41
  - README.rdoc
42
42
  files:
43
+ - lib/al_papi.rb
44
+ - lib/al_papi/config.rb
45
+ - lib/al_papi/engines.rb
46
+ - lib/al_papi/http.rb
47
+ - lib/al_papi/locales.rb
48
+ - lib/al_papi/request_error.rb
49
+ - lib/al_papi/request.rb
50
+ - lib/al_papi/response.rb
43
51
  - README.rdoc
44
52
  homepage: http://github.com/mtchavez/al_papi
45
53
  licenses: []