wykop 0.6

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.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.travis.yml +5 -0
  4. data/Contributors.md +0 -0
  5. data/Gemfile +8 -0
  6. data/README.md +52 -0
  7. data/Rakefile +8 -0
  8. data/TODO.md +7 -0
  9. data/doc/diggs/add.md +7 -0
  10. data/doc/diggs/favorites.md +17 -0
  11. data/doc/diggs/links.md +14 -0
  12. data/doc/diggs/observatory.md +15 -0
  13. data/doc/diggs/popular.md +14 -0
  14. data/doc/diggs/related.md +20 -0
  15. data/doc/diggs/stream.md +25 -0
  16. data/doc/diggs/tag.md +41 -0
  17. data/doc/diggs/tags.md +11 -0
  18. data/doc/diggs/top.md +44 -0
  19. data/doc/index.md +18 -0
  20. data/doc/search.md +44 -0
  21. data/doc/user/basic.md +81 -0
  22. data/doc/user/link.md +14 -0
  23. data/doc/user/pm.md +13 -0
  24. data/doc/user/rank.md +12 -0
  25. data/lib/wykop.rb +8 -0
  26. data/lib/wykop/client.rb +77 -0
  27. data/lib/wykop/configuration.rb +29 -0
  28. data/lib/wykop/error.rb +14 -0
  29. data/lib/wykop/operations/add.rb +22 -0
  30. data/lib/wykop/operations/favorites.rb +27 -0
  31. data/lib/wykop/operations/link.rb +55 -0
  32. data/lib/wykop/operations/links.rb +18 -0
  33. data/lib/wykop/operations/message.rb +30 -0
  34. data/lib/wykop/operations/observatory.rb +35 -0
  35. data/lib/wykop/operations/popular.rb +18 -0
  36. data/lib/wykop/operations/rank.rb +23 -0
  37. data/lib/wykop/operations/related.rb +24 -0
  38. data/lib/wykop/operations/request.rb +39 -0
  39. data/lib/wykop/operations/search.rb +42 -0
  40. data/lib/wykop/operations/stream.rb +26 -0
  41. data/lib/wykop/operations/tag.rb +33 -0
  42. data/lib/wykop/operations/tags.rb +17 -0
  43. data/lib/wykop/operations/top.rb +52 -0
  44. data/lib/wykop/operations/user.rb +55 -0
  45. data/lib/wykop/version.rb +3 -0
  46. data/spec/spec_helper.rb +16 -0
  47. data/spec/user_spec.rb +70 -0
  48. data/wykop.gemspec +26 -0
  49. metadata +141 -0
