Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Adding user agent name and version to client constructor. #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ RedditKit.rb is used through either the `RedditKit` module, or `RedditKit::Clien
**Module usage:**
```ruby
RedditKit.sign_in 'username', 'password'
RedditKit.user_agent = 'MyRedditBot/1.2 (+http://www.reddit.com/user/username)'
subreddits = RedditKit.subscribed_subreddits
```

**Instance method usage:**
```ruby
client = RedditKit::Client.new 'username', 'password'
client = RedditKit::Client.new 'username', 'password','MyRedditBot','1.2'
subreddits = client.subscribed_subreddits
```

Expand Down Expand Up @@ -102,7 +103,30 @@ end

You can configure various aspects of RedditKit.rb's operation, including its default API endpoint and user agent, by setting attributes on `RedditKit::Client`.

**You should set your user agent to the name and version of your app, along with your reddit username. That way, if you ever have a buggy version of your app abusing the API, the reddit admins will know who to contact.**
**You should set your user agent to the name and version of your app, along with your reddit username. That way, if you ever have a buggy version of your app abusing the API, the reddit admins will know who to contact.
```ruby
#Option 1
MY_BOT_VERSION = 1.7
client = RedditKit::Client.new 'myRedditUserName','password','myBotName',MY_BOT_VERSION
puts client.user_agent
# yields "myRedditUserName/1.7 (+http://www.reddit.com/user/myRedditUserName)"

#Option 2
client = RedditKit::Client.new 'myRedditUserName','password'
client.user_agent_name = 'myBotName'
client.user_agent_version = MY_BOT_VERSION
#This also works with the module usage pattern
RedditKit.sign_in 'myRedditUserName','password'
RedditKit.user_agent_name = 'myBotName'
RedditKit.user_agent_version = MY_BOT_VERSION

#Option 3
client = RedditKit::Client.new 'myRedditUserName','password'
client.user_agent='myCustomAgentString/0.4 (+http://example.com/contact/info.html)'
#This also works with the module usage pattern
RedditKit.sign_in 'myRedditUserName','password'
RedditKit.user_agent='myCustomAgentString/0.4 (+http://example.com/contact/info.html)'
```

## Contributing

Expand Down
28 changes: 23 additions & 5 deletions lib/redditkit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,18 @@ class Client

attr_accessor :api_endpoint
attr_accessor :authentication_endpoint
attr_accessor :user_agent
attr_accessor :middleware
attr_accessor :user_agent
attr_accessor :user_agent_name
attr_accessor :user_agent_version

def initialize(username = nil, password = nil)
def initialize(username = nil, password = nil, agent_name = nil, agent_version = nil)
@username = username
@password = password

@user_agent_name = agent_name
@user_agent_version = agent_version

@cookie = nil
@modhash = nil

Expand All @@ -65,12 +70,25 @@ def api_endpoint
@api_endpoint ||= 'http://www.reddit.com/'
end

def authentication_endpoint
@authentication_endpoint ||= 'https://ssl.reddit.com/'
def user_agent_name
@user_agent_name||="RedditKit.rb"
end

def user_agent_version
@user_agent_version||=RedditKit::Version.to_s
end

def user_agent
@user_agent ||= "RedditKit.rb #{RedditKit::Version.to_s}"
return @user_agent if @user_agent
if username
"#{user_agent_name}/#{user_agent_version} (+" << URI.join(api_endpoint,'/user/',username).to_s << ")"
else
"#{user_agent_name}/#{user_agent_version}"
end
end

def authentication_endpoint
@authentication_endpoint ||= 'https://ssl.reddit.com/'
end

def middleware
Expand Down
24 changes: 22 additions & 2 deletions spec/redditkit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

it "can retrieve default options" do
expect(RedditKit.api_endpoint).to eq 'http://www.reddit.com/'
expect(RedditKit.user_agent).to eq "RedditKit.rb #{RedditKit::Version}"
expect(RedditKit.user_agent).to eq "RedditKit.rb/#{RedditKit::Version}"
end

it "can configure options" do
Expand All @@ -14,6 +14,26 @@
expect(client.api_endpoint).to eq "http://github.com/"
end

it "can configure an agent string" do
client = RedditKit::Client.new nil,nil,"TestAgentBot","86"

expect(client.user_agent).to eq "TestAgentBot/86"


client.user_agent_name = "OtherTestAgentBot"
client.user_agent_version = 99
expect(client.user_agent).to eq "OtherTestAgentBot/99"

client.user_agent = "ThirdTestAgentBot/7.00 (+http://www.reddit.com/user/user/samsymons)"
expect(client.user_agent).to eq "ThirdTestAgentBot/7.00 (+http://www.reddit.com/user/user/samsymons)"

client.user_agent = nil
expect(client.user_agent).to eq "OtherTestAgentBot/99"

client = RedditKit::Client.new "samsymons",nil,"FourthTestAgentBot","86"
expect(client.user_agent).to eq "FourthTestAgentBot/86 (+http://www.reddit.com/user/samsymons)"
end

it "should have a modhash and cookie after signing in" do
expect(authenticated_client.modhash).to_not be_nil
expect(authenticated_client.cookie).to_not be_nil
Expand Down Expand Up @@ -43,4 +63,4 @@
expect(client.modhash).to be_nil
expect(client.cookie).to be_nil
end
end
end