Class: MediaWiktory::Wikipedia::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mediawiktory/wikipedia/client.rb

Overview

Internal low-level client class, used by Api.

Uses Faraday library inside (and will expose it's settings in future).

You should not use it directly, all you need is in Api.

Constant Summary collapse

UA =

Default MediaWiktory User-Agent header.

You can set yours as an option to #initialize

'MediaWiktory/0.1.0 '\
'(https://github.com/molybdenum-99/mediawiktory; [email protected])'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, **options) ⇒ Client

Returns a new instance of Client.



33
34
35
36
37
38
39
40
41
42
# File 'lib/mediawiktory/wikipedia/client.rb', line 33

def initialize(url, **options)
  @url = Addressable::URI.parse(url)
  @options = options
  @faraday = Faraday.new(url, headers: headers) do |f|
    f.request :url_encoded
    f.use FaradayMiddleware::FollowRedirects, limit: 5
    f.use FaradayMiddleware::Gzip
    f.adapter Faraday.default_adapter
  end
end

Class Attribute Details

.user_agentObject

User agent getter/setter.

Default value is UA.

You can also use per-instance option, see #initialize



28
29
30
# File 'lib/mediawiktory/wikipedia/client.rb', line 28

def user_agent
  @user_agent
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



31
32
33
# File 'lib/mediawiktory/wikipedia/client.rb', line 31

def url
  @url
end

Instance Method Details

#get(params) ⇒ Object



48
49
50
# File 'lib/mediawiktory/wikipedia/client.rb', line 48

def get(params)
  @faraday.get('', params).body
end

#post(params) ⇒ Object



52
53
54
# File 'lib/mediawiktory/wikipedia/client.rb', line 52

def post(params)
  @faraday.post('', params).body
end

#user_agentObject



44
45
46
# File 'lib/mediawiktory/wikipedia/client.rb', line 44

def user_agent
  @options[:user_agent] || @options[:ua] || self.class.user_agent || UA
end