@@ -0,0 +1,14 @@
1
+ ### Link
2
+
3
+ ```
4
+ client.link_index( :param1 => 2248060 )
5
+ client.link_dig( :param1 => 2248060 )
6
+ client.link_cancel( :param1 => 2248060 )
7
+ client.link_bury( :param1 => 2248060 )
8
+ client.link_comments( :param1 => 2248060 )
9
+ client.link_reports( :param1 => 2248060 )
10
+ client.link_digs( :param1 => 2248060 )
11
+ client.link_buryreasons
12
+ client.link_observe( :param1 => 2248060 )
13
+ client.link_favorite( :param1 => 2248060 )
14
+ ```
@@ -0,0 +1,13 @@
1
+ ### PM ( private messaging )
2
+
3
+ ```
4
+ client.messages_list
5
+
6
+ # Reads all messages from user 'ouna-'
7
+ client.messages_read(:param1 => 'ouna-')
8
+
9
+ # delete all messages from user 'ouna-'
10
+ client.messages_delete(:param1 => 'ouna-')
11
+
12
+ # send message 'test wykop gem' to user 'ouna-' with wykop.pl logo attached
13
+ client.message_send(:param1 => 'ouna-', :body => 'test wykop gem', :embed => 'http://wykop.pl/logo.png')
@@ -0,0 +1,12 @@
1
+ ### Rank
2
+
3
+ Displays current rank on wykop.pl
4
+
5
+ ```
6
+ # default: rank, incremental
7
+ client.rank_show
8
+
9
+ # other switches
10
+ # comment_count, link_count, hp_link_count, followers_count
11
+ client.rank_show({:order => 'followers_count'})
12
+ ```
@@ -0,0 +1,8 @@
1
+ require 'wykop/version'
2
+ require 'wykop/client'
3
+
4
+ module Wykop
5
+ def self.client
6
+ @client ||= Wykop::Client.new
7
+ end
8
+ end
@@ -0,0 +1,77 @@
1
+ require 'wykop/configuration'
2
+ require 'wykop/operations/user'
3
+ require 'wykop/error'
4
+ require 'awesome_print'
5
+ require 'httparty'
6
+ require 'json'
7
+
8
+ require 'wykop/operations/request'
9
+
10
+ require 'wykop/operations/add'
11
+ require 'wykop/operations/top'
12
+ require 'wykop/operations/tag'
13
+ require 'wykop/operations/tags'
14
+ require 'wykop/operations/rank'
15
+ require 'wykop/operations/link'
16
+ require 'wykop/operations/links'
17
+ require 'wykop/operations/search'
18
+ require 'wykop/operations/stream'
19
+ require 'wykop/operations/popular'
20
+ require 'wykop/operations/related'
21
+ require 'wykop/operations/message'
22
+ require 'wykop/operations/favorites'
23
+ require 'wykop/operations/observatory'
24
+
25
+ module Wykop
26
+ class Client
27
+ REQUEST_CLASSES = [Wykop::Operations::User, Wykop::Operations::Request, Wykop::Operations::Top,
28
+ Wykop::Operations::Popular, Wykop::Operations::Links, Wykop::Operations::Rank,
29
+ Wykop::Operations::Observatory, Wykop::Operations::Tags, Wykop::Operations::Stream,
30
+ Wykop::Operations::Search, Wykop::Operations::Add, Wykop::Operations::Related,
31
+ Wykop::Operations::Favorites, Wykop::Operations::Messages, Wykop::Operations::Link,
32
+ Wykop::Operations::Tag]
33
+ attr_reader :configuration
34
+ attr_accessor :user_info
35
+ attr_accessor :request_data
36
+
37
+ def initialize(options = nil)
38
+ @configuration = nil
39
+ @user_info = nil
40
+ @request_data = nil
41
+ define_request_methods
42
+
43
+ unless options.nil?
44
+ @configuration = Configuration.new(options)
45
+ check_api_keys
46
+ end
47
+ end
48
+
49
+ def check_api_keys
50
+ if configuration.nil? || !configuration.valid?
51
+ @configuration = nil
52
+ raise Error::NoApiKeys
53
+ else
54
+ @configuration.freeze
55
+ end
56
+ end
57
+
58
+ def define_request_methods
59
+ REQUEST_CLASSES.each do |request_class|
60
+ operations_instance = request_class.new(self)
61
+ create_methods_from_instance(operations_instance)
62
+ end
63
+ end
64
+
65
+ def create_methods_from_instance(instance)
66
+ instance.public_methods(false).each do |method_name|
67
+ add_method(instance, method_name)
68
+ end
69
+ end
70
+
71
+ def add_method(instance, method_name)
72
+ define_singleton_method(method_name) do |*args|
73
+ instance.public_send(method_name, *args)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,29 @@
1
+ module Wykop
2
+ class Configuration
3
+ AUTH_KEYS = [:app_user_key, :app_username, :app_generated_key, :app_user_secret, :api_host]
4
+ attr_accessor(*AUTH_KEYS)
5
+
6
+ # Creates configuration
7
+ # @param [Hash] - hash containing config params and their values
8
+ # @return [Configuration] - a new configuration with the vaules from the config_hash set
9
+ def initialize(config_hash = nil)
10
+ if config_hash.is_a?(Hash)
11
+ config_hash.each do |config_name, config_value|
12
+ self.send("#{config_name}=", config_value)
13
+ end
14
+ end
15
+ end
16
+
17
+ # Returns a hash of api keys and their values
18
+ def auth_keys
19
+ AUTH_KEYS.inject({}) do |keys_hash, key|
20
+ keys_hash[key] = send(key)
21
+ keys_hash
22
+ end
23
+ end
24
+
25
+ def valid?
26
+ AUTH_KEYS.none? { |key| send(key).nil? || send(key).empty? }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ module Wykop
2
+ module Error
3
+ # Validates Wykop API responses
4
+
5
+ class Base < StandardError;
6
+ end
7
+
8
+ class NoApiKeys < Base
9
+ def initialize(msg = "Missing API credentials.")
10
+ super
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ module Wykop
2
+ module Operations
3
+ class Add
4
+ # Documentation: http://www.wykop.pl/dla-programistow/dokumentacja/#info6_9
5
+ def initialize(client)
6
+ @client = client
7
+ @request = Wykop::Operations::Request.new(@client)
8
+ end
9
+
10
+ def add_new(p = {})
11
+ # Adding new link
12
+ p.delete(:group)
13
+ @request.execute(replace_url( { :banana => 'add', :potato => 'index', :group => p[:group].gsub(/^\#/, '') } ), p)
14
+ end
15
+
16
+ def replace_url( p = {} )
17
+ standard_url = "#{@client.configuration.api_host}/banana/potato/appkey,#{@client.configuration.app_user_key}/userkey,#{@client.user_info['userkey']}/group,groupid"
18
+ return standard_url.gsub(/banana/, p[:banana]).gsub(/potato/, p[:potato]).gsub(/groupid/, p[:group])
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ module Wykop
2
+ module Operations
3
+ class Favorites
4
+ # Documentation: http://www.wykop.pl/dla-programistow/dokumentacja/#info6_15
5
+ def initialize(client)
6
+ @client = client
7
+ @request = Wykop::Operations::Request.new(@client)
8
+ end
9
+
10
+ def favorite_index( p = {} )
11
+ return @request.execute(@request.replace_url({ :banana => 'favorites', :potato => 'index', :param1 => p[:param1] } ), Hash.new)
12
+ end
13
+
14
+ def favorite_comments( p = {} )
15
+ return @request.execute(@request.replace_url({ :banana => 'favorites', :potato => 'comments' } ), Hash.new)
16
+ end
17
+
18
+ def favorite_entries( p = {} )
19
+ return @request.execute(@request.replace_url({ :banana => 'favorites', :potato => 'entries' } ), Hash.new)
20
+ end
21
+
22
+ def favorite_lists( p = {} )
23
+ return @request.execute(@request.replace_url({ :banana => 'favorites', :potato => 'lists' } ), Hash.new)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,55 @@
1
+ module Wykop
2
+ module Operations
3
+ class Link
4
+ # Documentation: http://www.wykop.pl/dla-programistow/dokumentacja/#info6_2
5
+ def initialize(client)
6
+ @client = client
7
+ @request = Wykop::Operations::Request.new(@client)
8
+ end
9
+
10
+ def link_index( p = {} )
11
+ return @request.execute(@request.replace_url({ :banana => 'links', :potato => 'index', :param1 => p[:param1] }), Hash.new)
12
+ end
13
+
14
+ def link_dig( p = {} )
15
+ return @request.execute(@request.replace_url({ :banana => 'links', :potato => 'dig', :param1 => p[:param1] }), Hash.new)
16
+ end
17
+
18
+ def link_cancel( p = {} )
19
+ return @request.execute(@request.replace_url({ :banana => 'links', :potato => 'cancel', :param1 => p[:param1] }), Hash.new)
20
+ end
21
+
22
+ def link_bury( p = {} )
23
+ return @request.execute(@request.replace_url({ :banana => 'links', :potato => 'cancel', :param1 => p[:param1], :param2 => p[:param2] }), Hash.new)
24
+ end
25
+
26
+ def link_comments( p = {} )
27
+ return @request.execute(@request.replace_url({ :banana => 'links', :potato => 'comments', :param1 => p[:param1] }), Hash.new)
28
+ end
29
+
30
+ def link_reports( p = {} )
31
+ return @request.execute(@request.replace_url({ :banana => 'links', :potato => 'reports', :param1 => p[:param1] }), Hash.new)
32
+ end
33
+
34
+ def link_digs( p = {} )
35
+ return @request.execute(@request.replace_url({ :banana => 'links', :potato => 'digs', :param1 => p[:param1] }), Hash.new)
36
+ end
37
+
38
+ def link_related( p = {} )
39
+ return @request.execute(@request.replace_url({ :banana => 'links', :potato => 'related', :param1 => p[:param1] }), Hash.new)
40
+ end
41
+
42
+ def link_buryreasons( p = {} )
43
+ return @request.execute(@request.replace_url({ :banana => 'links', :potato => 'buryreasons' }), Hash.new)
44
+ end
45
+
46
+ def link_observe( p = {} )
47
+ return @request.execute(@request.replace_url({ :banana => 'links', :potato => 'observe', :param1 => p[:param1] }), Hash.new)
48
+ end
49
+
50
+ def link_favorite( p = {} )
51
+ return @request.execute(@request.replace_url({ :banana => 'links', :potato => 'favorite', :param1 => p[:param1] }), Hash.new)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,18 @@
1
+ module Wykop
2
+ module Operations
3
+ class Links
4
+ # Documentation: http://www.wykop.pl/dla-programistow/dokumentacja/#info6_3
5
+ def initialize(client)
6
+ @client = client
7
+ @request = Wykop::Operations::Request.new(@client)
8
+ end
9
+
10
+ def links_show( p = {} )
11
+ if ! p.has_key?(:category); p[:category] = 'promoted'; end
12
+ q_url = @request.replace_url({ :banana => 'links', :potato => p[:category]})
13
+ q_body = Hash.new
14
+ return @request.execute(q_url, q_body)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ module Wykop
2
+ module Operations
3
+ class Messages
4
+ # Documentation: http://www.wykop.pl/dla-programistow/dokumentacja/#info6_18
5
+ def initialize(client)
6
+ @client = client
7
+ @request = Wykop::Operations::Request.new(@client)
8
+ end
9
+
10
+ def messages_list
11
+ return @request.execute(@request.replace_url({ :banana => 'pm', :potato => 'conversationslist' } ), Hash.new)
12
+ end
13
+
14
+ def messages_read( p = {} )
15
+ p.delete(:param1)
16
+ return @request.execute(@request.replace_url({ :banana => 'pm', :potato => 'conversation', :param1 => p[:param1] } ), p)
17
+ end
18
+
19
+ def messages_delete( p = {} )
20
+ p.delete(:param1)
21
+ return @request.execute(@request.replace_url({ :banana => 'pm', :potato => 'deleteconversation', :param1 => p[:param1] } ), p)
22
+ end
23
+
24
+ def message_send( p = {} )
25
+ p.delete(:param1)
26
+ return @request.execute(@request.replace_url({ :banana => 'pm', :potato => 'sendmessage', :param1 => p[:param1] } ), p)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,35 @@
1
+ module Wykop
2
+ module Operations
3
+ class Observatory
4
+ # Documentation: http://www.wykop.pl/dla-programistow/dokumentacja/#info6_14
5
+ def initialize(client)
6
+ @client = client
7
+ @request = Wykop::Operations::Request.new(@client)
8
+ end
9
+
10
+ def observatory_votes
11
+ q_url = @request.replace_url({ :banana => 'observatory', :potato => 'votes'})
12
+ q_body = Hash.new
13
+ @request.execute(q_url, q_body)
14
+ end
15
+
16
+ def observatory_comments
17
+ q_url = @request.replace_url({ :banana => 'observatory', :potato => 'comments'})
18
+ q_body = Hash.new
19
+ @request.execute(q_url, q_body)
20
+ end
21
+
22
+ def observatory_entries
23
+ q_url = @request.replace_url({:banana => 'observatory', :potato => 'entries'})
24
+ q_body = Hash.new
25
+ @request.execute(q_url, q_body)
26
+ end
27
+
28
+ def observatory_entries_comments
29
+ q_url = @request.replace_url({:banana => 'observatory', :potato => 'entries_comments'})
30
+ q_body = Hash.new
31
+ @request.execute(q_url, q_body)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,18 @@
1
+ module Wykop
2
+ module Operations
3
+ class Popular
4
+ # Documentation: http://www.wykop.pl/dla-programistow/dokumentacja/#info6_4
5
+ def initialize(client)
6
+ @client = client
7
+ @request = Wykop::Operations::Request.new(@client)
8
+ end
9
+
10
+ def popular_show( p = {} )
11
+ if ! p.has_key?(:category); p[:category] = 'promoted'; end
12
+ q_url = @request.replace_url({ :banana => 'popular', :potato => p[:category]})
13
+ q_body = Hash.new
14
+ return @request.execute(q_url, q_body)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module Wykop
2
+ module Operations
3
+ class Rank
4
+ # Documentation: http://www.wykop.pl/dla-programistow/dokumentacja/#info6_13
5
+ def initialize(client)
6
+ @client = client
7
+ @request = Wykop::Operations::Request.new(@client)
8
+ end
9
+
10
+ def rank_show( p = {} )
11
+ if ! p.has_key?(:order); p[:order] = 'rank'; end
12
+ q_url = replace_url({ :banana => 'rank', :potato => 'index', :order => p[:order] })
13
+ q_body = Hash.new
14
+ return @request.execute(q_url, q_body)
15
+ end
16
+
17
+ def replace_url( p = {} )
18
+ standard_url = "#{@client.configuration.api_host}/banana/potato/order/order_content/appkey,#{@client.configuration.app_user_key}/userkey,#{@client.user_info['userkey']}"
19
+ return standard_url.gsub(/banana/, p[:banana]).gsub(/potato/, p[:potato]).gsub(/order_content/, p[:order])
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ module Wykop
2
+ module Operations
3
+ class Related
4
+ # Documentation: http://www.wykop.pl/dla-programistow/dokumentacja/#info6_10
5
+ def initialize(client)
6
+ @client = client
7
+ @request = Wykop::Operations::Request.new(@client)
8
+ end
9
+
10
+ def related_vote_up( p = {} )
11
+ return @request.execute(@request.replace_url({ :banana => 'related', :potato => 'plus', :param1 => p[:param1], :param2 => p[:param2] } ), Hash.new)
12
+ end
13
+
14
+ def related_vote_down( p = {} )
15
+ return @request.execute(@request.replace_url({ :banana => 'related', :potato => 'minus', :param1 => p[:param1], :param2 => p[:param2] } ), Hash.new)
16
+ end
17
+
18
+ def related_add( p = {} )
19
+ p.delete(:param1)
20
+ return @request.execute(@request.replace_url({ :banana => 'related', :potato => 'add', :param1 => p[:param1] } ), p)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,39 @@
1
+ require 'digest/md5'
2
+ require 'httparty'
3
+
4
+ module Wykop
5
+ module Operations
6
+ class Request
7
+ # Class created to prepare and parse all requests, includes signing replacing parameters.
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ def replace_url( p = {} )
13
+ standard_url = "#{@client.configuration.api_host}/banana/potato/param1/param2/appkey,#{@client.configuration.app_user_key}/userkey,#{@client.user_info['userkey']}"
14
+ if ! p[:param1].nil?
15
+ standard_url = standard_url.gsub(/param1/, p[:param1].to_s)
16
+ else
17
+ standard_url = standard_url.gsub(/\/param1/, '')
18
+ end
19
+ if ! p[:param2].nil?
20
+ standard_url = standard_url.gsub(/param2/, p[:param2].to_s)
21
+ else
22
+ standard_url = standard_url.gsub(/\/param2/, '')
23
+ end
24
+ return standard_url.gsub(/banana/, p[:banana]).gsub(/potato/, p[:potato])
25
+ end
26
+
27
+ def execute(req_url = nil, req_data = nil)
28
+ sorted_request_body = Hash[req_data.sort_by { |key, value| key }]
29
+ q_body_parsed = []
30
+ sorted_request_body.each do |sval|
31
+ q_body_parsed.push(sval[1])
32
+ end
33
+ request_signature = Digest::MD5.hexdigest(@client.configuration.app_user_secret + req_url + q_body_parsed.join(','))
34
+ obj = HTTParty.post(req_url.to_s, :body => req_data, :headers => {"apisign" => request_signature}).to_json
35
+ JSON.parse(obj)
36
+ end
37
+ end
38
+ end
39
+ end