Class: Cwsrb::API

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cwsrb/api.rb

Overview

This is the main class from which you use CWSrb. It includes HTTParty with a base_uri of "http://conworkshop.com".

Instance Method Summary collapse

Constructor Details

#initialize(user_agent) ⇒ API

Initialize a new instance of the API object, with a mandatory custom User-Agent for HTTP requests. This sets the User-Agent header for all subsequent requests, with your custom User-Agent and CWSrb's User-Agent.

Parameters:

  • user_agent (String)

    The User-Agent to use with HTTP requests. Please use a descriptive one.



17
18
19
20
# File 'lib/cwsrb/api.rb', line 17

def initialize(user_agent)
  self.class.headers 'User-Agent' =>
                         "#{user_agent} CWSrb/#{Cwsrb::VERSION} (#{RUBY_ENGINE}/#{RUBY_VERSION}p#{RUBY_PATCHLEVEL})"
end

Instance Method Details

#api_versionObject



99
100
101
# File 'lib/cwsrb/api.rb', line 99

def api_version
  self.class.get('/api')['api_ver']
end

#get_lang(val) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cwsrb/api.rb', line 48

def get_lang(val)
  response = self.class.get("/api/LANG/#{val}")

  Cwsrb::Helpers.check_for_errors(response)

  response = response['out']
  attribs = {
    code: response['CODE'],
    name: response['NAME'],
    native_name: response['NATIVE_NAME'],
    ipa: response['IPA'],
    type: get_lang_type(response['TYPE']),
    owners: response['OWNERS'],
    overview: response['OVERVIEW'],
    public: response['PUBLIC'],
    status: get_lang_status(response['STATUS']),
    registered: Time.at(response['REGISTERED']),
    word_count: response['WORD_COUNT']
  }

  Cwsrb::Language.new(attribs)
end

#get_lang_status(val) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cwsrb/api.rb', line 85

def get_lang_status(val)
  response = self.class.get("/api/LANG/STATUS/#{val}")

  Cwsrb::Helpers.check_for_errors(response)

  response = response['out']
  attribs = {
    code: response['STATUS'].upcase,
    desc: response['DESC']
  }

  Cwsrb::Language::Status.new(attribs)
end

#get_lang_type(val) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cwsrb/api.rb', line 71

def get_lang_type(val)
  response = self.class.get("/api/LANG/TYPE/#{val}")

  Cwsrb::Helpers.check_for_errors(response)

  response = response['out']
  attribs = {
    code: response['TYPE'].upcase,
    desc: response['DESC']
  }

  Cwsrb::Language::Type.new(attribs)
end

#get_user(val) ⇒ User

Gets a ConWorkShop's user's info plus karma counts.

Parameters:

  • val (String, Integer)

    The user's name or ID.

Returns:

  • (User)

    The user queried for

Raises:

  • (APIError)

    If any error ocurred while querying for that user



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cwsrb/api.rb', line 26

def get_user(val)
  usr = Cwsrb::Helpers.resolve(val)

  # First general info
  response = self.class.get("/api/USER/#{usr}")

  # Check if any errors occurred
  Cwsrb::Helpers.check_for_errors(response)

  response = response['out']
  attribs = {
    id: response['USER_ID'],
    name: response['USER_NAME'],
    gender: response['USER_GENDER'],
    bio: response['USER_BIO'],
    country: response['USER_COUNTRY'],
    karma: response['KARMA']
  }

  Cwsrb::User.new(attribs)
end