act_as_api_client 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f1c7c6c3d7211351b1bd796c0f246656d4cf5e860c586d88a52da0c133b8622
4
- data.tar.gz: 525403634a6f733f4ef1729b8bbc7dc4d30087029eefb8aecfe025642ed518d1
3
+ metadata.gz: 2872e7e3132a079bc57e2143b28a3898b18145033cde50bb6375c29400e91b38
4
+ data.tar.gz: 186709908bdf7221b87e8c12879843bb0ebe03ab87741cb1d9af562a1aebe057
5
5
  SHA512:
6
- metadata.gz: '028bb21fa8d582e88781684576b07878a00bdef6b556de591bd23a3c3fbf43be12a71f673a55d189f49ca45accb94bfbc86ff2d90a31a9b84bb35c354f0ffb59'
7
- data.tar.gz: b5769d9b5db7fafb4cf7b4cf2ee9a98278154ad1536ddb83be48634f81085c4ee84de16daa1172e6000bf7602710519012efd9ebf115e46094687e39b813318a
6
+ metadata.gz: 00443a970f37aebf5b33cf1e32c6ff3f7dc9fafeef5cd1e36ec1e102ca2d856088dd152ea327a13628c715fc7bf187979b3183bf290b3aee5c46a264354df37d
7
+ data.tar.gz: a0781851c2b72663c7e930a3504e2b828a4e894cebf4bd65810f49ca361ada94c193959af25fa43d68ed09d8753e27207d5ffe9c025dbf3d2c9c9a9d9753e32d
data/.gitignore CHANGED
@@ -12,4 +12,6 @@
12
12
 
13
13
  .idea
14
14
  .byebug_history
15
- spec/credentials.yml
15
+ spec/credentials.yml
16
+
17
+ *.gem
data/.rubocop.yml CHANGED
@@ -15,4 +15,8 @@ Layout/LineLength:
15
15
  Max: 120
16
16
 
17
17
  AllCops:
18
- NewCops: enable
18
+ NewCops: enable
19
+ TargetRubyVersion: 2.4
20
+
21
+ Style/Documentation:
22
+ Enabled: false
data/.rubocop_todo.yml CHANGED
@@ -1,91 +1,56 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config --auto-gen-only-exclude`
3
- # on 2022-07-04 14:26:01 UTC using RuboCop version 0.93.1.
3
+ # on 2022-07-11 09:13:33 UTC using RuboCop version 0.93.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
9
  # Offense count: 1
10
- # Configuration parameters: Include.
11
- # Include: **/*.gemspec
12
- Gemspec/RequiredRubyVersion:
13
- Exclude:
14
- - 'act_as_api_client.gemspec'
15
-
16
- # Offense count: 7
17
- Lint/ConstantDefinitionInBlock:
18
- Exclude:
19
- - 'spec/act_as_api_client_spec.rb'
20
- - 'spec/clients/github_client_spec.rb'
21
-
22
- # Offense count: 1
23
- # Cop supports --auto-correct.
24
- # Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods.
25
- Lint/UnusedMethodArgument:
10
+ # Configuration parameters: IgnoredMethods, Max.
11
+ Metrics/AbcSize:
26
12
  Exclude:
27
13
  - 'lib/act_as_api_client/clients/github_client.rb'
28
14
 
29
- # Offense count: 1
15
+ # Offense count: 4
30
16
  # Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods.
31
17
  # ExcludedMethods: refine
32
18
  Metrics/BlockLength:
33
19
  Exclude:
34
20
  - '**/*.gemspec'
35
- - 'spec/act_as_api_client_spec.rb'
21
+ - 'spec/act_as_api_client/clients/github_client/find_by_spec.rb'
22
+ - 'spec/act_as_api_client/clients/github_client/find_spec.rb'
23
+ - 'spec/act_as_api_client/clients/github_client/where_spec.rb'
36
24
 
37
25
  # Offense count: 1
26
+ # Configuration parameters: IgnoredMethods, Max.
27
+ Metrics/CyclomaticComplexity:
28
+ Exclude:
29
+ - 'lib/act_as_api_client/clients/github_client.rb'
30
+
31
+ # Offense count: 2
38
32
  # Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods.
39
33
  Metrics/MethodLength:
40
34
  Exclude:
41
35
  - 'lib/act_as_api_client/clients/github_client.rb'
36
+ - 'lib/act_as_api_client/clients/http_client.rb'
42
37
 
43
38
  # Offense count: 2
44
39
  Naming/AccessorMethodName:
45
40
  Exclude:
46
41
  - 'lib/act_as_api_client.rb'
47
42
 
48
- # Offense count: 1
49
- # Configuration parameters: Prefixes.
50
- # Prefixes: when, with, without
51
- RSpec/ContextWording:
52
- Exclude:
53
- - 'spec/act_as_api_client_spec.rb'
54
-
55
43
  # Offense count: 1
56
44
  # Configuration parameters: Max.
57
45
  RSpec/ExampleLength:
58
46
  Exclude:
59
- - 'spec/act_as_api_client_spec.rb'
47
+ - 'spec/act_as_api_client/clients/http_client_spec.rb'
60
48
 
61
- # Offense count: 1
62
- # Configuration parameters: CustomTransform, IgnoreMethods, SpecSuffixOnly.
63
- RSpec/FilePath:
64
- Exclude:
65
- - 'spec/clients/github_client_spec.rb'
66
-
67
- # Offense count: 7
68
- RSpec/LeakyConstantDeclaration:
69
- Exclude:
70
- - 'spec/act_as_api_client_spec.rb'
71
- - 'spec/clients/github_client_spec.rb'
72
-
73
- # Offense count: 5
74
- Style/Documentation:
75
- Exclude:
76
- - 'spec/**/*'
77
- - 'test/**/*'
78
- - 'demo.rb'
79
- - 'lib/act_as_api_client.rb'
80
- - 'lib/act_as_api_client/base_api_methods.rb'
81
- - 'lib/act_as_api_client/clients/github_client.rb'
82
- - 'lib/act_as_api_client/clients/http_client.rb'
83
-
84
- # Offense count: 1
85
- # Configuration parameters: MinBodyLength.
86
- Style/GuardClause:
49
+ # Offense count: 3
50
+ # Configuration parameters: Max.
51
+ RSpec/NestedGroups:
87
52
  Exclude:
88
- - 'lib/act_as_api_client.rb'
53
+ - 'spec/act_as_api_client/clients/github_client/find_by_spec.rb'
89
54
 
90
55
  # Offense count: 1
91
56
  # Cop supports --auto-correct.
@@ -93,4 +58,4 @@ Style/GuardClause:
93
58
  # URISchemes: http, https
94
59
  Layout/LineLength:
95
60
  Exclude:
96
- - 'act_as_api_client.gemspec'
61
+ - 'lib/act_as_api_client/clients/github_client.rb'
data/Gemfile.lock CHANGED
@@ -1,8 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- act_as_api_client (0.1.0)
5
- httparty (~> 0.20)
4
+ act_as_api_client (0.1.1)
6
5
 
7
6
  GEM
8
7
  remote: https://rubygems.org/
@@ -15,13 +14,6 @@ GEM
15
14
  rexml
16
15
  diff-lcs (1.5.0)
17
16
  hashdiff (1.0.1)
18
- httparty (0.20.0)
19
- mime-types (~> 3.0)
20
- multi_xml (>= 0.5.2)
21
- mime-types (3.4.1)
22
- mime-types-data (~> 3.2015)
23
- mime-types-data (3.2022.0105)
24
- multi_xml (0.6.0)
25
17
  parallel (1.22.1)
26
18
  parser (3.1.2.0)
27
19
  ast (~> 2.4.1)
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  How to create api clients for your application? What is a better way to encapsualte interactions with third APIs? My answer is `act_as_api_client`.
4
4
 
5
- Let's assume you have a typical Rails or any ruby application and want to play around with an API, Github for example. See the [Usage](#usage) section to find how to use existing preconfigured API's and encapsulate all logic inside `APIClient` classes.
5
+ Let's assume you have a typical Rails or any ruby application and want to play around with an API, Github for example. See the [Usage](https://github.com/Rukomoynikov/act_as_api_client#usage) section to find how to use existing preconfigured API's and encapsulate all logic inside `APIClient` classes.
6
6
 
7
7
  _At the moment i experiment in order to make api clients behavior very similiar to `ActiveRecord` models, so out of the box most of the clients support these methods: find, where, delete, update, find_by, create, update_
8
8
 
@@ -24,10 +24,10 @@ Or install it yourself as:
24
24
 
25
25
  ## Usage
26
26
 
27
- **1. Folder for clients:**
28
- Create a folder for examples api_clients inside your `lib` directory.
27
+ ### Folder for clients:
28
+ Create a folder in your app for examples `api_clients` inside your `lib` directory. This will be the place where you will keep all... api clients.
29
29
 
30
- **2. Create API client class:**
30
+ ### Create API client class:
31
31
  For example you want to fetch and update Github repositoties, then you class may have a form like this:
32
32
 
33
33
  ```ruby
