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_versionString

Gets the API version provided by the API.

Returns:

  • (String)

    The returned API version.


117
118
119
# File 'lib/cwsrb/api.rb', line 117

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

#get_lang(val) ⇒ Language

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

Parameters:

  • val (String)

    The language's three-character code.

Returns:

  • (Language)

    The language queried for.

Raises:

  • (APIError)

    If any error occurred while querying for that language


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cwsrb/api.rb', line 52

def get_lang(val)
  val = URI.encode(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'],
    karma: response['KARMA']
  }

  Cwsrb::Language.new(attribs)
end

#get_lang_status(val) ⇒ Language::Status

Translates a language status code to a Language::Status class.

Parameters:

  • val (String)

    The one-character code.

Returns:

Raises:

  • (APIError)

    If any error occurred while translating.


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cwsrb/api.rb', line 100

def get_lang_status(val)
  val = URI.encode(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) ⇒ Language::Type

Translates a language type code to a Language::Type class.

Parameters:

  • val (String)

    The one-character code.

Returns:

Raises:

  • (APIError)

    If any error occurred while translating.


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cwsrb/api.rb', line 81

def get_lang_type(val)
  val = URI.encode(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 occurred 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 = URI.encode(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