mozapi 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mozapi.rb +71 -67
  3. metadata +14 -13
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 77250e2514066976dcaeb9c307281db32d1a433e
4
+ data.tar.gz: f107cc53711a4f14f0373759753c0a1179e3239d
5
+ SHA512:
6
+ metadata.gz: f581807f7b8232cd9555be201ecef4ecde70b48a199bd04f859efa613349206cfae0b4b7ce6bb1887f2df9225d87edd373977bf18477cb0a6f70a91b140ae0b2
7
+ data.tar.gz: 34da714cec0a2df47a39ea2fcd2f93c6d8216a2272a734346f5615c69155befdbc79b699db10ece23e70bb9a9dc0cee669586c2219880253b97959d3379223bb
@@ -1,68 +1,72 @@
1
- require 'httparty'
2
- require 'open-uri'
3
- require 'openssl'
4
- require 'cgi'
5
-
6
- class Hash
7
- # very naive implementation of to_query
8
- def to_query
9
- map { |key, value| "#{key}=#{value}" }.join('&')
10
- end
11
- end
12
-
13
-
14
- class MozAPI
15
- include HTTParty
16
- base_uri 'http://lsapi.seomoz.com/linkscape'
17
-
18
- LIMIT = 100
19
- # URL + root_domain + page_authority + domain_authority
20
- DEFAULT_SOURCE_COLS = 4 + 16 + 34359738368 + 68719476736
21
- # URL + root_domain
22
- DEFAULT_TARGET_COLS = 4 + 16
23
- # anchor_text
24
- DEFAULT_LINK_COLS = 4
25
-
26
-
27
- def initialize
28
- @api_id = ENV['MOZ_API_ID']
29
- @api_key = ENV['MOZ_API_KEY']
30
- end
31
-
32
- def links(target_url, options)
33
- sleep(10) # do this to honor the API rate limit
34
- # add 5 minutes
35
- expires = (Time.now + 5 * 60).utc.to_i
36
-
37
- options = {
38
- Sort: 'page_authority',
39
- Filter: 'follow+external',
40
- SourceCols: DEFAULT_SOURCE_COLS,
41
- TargetCols: DEFAULT_TARGET_COLS,
42
- LinkCols: DEFAULT_LINK_COLS,
43
- AccessID: @api_id,
44
- Expires: expires,
45
- Signature: calculate_signature(expires),
46
- Limit: LIMIT,
47
- Offset: 0
48
- }.merge(options)
49
-
50
- #puts "[MozAPI#links] options: #{options[:Offset]}"
51
- req_url = "http://lsapi.seomoz.com/linkscape/links/#{URI::encode(target_url)}?#{options.to_query}"
52
-
53
- response = HTTParty.get(req_url, :headers => {"User-Agent" => 'node-linkscape (https://github.com/mjp/node-linkscape)'})
54
- json = JSON.parse response.body
55
- puts "[MozAPI#links] links returned: #{json.size}"
56
-
57
- json
58
- end
59
-
60
- # private
61
- def calculate_signature(expires)
62
- signature = "#{@api_id}\n#{expires}"
63
- digest = OpenSSL::HMAC.digest('sha1', @api_key, signature)
64
-
65
- b64 = Digest::HMAC.base64digest(signature, @api_key, Digest::SHA1)
66
- CGI::escape(Base64.encode64(digest).chomp)
67
- end
1
+ require 'httparty'
2
+ require 'open-uri'
3
+ require 'openssl'
4
+ require 'cgi'
5
+
6
+ class Hash
7
+ # very naive implementation of to_query
8
+ def to_query
9
+ map { |key, value| "#{key}=#{value}" }.join('&')
10
+ end
11
+ end
12
+
13
+
14
+ class MozAPI
15
+ include HTTParty
16
+ base_uri 'http://lsapi.seomoz.com/linkscape'
17
+
18
+ # there is a global limit on the Mozscape API that only lets you
19
+ # retrieve 100,000 links for any URL
20
+ GLOBAL_LIMIT = 100000
21
+
22
+ LIMIT = 100
23
+ # URL + root_domain + page_authority + domain_authority
24
+ DEFAULT_SOURCE_COLS = 4 + 16 + 34359738368 + 68719476736
25
+ # URL + root_domain
26
+ DEFAULT_TARGET_COLS = 4 + 16
27
+ # anchor_text
28
+ DEFAULT_LINK_COLS = 4
29
+
30
+
31
+ def initialize
32
+ @api_id = ENV['MOZ_API_ID']
33
+ @api_key = ENV['MOZ_API_KEY']
34
+ end
35
+
36
+ def links(target_url, options)
37
+ sleep(10) # do this to honor the API rate limit
38
+ # add 5 minutes
39
+ expires = (Time.now + 5 * 60).utc.to_i
40
+
41
+ options = {
42
+ Sort: 'page_authority',
43
+ Filter: 'follow+external',
44
+ SourceCols: DEFAULT_SOURCE_COLS,
45
+ TargetCols: DEFAULT_TARGET_COLS,
46
+ LinkCols: DEFAULT_LINK_COLS,
47
+ AccessID: @api_id,
48
+ Expires: expires,
49
+ Signature: calculate_signature(expires),
50
+ Limit: LIMIT,
51
+ Offset: 0
52
+ }.merge(options)
53
+
54
+ #puts "[MozAPI#links] options: #{options[:Offset]}"
55
+ req_url = "http://lsapi.seomoz.com/linkscape/links/#{URI::encode(target_url)}?#{options.to_query}"
56
+
57
+ response = HTTParty.get(req_url, :headers => {"User-Agent" => 'node-linkscape (https://github.com/mjp/node-linkscape)'})
58
+ json = JSON.parse response.body
59
+ puts "[MozAPI#links] links returned: #{json.size}"
60
+
61
+ json
62
+ end
63
+
64
+ # private
65
+ def calculate_signature(expires)
66
+ signature = "#{@api_id}\n#{expires}"
67
+ digest = OpenSSL::HMAC.digest('sha1', @api_key, signature)
68
+
69
+ b64 = Digest::HMAC.base64digest(signature, @api_key, Digest::SHA1)
70
+ CGI::escape(Base64.encode64(digest).chomp)
71
+ end
68
72
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mozapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
5
- prerelease:
4
+ version: 0.1.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Christoph Engelhardt
@@ -13,15 +12,18 @@ date: 2014-05-15 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: httparty
16
- requirement: &27500892 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *27500892
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
25
27
  description: MozAPI is a light-weight wrapper for the MozscapeAPI (http://moz.com/products/api).
26
28
  It currently supports parts of the 'links' endpoint
27
29
  email: christoph@it-engelhardt.de
@@ -30,29 +32,28 @@ extensions: []
30
32
  extra_rdoc_files: []
31
33
  files:
32
34
  - lib/mozapi.rb
33
- homepage: http://www.it-engelhardt.de
35
+ homepage: https://github.com/yas4891/mozapi
34
36
  licenses:
35
37
  - MIT
38
+ metadata: {}
36
39
  post_install_message:
37
40
  rdoc_options: []
38
41
  require_paths:
39
42
  - lib
40
43
  required_ruby_version: !ruby/object:Gem::Requirement
41
- none: false
42
44
  requirements:
43
- - - ! '>='
45
+ - - ">="
44
46
  - !ruby/object:Gem::Version
45
47
  version: '0'
46
48
  required_rubygems_version: !ruby/object:Gem::Requirement
47
- none: false
48
49
  requirements:
49
- - - ! '>='
50
+ - - ">="
50
51
  - !ruby/object:Gem::Version
51
52
  version: '0'
52
53
  requirements: []
53
54
  rubyforge_project:
54
- rubygems_version: 1.8.17
55
+ rubygems_version: 2.2.2
55
56
  signing_key:
56
- specification_version: 3
57
+ specification_version: 4
57
58
  summary: A light-weight wrapper around the Mozscape API
58
59
  test_files: []