@@ -36,7 +36,7 @@ class GithubClient < ApiClient
36
36
  end
37
37
  ```
38
38
 
39
- In case you want to provide and use auth token for Github:
39
+ In case you want to provide and use authorization token for Github:
40
40
 
41
41
  ```ruby
42
42
  class GithubClient < ApiClient
@@ -46,8 +46,17 @@ class GithubClient < ApiClient
46
46
  }
47
47
  end
48
48
  ```
49
+ btw, all values from `with` hash will be availabe in tour clients as instance variable `@options`
49
50
 
50
- btw, all values from `with` hash will be availabe in tour clients as instance variable `@options`
51
+ ### Use it:
52
+ Let's fetch all repositories from Rails organization:
53
+
54
+ ```ruby
55
+ github_client = GithubClient.new()
56
+ github_client.find_by(organization: 'rails')
57
+ ```
58
+
59
+ Voila.
51
60
 
52
61
  ## List of supported api clients
53
62
  1. [Github Repositories](https://docs.github.com/en/rest/repos/repos)
@@ -58,14 +67,6 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
58
67
 
59
68
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
69
 
61
- ## Contributing
62
-
63
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/act_as_api_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/act_as_api_client/blob/master/CODE_OF_CONDUCT.md).
64
-
65
70
  ## License
66
71
 
67
72
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
68
-
69
- ## Code of Conduct
70
-
71
- Everyone interacting in the ActAsApiClient project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/act_as_api_client/blob/master/CODE_OF_CONDUCT.md).
@@ -9,13 +9,17 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ["rukomoynikov@gmail.com"]
10
10
 
11
11
  spec.summary = "Collection of predefined API clients"
12
- spec.description = "Helps you to build reliable API clients in a minute. Just add act_as_api_client to your classes."
12
+ spec.description = "Helps you to build reliable API clients in a minute. Just add act_as_api_client to your classes"
13
13
  spec.homepage = "https://rubygems.org/gems/act_as_api_client"
14
14
  spec.license = "MIT"
15
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
16
 
17
- spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = "https://github.com/Rukomoynikov/act_as_api_client"
17
+ spec.metadata = {
18
+ "source_code_uri" => "https://github.com/Rukomoynikov/act_as_api_client",
19
+ "homepage_uri" => spec.homepage,
20
+ "documentation_uri" => "https://rubydoc.info/github/Rukomoynikov/act_as_api_client/main",
21
+ "bug_tracker_uri" => "https://github.com/Rukomoynikov/act_as_api_client/issues"
22
+ }
19
23
 
20
24
  # Specify which files should be added to the gem when it is released.
21
25
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -25,7 +29,6 @@ Gem::Specification.new do |spec|
25
29
  spec.require_paths = ["lib"]
26
30
 
27
31
  # Uncomment to register a new dependency of your gem
28
- spec.add_dependency "httparty", "~> 0.20"
29
32
 
30
33
  # For more information and examples about making a new gem, checkout our
31
34
  # guide at: https://bundler.io/guides/creating_gem.html
@@ -1,50 +1,94 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "http_client"
4
- require "httparty"
5
4
 
6
5
  module ActAsApiClient
7
6
  module Clients
8
7
  module GithubClient
9
8
  include HttpClient
9
+
10
+ # Searches Github for one repository by it's owner and repository names.
11
+ # More details look at the corresponding {https://docs.github.com/en/rest/repos/repos#get-a-repository Github Docs page}
12
+ #
13
+ # @param repository_name [String] owner and repository name, 'Rukomoynikov/tabled'
14
+ #
15
+ # @return [Hash] detailed info about the repository.
10
16
  def find(repository_name)
11
17
  unless repository_name.match?(%r{[a-zA-Z]/[a-zA-Z]})
12
18
  raise StandardError, "repository_name parameter is not valid"
13
19
  end
14
20
 
15
- HTTParty
16
- .get(
17
- "https://api.github.com/repos/#{repository_name}",
18
- headers: { "Content-Type" => "application/json",
19
- "Accept" => "application/vnd.github.v3+json",
20
- "Authorization" => (options[:token] ? "token #{options[:token]}" : nil) }
21
- )
22
- .parsed_response
21
+ get("https://api.github.com/repos/#{repository_name}",
22
+ headers: { "Accept" => "application/vnd.github.v3+json",
23
+ "Authorization" => (options[:token] ? "token #{options[:token]}" : nil) })
23
24
  end
24
25
 
25
- def where
26
- "where"
26
+ # Search through Github repositories using query string and additional parameters explained on the {https://docs.github.com/en/rest/search#search-repositories Github Docs page}
27
+ #
28
+ # @param query_string [String] query string to search github repositories
29
+ # @param parameters [Hash] paramaters like per_page, page, sort and order
30
+ #
31
+ # @example Reverse a string
32
+ # class GithubClient < ApiClient
33
+ # act_as_api_client for: :github
34
+ # end
35
+ #
36
+ # GithubClient.new.where('rails')
37
+ # GithubClient.new.where('rails', per_page: 100)
38
+ #
39
+ # @return [Array] list of repositories
40
+ def where(query_string, parameters = {})
41
+ get("https://api.github.com/search/repositories",
42
+ headers: { "Accept" => "application/vnd.github.v3+json",
43
+ "Authorization" => (options[:token] ? "token #{options[:token]}" : nil) },
44
+ params: { q: query_string }.merge(parameters))
27
45
  end
28
46
 
47
+ # Use this method if you need to get list of repositories selected by one of this conditions: user, authenticated_user, organization
48
+ #
49
+ # @param options [Hash] has to have one of these keys: user, authenticated_user, organization
50
+ #
51
+ # @example Reverse a string
52
+ # class GithubClient < ApiClient
53
+ # act_as_api_client for: :github
54
+ # end
55
+ #
56
+ # GithubClient.new.find_by(organization: 'rails')
57
+ #
58
+ # @return [Array] list of repositories
29
59
  def find_by(options = {})
30
- # by_organization https://docs.github.com/en/rest/repos/repos#list-organization-repositories
31
- # by_user https://docs.github.com/en/rest/repos/repos#list-repositories-for-a-user
32
- # by_authenticated_user https://docs.github.com/en/rest/repos/repos#list-repositories-for-the-authenticated-user
33
- "find_by"
34
- end
60
+ unless options.is_a?(Hash)
61
+ raise StandardError, "provide a hash as an argument not #{options.class.to_s.downcase}"
62
+ end
63
+ raise StandardError, "provide at least one parameter" if options.keys.length.zero?
64
+ raise StandardError, "method 'find_by' supports only one parameter" if options.keys.length > 1
35
65
 
36
- def delete
37
- # Call only on queried before repository and repository is not 404/400 and has right to delete (write)
38
- "delete"
39
- end
66
+ url = case options.keys.first
67
+ when :organization
68
+ "https://api.github.com/orgs/#{options[:organization]}/repos"
69
+ when :user
70
+ "https://api.github.com/users/#{options[:user]}/repos"
71
+ when :authenticated_user
72
+ "https://api.github.com/repositories"
73
+ end
40
74
 
41
- def create
42
- "create"
75
+ get(url,
76
+ headers: { "Accept" => "application/vnd.github.v3+json",
77
+ "Authorization" => (options[:token] ? "token #{options[:token]}" : nil) })
43
78
  end
44
79
 
45
- def update
46
- "update"
47
- end
80
+ # def delete
81
+ # # Call only on queried before repository and repository is not 404/400 and has right to delete (write)
82
+ # "delete"
83
+ # end
84
+ #
85
+ # def create
86
+ # "create"
87
+ # end
88
+ #
89
+ # def update
90
+ # "update"
91
+ # end
48
92
  end
49
93
  end
50
94
  end
@@ -1,28 +1,52 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "httparty"
3
+ require "net/http"
4
4
 
5
5
  module ActAsApiClient
6
6
  module Clients
7
7
  module HttpClient
8
+ private
9
+
8
10
  def get(url, options = {})
9
- HTTParty.get(url, options).parsed_response
11
+ # Request part
12
+ uri = URI(url)
13
+ uri.query = URI.encode_www_form(options.fetch(:params, {}))
14
+
15
+ request = Net::HTTP::Get.new(uri)
16
+ request_headers(headers: options.fetch(:headers, {}),
17
+ request: request)
18
+
19
+ # Response part
20
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
21
+ http.request(request)
22
+ end
23
+
24
+ case response
25
+ when Net::HTTPNotFound, Net::HTTPSuccess, Net::HTTPUnprocessableEntity
26
+ JSON.parse(response.body)
27
+ end
10
28
  end
11
29
 
12
30
  def post
13
- HTTParty.post
31
+ # HTTParty.post
14
32
  end
15
33
 
16
34
  def put
17
- HTTParty.put
35
+ # HTTParty.put
18
36
  end
19
37
 
20
38
  def update
21
- HTTParty.update
39
+ # HTTParty.update
22
40
  end
23
41
 
24
42
  def delete
25
- HTTParty.delete
43
+ # HTTParty.delete
44
+ end
45
+
46
+ def request_headers(headers:, request:)
47
+ headers.each do |key, value|
48
+ request[key] = value
49
+ end
26
50
  end
27
51
  end
28
52
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActAsApiClient
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
@@ -15,11 +15,11 @@ class ApiClient
15
15
  private
16
16
 
17
17
  def set_general_client(client_for:)
18
- unless client_for.nil?
19
- require(File.expand_path("act_as_api_client/clients/#{client_for}_client",
20
- File.dirname(__FILE__)))
21
- include const_get("ActAsApiClient::Clients::#{client_for.capitalize}Client")
22
- end
18
+ return if client_for.nil?
19
+
20
+ require(File.expand_path("act_as_api_client/clients/#{client_for}_client",
21
+ File.dirname(__FILE__)))
22
+ include const_get("ActAsApiClient::Clients::#{client_for.capitalize}Client")
23
23
  end
24
24
 
25
25
  def set_options(options:)
metadata CHANGED
@@ -1,31 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: act_as_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Rukomoynikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-04 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: httparty
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '0.20'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '0.20'
11
+ date: 2022-07-11 00:00:00.000000000 Z
12
+ dependencies: []
27
13
  description: Helps you to build reliable API clients in a minute. Just add act_as_api_client
28
- to your classes.
14
+ to your classes
29
15
  email:
30
16
  - rukomoynikov@gmail.com
31
17
  executables: []
@@ -56,8 +42,10 @@ homepage: https://rubygems.org/gems/act_as_api_client
56
42
  licenses:
57
43
  - MIT
58
44
  metadata:
59
- homepage_uri: https://rubygems.org/gems/act_as_api_client
60
45
  source_code_uri: https://github.com/Rukomoynikov/act_as_api_client
46
+ homepage_uri: https://rubygems.org/gems/act_as_api_client
47
+ documentation_uri: https://rubydoc.info/github/Rukomoynikov/act_as_api_client/main
48
+ bug_tracker_uri: https://github.com/Rukomoynikov/act_as_api_client/issues
61
49
  post_install_message:
62
50
  rdoc_options: []
63
51
  require_paths:
@@ -66,14 +54,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
54
  requirements:
67
55
  - - ">="
68
56
  - !ruby/object:Gem::Version
69
- version: 2.3.0
57
+ version: 2.4.0
70
58
  required_rubygems_version: !ruby/object:Gem::Requirement
71
59
  requirements:
72
60
  - - ">="
73
61
  - !ruby/object:Gem::Version
74
62
  version: '0'
75
63
  requirements: []
76
- rubygems_version: 3.2.3
64
+ rubygems_version: 3.1.6
77
65
  signing_key:
78
66
  specification_version: 4
79
67
  summary: Collection of predefined API clients