mozapi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/mozapi.rb +68 -0
  2. metadata +47 -0
@@ -0,0 +1,68 @@
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
68
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mozapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christoph Engelhardt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: MozAPI is a light-weight wrapper for the MozscapeAPI. It currently supports
15
+ parts of the 'links' endpoint
16
+ email: christoph@it-engelhardt.de
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/mozapi.rb
22
+ homepage: http://www.it-engelhardt.de
23
+ licenses:
24
+ - MIT
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.17
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: A light-weight wrapper around the Mozscape API
47
+ test_files: []